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