1import fs from 'fs';
2import { vol } from 'memfs';
3import path from 'path';
4
5import rnFixture from '../../plugins/__tests__/fixtures/react-native-project';
6import { setProvisioningProfileForPbxproj } from '../ProvisioningProfile';
7
8jest.mock('fs');
9
10const originalFs = jest.requireActual('fs') as typeof import('fs');
11
12describe('ProvisioningProfile module', () => {
13  describe(setProvisioningProfileForPbxproj, () => {
14    const projectRoot = '/testproject';
15    const pbxProjPath = 'ios/testproject.xcodeproj/project.pbxproj';
16
17    afterEach(() => {
18      vol.reset();
19    });
20
21    describe('multitarget', () => {
22      it('configures the project.pbxproj file for multitarget projects', () => {
23        vol.fromJSON(
24          {
25            [pbxProjPath]: originalFs.readFileSync(
26              path.join(__dirname, 'fixtures/project-multitarget.pbxproj'),
27              'utf-8'
28            ),
29          },
30          projectRoot
31        );
32        setProvisioningProfileForPbxproj(projectRoot, {
33          targetName: 'multitarget',
34          profileName: '*[expo] com.swmansion.dominik.abcd.v2 AppStore 2020-07-24T07:56:22.983Z',
35          appleTeamId: 'J5FM626PE2',
36        });
37        const pbxprojContents = fs.readFileSync(path.join(projectRoot, pbxProjPath), 'utf-8');
38        expect(pbxprojContents).toMatchSnapshot();
39      });
40
41      it('configures the project.pbxproj file for multitarget projects without existing target attributes', () => {
42        vol.fromJSON(
43          {
44            [pbxProjPath]: originalFs.readFileSync(
45              path.join(__dirname, 'fixtures/project-multitarget-missing-targetattributes.pbxproj'),
46              'utf-8'
47            ),
48          },
49          projectRoot
50        );
51        setProvisioningProfileForPbxproj(projectRoot, {
52          targetName: 'multitarget',
53          profileName: '*[expo] com.swmansion.dominik.abcd.v2 AppStore 2020-07-24T07:56:22.983Z',
54          appleTeamId: 'J5FM626PE2',
55        });
56        const pbxprojContents = fs.readFileSync(path.join(projectRoot, pbxProjPath), 'utf-8');
57        expect(pbxprojContents).toMatchSnapshot();
58      });
59    });
60
61    describe('single target', () => {
62      beforeEach(() => {
63        vol.fromJSON(
64          {
65            [pbxProjPath]: rnFixture['ios/HelloWorld.xcodeproj/project.pbxproj'],
66          },
67          projectRoot
68        );
69      });
70
71      it('configures the project.pbxproj file with the profile name and apple team id', () => {
72        setProvisioningProfileForPbxproj(projectRoot, {
73          profileName: '*[expo] com.swmansion.dominik.abcd.v2 AppStore 2020-07-24T07:56:22.983Z',
74          appleTeamId: 'J5FM626PE2',
75        });
76        const pbxprojContents = fs.readFileSync(path.join(projectRoot, pbxProjPath), 'utf-8');
77        expect(pbxprojContents).toMatchSnapshot();
78      });
79      it('configures the project.pbxproj file with the profile name and apple team id', () => {
80        setProvisioningProfileForPbxproj(projectRoot, {
81          profileName: '*[expo] com.swmansion.dominik.abcd.v2 AppStore 2020-07-24T07:56:22.983Z',
82          appleTeamId: 'Something Spaced',
83        });
84        const pbxprojContents = fs.readFileSync(path.join(projectRoot, pbxProjPath), 'utf-8');
85        expect(pbxprojContents).toMatch(/DevelopmentTeam = "Something Spaced";/);
86      });
87      it('throws descriptive error when target name does not exist', () => {
88        expect(() =>
89          setProvisioningProfileForPbxproj(projectRoot, {
90            targetName: 'faketargetname',
91            profileName: '*[expo] com.swmansion.dominik.abcd.v2 AppStore 2020-07-24T07:56:22.983Z',
92            appleTeamId: 'J5FM626PE2',
93          })
94        ).toThrow("Could not find target 'faketargetname' in project.pbxproj");
95      });
96    });
97  });
98});
99