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