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 {
14  ExpoModulesAppDelegate,
15  ExpoReactAppDelegate,
16  UnimodulesAppDelegate,
17} from './fixtures/AppDelegate';
18import { PodfileBasic } from './fixtures/Podfile';
19
20describe('MATCH_INIT', () => {
21  it(`matches unimodules projects`, () => {
22    expect(
23      `self.moduleRegistryAdapter = [[UMModuleRegistryAdapter alloc] initWithModuleRegistryProvider:[[UMModuleRegistryProvider alloc] init]];`
24    ).toMatch(MATCH_INIT);
25    // wrapped
26    expect(`self.moduleRegistryAdapter = [[UMModuleRegistryAdapter alloc]`).toMatch(MATCH_INIT);
27    // short hand
28    expect(`_moduleRegistryAdapter=[[UMModuleRegistryAdapter alloc]`).toMatch(MATCH_INIT);
29    // any name
30    expect(`_mo=[[UMModuleRegistryAdapter alloc]`).toMatch(MATCH_INIT);
31  });
32  it(`matches RN projects`, () => {
33    expect(
34      `RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];`
35    ).toMatch(MATCH_INIT);
36    // wrapped
37    expect(`RCTBridge*bri=[[RCTBridge alloc]`).toMatch(MATCH_INIT);
38  });
39  it(`matches React AppDelegate`, () => {
40    expect(
41      `RCTBridge *bridge = [self.reactDelegate createBridgeWithDelegate:self launchOptions:launchOptions];`
42    ).toMatch(MATCH_INIT);
43  });
44});
45
46describe(getGoogleMapsApiKey, () => {
47  it(`returns null from all getters if no value provided`, () => {
48    expect(getGoogleMapsApiKey({})).toBe(null);
49  });
50
51  it(`returns the correct values from all getters if a value is provided`, () => {
52    expect(getGoogleMapsApiKey({ ios: { config: { googleMapsApiKey: '123' } } })).toBe('123');
53  });
54});
55describe(getGoogleMapsApiKey, () => {
56  it(`sets the google maps api key if provided or returns plist`, () => {
57    expect(setGoogleMapsApiKey({ ios: { config: { googleMapsApiKey: '123' } } }, {})).toMatchObject(
58      {
59        GMSApiKey: '123',
60      }
61    );
62
63    expect(setGoogleMapsApiKey({}, {})).toMatchObject({});
64  });
65});
66
67describe(addMapsCocoaPods, () => {
68  it(`adds maps pods to Podfile`, () => {
69    const results = addMapsCocoaPods(PodfileBasic);
70    // matches a static snapshot
71    expect(results.contents).toMatchSnapshot();
72    expect(results.contents).toMatch(/e9cc66c360abe50bc66d89fffb3c55b034d7d369/);
73    // did add new content
74    expect(results.didMerge).toBe(true);
75    // didn't remove old content
76    expect(results.didClear).toBe(false);
77
78    const modded = addMapsCocoaPods(results.contents);
79    // nothing changed
80    expect(modded.didMerge).toBe(false);
81    expect(modded.didClear).toBe(false);
82
83    const modded2 = removeMapsCocoaPods(modded.contents);
84    expect(modded2.contents).toBe(PodfileBasic);
85    // didn't add new content
86    expect(modded2.didMerge).toBe(false);
87    // did remove the generated content
88    expect(modded2.didClear).toBe(true);
89  });
90});
91
92describe(addGoogleMapsAppDelegateImport, () => {
93  it(`adds maps import to AppDelegate`, () => {
94    const results = addGoogleMapsAppDelegateImport(
95      rnFixture['ios/ReactNativeProject/AppDelegate.m']
96    );
97    // matches a static snapshot
98    expect(results.contents).toMatchSnapshot();
99    expect(results.contents).toMatch(/f2f83125c99c0d74b42a2612947510c4e08c423a/);
100    // did add new content
101    expect(results.didMerge).toBe(true);
102    // didn't remove old content
103    expect(results.didClear).toBe(false);
104
105    const modded = addGoogleMapsAppDelegateImport(results.contents);
106    // nothing changed
107    expect(modded.didMerge).toBe(false);
108    expect(modded.didClear).toBe(false);
109
110    const modded2 = removeGoogleMapsAppDelegateImport(modded.contents);
111    expect(modded2.contents).toBe(rnFixture['ios/ReactNativeProject/AppDelegate.m']);
112    // didn't add new content
113    expect(modded2.didMerge).toBe(false);
114    // did remove the generated content
115    expect(modded2.didClear).toBe(true);
116  });
117  it(`fails to add to a malformed podfile`, () => {
118    expect(() => addGoogleMapsAppDelegateImport(`foobar`)).toThrow(/foobar/);
119  });
120});
121
122describe(addGoogleMapsAppDelegateInit, () => {
123  it(`adds maps import to AppDelegate`, () => {
124    const results = addGoogleMapsAppDelegateInit(
125      rnFixture['ios/ReactNativeProject/AppDelegate.m'],
126      'mykey'
127    );
128    // matches a static snapshot
129    expect(results.contents).toMatchSnapshot();
130    expect(results.contents).toMatch(/97501819d6911e5f50d66c63d369b0cec62853c2/);
131    // did add new content
132    expect(results.didMerge).toBe(true);
133    // didn't remove old content
134    expect(results.didClear).toBe(false);
135
136    const modded = addGoogleMapsAppDelegateInit(results.contents, 'mykey');
137    // nothing changed
138    expect(modded.didMerge).toBe(false);
139    expect(modded.didClear).toBe(false);
140
141    // Test that the block is updated when the API key changes
142    const modded2 = addGoogleMapsAppDelegateInit(results.contents, 'mykey-2');
143    expect(modded2.contents).not.toMatch(/97501819d6911e5f50d66c63d369b0cec62853c2/);
144    expect(modded2.contents).toMatch(/a5c6f82bb5656264220096dea4cfdaa4383d60ab/);
145    // nothing changed
146    expect(modded2.didMerge).toBe(true);
147    expect(modded2.didClear).toBe(true);
148
149    const modded3 = removeGoogleMapsAppDelegateInit(modded.contents);
150    expect(modded3.contents).toBe(rnFixture['ios/ReactNativeProject/AppDelegate.m']);
151    // didn't add new content
152    expect(modded3.didMerge).toBe(false);
153    // did remove the generated content
154    expect(modded3.didClear).toBe(true);
155  });
156  it(`adds maps import to Unimodules AppDelegate`, () => {
157    const results = addGoogleMapsAppDelegateInit(UnimodulesAppDelegate, 'mykey');
158    // matches a static snapshot
159    expect(results.contents).toMatchSnapshot();
160    expect(results.contents).toMatch(/97501819d6911e5f50d66c63d369b0cec62853c2/);
161    // did add new content
162    expect(results.didMerge).toBe(true);
163    // didn't remove old content
164    expect(results.didClear).toBe(false);
165  });
166  it(`adds maps import to Expo Modules AppDelegate`, () => {
167    const results = addGoogleMapsAppDelegateInit(ExpoModulesAppDelegate, 'mykey');
168    // matches a static snapshot
169    expect(results.contents).toMatchSnapshot();
170    expect(results.contents).toMatch(/97501819d6911e5f50d66c63d369b0cec62853c2/);
171    // did add new content
172    expect(results.didMerge).toBe(true);
173    // didn't remove old content
174    expect(results.didClear).toBe(false);
175  });
176  it(`adds maps import to Expo Modules SDK 44 AppDelegate`, () => {
177    const results = addGoogleMapsAppDelegateInit(ExpoReactAppDelegate, 'mykey');
178    // matches a static snapshot
179    expect(results.contents).toMatchSnapshot();
180    expect(results.contents).toMatch(/97501819d6911e5f50d66c63d369b0cec62853c2/);
181    // did add new content
182    expect(results.didMerge).toBe(true);
183    // didn't remove old content
184    expect(results.didClear).toBe(false);
185  });
186
187  it(`fails to add to a malformed app delegate`, () => {
188    expect(() => addGoogleMapsAppDelegateInit(`foobar`, 'mykey')).toThrow(/foobar/);
189  });
190});
191