1import * as fs from 'fs';
2import { vol } from 'memfs';
3import * as path from 'path';
4
5import { createBuildSourceFile } from '../XcodeProjectFile';
6import { getPbxproj } from '../utils/Xcodeproj';
7
8const fsReal = jest.requireActual('fs') as typeof fs;
9
10jest.mock('fs');
11
12describe(createBuildSourceFile, () => {
13  const projectRoot = '/alpha';
14  beforeAll(async () => {
15    vol.fromJSON(
16      {
17        'ios/testproject.xcodeproj/project.pbxproj': fsReal.readFileSync(
18          path.join(__dirname, 'fixtures/project.pbxproj'),
19          'utf-8'
20        ),
21        'ios/testproject/AppDelegate.m': '',
22      },
23      projectRoot
24    );
25  });
26
27  afterAll(() => {
28    vol.reset();
29  });
30
31  it(`creates a source file`, () => {
32    const project = getPbxproj(projectRoot);
33    // perform action
34    createBuildSourceFile({
35      project,
36      nativeProjectRoot: path.join(projectRoot, 'ios'),
37      filePath: 'testproject/myfile.swift',
38      fileContents: '// hello',
39    });
40
41    expect(project.hasFile('testproject/myfile.swift')).toStrictEqual({
42      explicitFileType: undefined,
43      fileEncoding: 4,
44      includeInIndex: 0,
45      isa: 'PBXFileReference',
46      lastKnownFileType: 'sourcecode.swift',
47      name: '"myfile.swift"',
48      path: '"testproject/myfile.swift"',
49      sourceTree: '"<group>"',
50    });
51
52    expect(vol.existsSync(path.join(projectRoot, 'ios/testproject/myfile.swift'))).toBe(true);
53  });
54});
55