1import * as fs from 'fs';
2import { vol } from 'memfs';
3import * as path from 'path';
4
5import rnFixture from '../../plugins/__tests__/fixtures/react-native-project';
6import { compileModsAsync } from '../../plugins/mod-compiler';
7import {
8  ensureSwiftBridgingHeaderSetup,
9  getDesignatedSwiftBridgingHeaderFileReference,
10  withNoopSwiftFile,
11} from '../Swift';
12import { getPbxproj } from '../utils/Xcodeproj';
13
14jest.mock('fs');
15
16describe(ensureSwiftBridgingHeaderSetup, () => {
17  afterEach(() => {
18    vol.reset();
19  });
20
21  it(`creates a bridging header when none are designated`, () => {
22    const projectRoot = '/';
23    vol.fromJSON(rnFixture, projectRoot);
24    const project = getPbxproj(projectRoot);
25    // perform action
26    ensureSwiftBridgingHeaderSetup({ projectRoot, project });
27
28    expect(getDesignatedSwiftBridgingHeaderFileReference({ project })).toBe(
29      'HelloWorld/HelloWorld-Bridging-Header.h'
30    );
31
32    expect(
33      vol.existsSync(path.join(projectRoot, 'ios/HelloWorld/HelloWorld-Bridging-Header.h'))
34    ).toBe(true);
35  });
36
37  it(`skips creating a bridging header when using swift`, () => {
38    const projectRoot = '/';
39    vol.fromJSON(
40      {
41        'ios/HelloWorld.xcodeproj/project.pbxproj':
42          rnFixture['ios/HelloWorld.xcodeproj/project.pbxproj'],
43        'ios/HelloWorld/AppDelegate.swift': '',
44      },
45      projectRoot
46    );
47    const project = getPbxproj(projectRoot);
48    // perform action
49    ensureSwiftBridgingHeaderSetup({ projectRoot, project });
50
51    // Won't link a bridging header
52    expect(getDesignatedSwiftBridgingHeaderFileReference({ project })).toBe(null);
53
54    expect(
55      vol.existsSync(path.join(projectRoot, 'ios/HelloWorld/HelloWorld-Bridging-Header.h'))
56    ).toBe(false);
57  });
58});
59
60describe(withNoopSwiftFile, () => {
61  afterEach(() => {
62    vol.reset();
63  });
64
65  it(`creates a noop swift file`, async () => {
66    const projectRoot = '/alpha';
67    vol.fromJSON(rnFixture, projectRoot);
68    const config = withNoopSwiftFile({
69      name: 'testproject',
70      slug: 'testproject',
71    });
72
73    await compileModsAsync(config, { projectRoot: '/alpha', platforms: ['ios'] });
74    expect(fs.existsSync('/alpha/ios/HelloWorld/noop-file.swift')).toBeTruthy();
75  });
76});
77