1import rnFixture from '../../plugins/__tests__/fixtures/react-native-project';
2import {
3  addGoogleMapsAppDelegateImport,
4  addGoogleMapsAppDelegateInit,
5  addMapsCocoaPods,
6  getGoogleMapsApiKey,
7  MATCH_INIT,
8  removeGoogleMapsAppDelegateImport,
9  removeGoogleMapsAppDelegateInit,
10  removeMapsCocoaPods,
11  setGoogleMapsApiKey,
12} from '../Maps';
13
14describe('MATCH_INIT', () => {
15  it(`matches React AppDelegate`, () => {
16    expect(
17      `- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions`
18    ).toMatch(MATCH_INIT);
19  });
20});
21
22describe(getGoogleMapsApiKey, () => {
23  it(`returns null from all getters if no value provided`, () => {
24    expect(getGoogleMapsApiKey({})).toBe(null);
25  });
26
27  it(`returns the correct values from all getters if a value is provided`, () => {
28    expect(getGoogleMapsApiKey({ ios: { config: { googleMapsApiKey: '123' } } })).toBe('123');
29  });
30});
31describe(getGoogleMapsApiKey, () => {
32  it(`sets the google maps api key if provided or returns plist`, () => {
33    expect(setGoogleMapsApiKey({ ios: { config: { googleMapsApiKey: '123' } } }, {})).toMatchObject(
34      {
35        GMSApiKey: '123',
36      }
37    );
38
39    expect(setGoogleMapsApiKey({}, {})).toMatchObject({});
40  });
41});
42
43describe(addMapsCocoaPods, () => {
44  it(`adds maps pods to Podfile`, () => {
45    const results = addMapsCocoaPods(rnFixture['ios/Podfile']);
46    // matches a static snapshot
47    expect(results.contents).toMatchSnapshot();
48    expect(results.contents).toMatch(/e9cc66c360abe50bc66d89fffb3c55b034d7d369/);
49    // did add new content
50    expect(results.didMerge).toBe(true);
51    // didn't remove old content
52    expect(results.didClear).toBe(false);
53
54    const modded = addMapsCocoaPods(results.contents);
55    // nothing changed
56    expect(modded.didMerge).toBe(false);
57    expect(modded.didClear).toBe(false);
58
59    const modded2 = removeMapsCocoaPods(modded.contents);
60    expect(modded2.contents).toBe(rnFixture['ios/Podfile']);
61    // didn't add new content
62    expect(modded2.didMerge).toBe(false);
63    // did remove the generated content
64    expect(modded2.didClear).toBe(true);
65  });
66});
67
68describe(addGoogleMapsAppDelegateImport, () => {
69  it(`adds maps import to AppDelegate`, () => {
70    const results = addGoogleMapsAppDelegateImport(rnFixture['ios/HelloWorld/AppDelegate.mm']);
71    // matches a static snapshot
72    expect(results.contents).toMatchSnapshot();
73    expect(results.contents).toMatch(/f2f83125c99c0d74b42a2612947510c4e08c423a/);
74    // did add new content
75    expect(results.didMerge).toBe(true);
76    // didn't remove old content
77    expect(results.didClear).toBe(false);
78
79    const modded = addGoogleMapsAppDelegateImport(results.contents);
80    // nothing changed
81    expect(modded.didMerge).toBe(false);
82    expect(modded.didClear).toBe(false);
83
84    const modded2 = removeGoogleMapsAppDelegateImport(modded.contents);
85    expect(modded2.contents).toBe(rnFixture['ios/HelloWorld/AppDelegate.mm']);
86    // didn't add new content
87    expect(modded2.didMerge).toBe(false);
88    // did remove the generated content
89    expect(modded2.didClear).toBe(true);
90  });
91  it(`fails to add to a malformed podfile`, () => {
92    expect(() => addGoogleMapsAppDelegateImport(`foobar`)).toThrow(/foobar/);
93  });
94});
95
96describe(addGoogleMapsAppDelegateInit, () => {
97  it(`adds maps import to AppDelegate`, () => {
98    const results = addGoogleMapsAppDelegateInit(
99      rnFixture['ios/HelloWorld/AppDelegate.mm'],
100      'mykey'
101    );
102    // matches a static snapshot
103    expect(results.contents).toMatchSnapshot();
104    expect(results.contents).toMatch(/97501819d6911e5f50d66c63d369b0cec62853c2/);
105    // did add new content
106    expect(results.didMerge).toBe(true);
107    // didn't remove old content
108    expect(results.didClear).toBe(false);
109
110    const modded = addGoogleMapsAppDelegateInit(results.contents, 'mykey');
111    // nothing changed
112    expect(modded.didMerge).toBe(false);
113    expect(modded.didClear).toBe(false);
114
115    // Test that the block is updated when the API key changes
116    const modded2 = addGoogleMapsAppDelegateInit(results.contents, 'mykey-2');
117    expect(modded2.contents).not.toMatch(/97501819d6911e5f50d66c63d369b0cec62853c2/);
118    expect(modded2.contents).toMatch(/a5c6f82bb5656264220096dea4cfdaa4383d60ab/);
119    // nothing changed
120    expect(modded2.didMerge).toBe(true);
121    expect(modded2.didClear).toBe(true);
122
123    const modded3 = removeGoogleMapsAppDelegateInit(modded.contents);
124    expect(modded3.contents).toBe(rnFixture['ios/HelloWorld/AppDelegate.mm']);
125    // didn't add new content
126    expect(modded3.didMerge).toBe(false);
127    // did remove the generated content
128    expect(modded3.didClear).toBe(true);
129  });
130  it(`adds maps import to AppDelegate`, () => {
131    const results = addGoogleMapsAppDelegateInit(
132      rnFixture['ios/HelloWorld/AppDelegate.mm'],
133      'mykey'
134    );
135    // matches a static snapshot
136    expect(results.contents).toMatchSnapshot();
137    expect(results.contents).toMatch(/97501819d6911e5f50d66c63d369b0cec62853c2/);
138    // did add new content
139    expect(results.didMerge).toBe(true);
140    // didn't remove old content
141    expect(results.didClear).toBe(false);
142  });
143
144  it(`fails to add to a malformed app delegate`, () => {
145    expect(() => addGoogleMapsAppDelegateInit(`foobar`, 'mykey')).toThrow(/foobar/);
146  });
147});
148