Skip to content

Commit 0de9d36

Browse files
authored
Merge pull request #30 from Iterable/MOB-11403-add-tests-for-withstoreconfigvalues
[MOB-11403] add-tests-for-withstoreconfigvalues
2 parents 716f154 + 7bd8d99 commit 0de9d36

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

plugin/__mocks__/testUtils.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { ExpoConfig } from 'expo/config';
2+
import {
3+
ExportedConfigWithProps,
4+
Mod,
5+
ModPlatform,
6+
ModProps,
7+
} from 'expo/config-plugins';
8+
9+
// Extend ExpoConfig to include mods
10+
export interface ConfigWithMods extends ExpoConfig {
11+
mods?: {
12+
[key in ModPlatform]?: {
13+
[key in string]?: Mod<any>;
14+
};
15+
};
16+
}
17+
18+
// Type for the result of withIterable
19+
export type WithIterableResult = ConfigWithMods & {
20+
mods: {
21+
ios: {
22+
infoPlist: Mod<Record<string, any>>;
23+
entitlements: Mod<Record<string, any>>;
24+
podfile: Mod<Record<string, any>>;
25+
dangerous: Mod<Record<string, any>>;
26+
xcodeproj: Mod<any>;
27+
};
28+
android: {
29+
manifest: Mod<Record<string, any>>;
30+
projectBuildGradle: Mod<Record<string, any>>;
31+
appBuildGradle: Mod<Record<string, any>>;
32+
dangerous: Mod<Record<string, any>>;
33+
};
34+
};
35+
};
36+
37+
/**
38+
* Creates a function that generates mock config objects for a given platform.
39+
* @param platform - The platform to create the mock config for.
40+
* @returns A function that generates mock config objects for the given platform.
41+
*/
42+
const createConfigFunction =
43+
(platform: ModPlatform) =>
44+
(modName: string): ExportedConfigWithProps<Record<string, any>> => ({
45+
modResults: {},
46+
modRequest: {
47+
projectRoot: process.cwd(),
48+
platformProjectRoot: process.cwd(),
49+
modName,
50+
platform,
51+
introspect: true,
52+
severity: 'info',
53+
} as ModProps<Record<string, any>>,
54+
modRawConfig: { name: 'TestApp', slug: 'test-app' },
55+
name: 'TestApp',
56+
slug: 'test-app',
57+
});
58+
59+
/**
60+
* Creates a mock config object for the iOS platform.
61+
* @param modName - The name of the module to create the mock config for.
62+
* @returns A mock config object for the iOS platform.
63+
*/
64+
export const createMockIosConfig = createConfigFunction('ios');
65+
66+
/**
67+
* Creates a mock config object for the Android platform.
68+
* @param modName - The name of the module to create the mock config for.
69+
* @returns A mock config object for the Android platform.
70+
*/
71+
export const createMockConfig = (
72+
modName: string
73+
): ExportedConfigWithProps<Record<string, any>> => ({
74+
modResults: {},
75+
modRequest: {
76+
projectRoot: process.cwd(),
77+
platformProjectRoot: process.cwd(),
78+
modName,
79+
platform: 'android',
80+
introspect: true,
81+
severity: 'info',
82+
} as ModProps<Record<string, any>>,
83+
modRawConfig: { name: 'TestApp', slug: 'test-app' },
84+
name: 'TestApp',
85+
slug: 'test-app',
86+
});
87+
88+
/**
89+
* Creates a mock config object for the Info.plist file.
90+
* @param modResults - The results of the module to create the mock config for.
91+
* @returns A mock config object for the Info.plist file.
92+
*/
93+
export const createMockPlistConfig = (
94+
modResults: Record<string, any> = {}
95+
): ExportedConfigWithProps<Record<string, any>> => ({
96+
...createMockIosConfig('infoPlist'),
97+
modResults,
98+
});
99+
100+
export const createMockManifestConfig = (
101+
modResults: Record<string, any> = {}
102+
): ExportedConfigWithProps<Record<string, any>> => ({
103+
...createMockConfig('manifest'),
104+
modResults,
105+
});
106+
107+
/**
108+
* Creates a mock Android manifest object.
109+
* @returns A mock Android manifest object.
110+
*/
111+
export const createMockAndroidManifest = (): Record<string, any> => ({
112+
manifest: {
113+
application: [
114+
{ $: { 'android:name': '.MainApplication' }, activity: [{ $: {} }] },
115+
],
116+
},
117+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { type Mod } from 'expo/config-plugins';
2+
3+
import {
4+
createMockAndroidManifest,
5+
createMockManifestConfig,
6+
createMockPlistConfig,
7+
type ConfigWithMods,
8+
type WithIterableResult,
9+
} from '../__mocks__/testUtils';
10+
import withIterable from '../src/withIterable';
11+
import { type ConfigPluginProps } from '../src/withIterable.types';
12+
13+
const createTestConfig = (): ConfigWithMods => ({
14+
name: 'TestApp',
15+
slug: 'test-app',
16+
ios: { infoPlist: {}, entitlements: {} },
17+
android: { googleServicesFile: './__mocks__/google-services.json' },
18+
_internal: { projectRoot: process.cwd() },
19+
});
20+
21+
describe('withStoreConfigValues', () => {
22+
it('should store `ITERABLE_REQUEST_PERMISSIONS_FOR_PUSH_NOTIFICATIONS` in Info.plist', async () => {
23+
const config = createTestConfig();
24+
const props: ConfigPluginProps = {
25+
requestPermissionsForPushNotifications: true,
26+
};
27+
const result = withIterable(config, props) as WithIterableResult;
28+
const modifiedInfoPlist = await result.mods.ios.infoPlist(
29+
createMockPlistConfig()
30+
);
31+
expect(
32+
modifiedInfoPlist.modResults
33+
.ITERABLE_REQUEST_PERMISSIONS_FOR_PUSH_NOTIFICATIONS
34+
).toBe(true);
35+
});
36+
37+
it('should add `ITERABLE_REQUEST_PERMISSIONS_FOR_PUSH_NOTIFICATIONS` to Android Manifest', async () => {
38+
const config = createTestConfig();
39+
const props: ConfigPluginProps = {
40+
requestPermissionsForPushNotifications: true,
41+
};
42+
const result = withIterable(config, props) as WithIterableResult;
43+
const manifestMod = result.mods.android.manifest as Mod<any>;
44+
const modifiedManifest = await manifestMod(
45+
createMockManifestConfig(createMockAndroidManifest())
46+
);
47+
const manifest = modifiedManifest.modResults.manifest;
48+
expect(manifest.application[0]['meta-data']).toEqual(
49+
expect.arrayContaining([
50+
expect.objectContaining({
51+
$: {
52+
'android:name':
53+
'ITERABLE_REQUEST_PERMISSIONS_FOR_PUSH_NOTIFICATIONS',
54+
'android:value': 'true',
55+
},
56+
}),
57+
])
58+
);
59+
});
60+
});

0 commit comments

Comments
 (0)