1import { vol } from 'memfs';
2
3import { Log } from '../../../log';
4import rnFixture from '../../../prebuild/__tests__/fixtures/react-native-project';
5import { logProjectLogsLocation } from '../../hints';
6import { buildAsync } from '../XcodeBuild';
7import { launchAppAsync } from '../launchApp';
8import { isSimulatorDevice, resolveDeviceAsync } from '../options/resolveDevice';
9import { resolveOptionsAsync } from '../options/resolveOptions';
10import { runIosAsync } from '../runIosAsync';
11
12jest.mock('../../hints', () => ({
13  logProjectLogsLocation: jest.fn(),
14  logDeviceArgument: jest.fn(),
15}));
16
17jest.mock('../../../log');
18
19jest.mock('../../../utils/port');
20
21jest.mock('../options/resolveDevice', () => ({
22  isSimulatorDevice: jest.fn(() => true),
23  resolveDeviceAsync: jest.fn(async () => ({
24    name: 'mock',
25    udid: '123',
26  })),
27}));
28
29jest.mock('../../../utils/env', () => ({
30  env: {
31    CI: false,
32  },
33}));
34
35jest.mock('../../startBundler', () => ({
36  startBundlerAsync: jest.fn(() => ({
37    startAsync: jest.fn(),
38  })),
39}));
40
41jest.mock('../../../start/platforms/ios/AppleDeviceManager', () => ({
42  AppleDeviceManager: {
43    resolveAsync: async () => ({
44      device: {},
45    }),
46  },
47}));
48
49jest.mock('../XcodeBuild', () => ({
50  logPrettyItem: jest.fn(),
51  buildAsync: jest.fn(async () => '...'),
52  getAppBinaryPath: jest.fn(() => '/mock_binary'),
53}));
54
55jest.mock('../launchApp', () => ({
56  launchAppAsync: jest.fn(async () => {}),
57}));
58
59const asMock = <T extends (...args: any[]) => any>(fn: T): jest.MockedFunction<T> =>
60  fn as jest.MockedFunction<T>;
61
62const mockPlatform = (value: typeof process.platform) =>
63  Object.defineProperty(process, 'platform', {
64    value,
65  });
66
67const platform = process.platform;
68
69afterEach(() => {
70  mockPlatform(platform);
71});
72
73describe(resolveOptionsAsync, () => {
74  afterEach(() => vol.reset());
75
76  it(`asserts that the function only runs on darwin machines`, async () => {
77    mockPlatform('win32');
78    await expect(runIosAsync('/', {})).rejects.toThrow(/EXIT_CALLED/);
79    expect(Log.exit).toBeCalledWith(expect.stringMatching(/eas build -p ios/));
80  });
81
82  it(`runs ios on simulator`, async () => {
83    mockPlatform('darwin');
84    vol.fromJSON(rnFixture, '/');
85
86    await runIosAsync('/', {});
87
88    expect(buildAsync).toBeCalledWith({
89      buildCache: true,
90      configuration: 'Debug',
91      device: { name: 'mock', udid: '123' },
92      isSimulator: true,
93      port: 8081,
94      projectRoot: '/',
95      scheme: 'ReactNativeProject',
96      shouldSkipInitialBundling: false,
97      shouldStartBundler: true,
98      xcodeProject: { isWorkspace: false, name: '/ios/ReactNativeProject.xcodeproj' },
99    });
100
101    expect(launchAppAsync).toBeCalledWith('/mock_binary', expect.anything(), {
102      device: { name: 'mock', udid: '123' },
103      isSimulator: true,
104      shouldStartBundler: true,
105    });
106
107    expect(logProjectLogsLocation).toBeCalled();
108  });
109
110  it(`runs ios on device`, async () => {
111    asMock(resolveDeviceAsync).mockResolvedValueOnce({
112      name: "Evan's phone",
113      model: 'iPhone13,4',
114      osVersion: '15.4.1',
115      deviceType: 'device',
116      udid: '00008101-001964A22629003A',
117    });
118    asMock(isSimulatorDevice).mockReturnValueOnce(false);
119    mockPlatform('darwin');
120    vol.fromJSON(rnFixture, '/');
121
122    await runIosAsync('/', { device: '00008101-001964A22629003A' });
123
124    expect(buildAsync).toBeCalledWith({
125      buildCache: true,
126      configuration: 'Debug',
127      device: {
128        deviceType: 'device',
129        model: 'iPhone13,4',
130        name: "Evan's phone",
131        osVersion: '15.4.1',
132        udid: '00008101-001964A22629003A',
133      },
134      isSimulator: false,
135      port: 8081,
136      projectRoot: '/',
137      scheme: 'ReactNativeProject',
138      shouldSkipInitialBundling: true,
139      shouldStartBundler: true,
140      xcodeProject: { isWorkspace: false, name: '/ios/ReactNativeProject.xcodeproj' },
141    });
142
143    expect(launchAppAsync).toBeCalledWith('/mock_binary', expect.anything(), {
144      device: {
145        deviceType: 'device',
146        model: 'iPhone13,4',
147        name: "Evan's phone",
148        osVersion: '15.4.1',
149        udid: '00008101-001964A22629003A',
150      },
151      isSimulator: false,
152      shouldStartBundler: true,
153    });
154
155    expect(logProjectLogsLocation).toBeCalled();
156  });
157});
158