1import { IOSConfig } from 'expo/config-plugins';
2import { fs, vol } from 'memfs';
3import * as path from 'path';
4
5import { getDirFromFS } from './withNotificationsAndroid-test';
6import { setNotificationSounds } from '../withNotificationsIOS';
7
8jest.mock('fs');
9
10const fsReal = jest.requireActual('fs') as typeof fs;
11
12const template = path.join(
13  __dirname,
14  '../../../../../templates/expo-template-bare-minimum/ios/HelloWorld.xcodeproj/project.pbxproj'
15);
16const pbxproj = fsReal.readFileSync(template, 'utf-8');
17const LIST_OF_GENERATED_FILES = [
18  'assets/notificationSound.wav',
19  'ios/testproject/notificationSound.wav',
20  'ios/testproject.xcodeproj/project.pbxproj',
21  'ios/testproject/AppDelegate.m',
22];
23
24const soundPath = path.resolve(__dirname, './fixtures/cat.wav');
25
26const projectRoot = '/app';
27
28describe('iOS notifications configuration', () => {
29  beforeAll(async () => {
30    jest.mock('fs');
31    const sound = fsReal.readFileSync(soundPath);
32    vol.fromJSON({ 'ios/testproject/AppDelegate.m': '' }, projectRoot);
33    vol.mkdirpSync('/app/assets');
34    vol.mkdirpSync('/app/ios/testproject.xcodeproj/');
35    vol.writeFileSync('/app/assets/notificationSound.wav', sound);
36    vol.writeFileSync('/app/ios/testproject.xcodeproj/project.pbxproj', pbxproj);
37  });
38
39  afterAll(() => {
40    jest.unmock('fs');
41    vol.reset();
42  });
43
44  it('writes all the asset files (sounds and images) as expected', async () => {
45    const project = IOSConfig.XcodeUtils.getPbxproj(projectRoot);
46    // TODO: test pbxproj result via snapshot
47    setNotificationSounds(projectRoot, {
48      sounds: ['/app/assets/notificationSound.wav'],
49      project,
50      projectName: 'testproject',
51    });
52
53    const after = getDirFromFS(vol.toJSON(), projectRoot);
54    expect(Object.keys(after).sort()).toEqual(LIST_OF_GENERATED_FILES.sort());
55  });
56});
57