1import * as fs from 'fs';
2import { vol } from 'memfs';
3import * as path from 'path';
4
5import { compileModsAsync } from '../../plugins/mod-compiler';
6import {
7  ensureSwiftBridgingHeaderSetup,
8  getDesignatedSwiftBridgingHeaderFileReference,
9  withNoopSwiftFile,
10} from '../Swift';
11import { getPbxproj } from '../utils/Xcodeproj';
12
13const fsReal = jest.requireActual('fs') as typeof fs;
14
15jest.mock('fs');
16
17describe(ensureSwiftBridgingHeaderSetup, () => {
18  const projectRoot = '/alpha';
19  const projectRootSwift = '/swift';
20  beforeAll(async () => {
21    vol.fromJSON(
22      {
23        'ios/testproject.xcodeproj/project.pbxproj': fsReal.readFileSync(
24          path.join(__dirname, 'fixtures/project.pbxproj'),
25          'utf-8'
26        ),
27        'ios/testproject/AppDelegate.m': '',
28      },
29      projectRoot
30    );
31    vol.fromJSON(
32      {
33        'ios/testproject.xcodeproj/project.pbxproj': fsReal.readFileSync(
34          path.join(__dirname, 'fixtures/project-swift.pbxproj'),
35          'utf-8'
36        ),
37        'ios/testproject/AppDelegate.swift': '',
38      },
39      projectRootSwift
40    );
41  });
42
43  afterAll(() => {
44    vol.reset();
45  });
46
47  it(`creates a bridging header when none are designated`, () => {
48    const project = getPbxproj(projectRoot);
49    // perform action
50    ensureSwiftBridgingHeaderSetup({ projectRoot, project });
51
52    expect(getDesignatedSwiftBridgingHeaderFileReference({ project })).toBe(
53      'testproject/testproject-Bridging-Header.h'
54    );
55
56    expect(
57      vol.existsSync(path.join(projectRoot, 'ios/testproject/testproject-Bridging-Header.h'))
58    ).toBe(true);
59  });
60
61  it(`skips creating a bridging header when using swift`, () => {
62    const project = getPbxproj(projectRootSwift);
63    // perform action
64    ensureSwiftBridgingHeaderSetup({ projectRoot: projectRootSwift, project });
65
66    // Won't link a bridging header
67    expect(getDesignatedSwiftBridgingHeaderFileReference({ project })).toBe(null);
68
69    expect(
70      vol.existsSync(path.join(projectRootSwift, 'ios/testproject/testproject-Bridging-Header.h'))
71    ).toBe(false);
72  });
73});
74
75describe(withNoopSwiftFile, () => {
76  const projectRoot = '/alpha';
77  beforeAll(async () => {
78    vol.fromJSON(
79      {
80        'ios/testproject.xcodeproj/project.pbxproj': fsReal.readFileSync(
81          path.join(__dirname, 'fixtures/project.pbxproj'),
82          'utf-8'
83        ),
84        'ios/testproject/AppDelegate.m': '',
85      },
86      projectRoot
87    );
88  });
89
90  afterAll(() => {
91    vol.reset();
92  });
93
94  it(`creates a noop swift file`, async () => {
95    const config = withNoopSwiftFile({
96      name: 'testproject',
97      slug: 'testproject',
98    });
99
100    await compileModsAsync(config, { projectRoot: '/alpha', platforms: ['ios'] });
101    expect(fs.existsSync('/alpha/ios/testproject/noop-file.swift')).toBeTruthy();
102  });
103});
104