1import { vol } from 'memfs';
2
3import rnFixture from '../../../prebuild/__tests__/fixtures/react-native-project';
4import { SimulatorLogStreamer } from '../../../start/platforms/ios/simctlLogging';
5import { DevServerManager } from '../../../start/server/DevServerManager';
6import { getAppDeltaDirectory, installOnDeviceAsync } from '../appleDevice/installOnDeviceAsync';
7import { launchAppAsync } from '../launchApp';
8
9jest.mock('../../../log');
10
11jest.mock('../../../utils/env', () => ({
12  env: {
13    CI: false,
14  },
15}));
16
17jest.mock('../../../start/platforms/ios/AppleDeviceManager', () => ({
18  AppleDeviceManager: {
19    resolveAsync: async () => ({
20      installAppAsync: jest.fn(),
21      device: {},
22    }),
23  },
24}));
25
26jest.mock('../../../start/platforms/ios/simctlLogging', () => ({
27  SimulatorLogStreamer: {
28    getStreamer: jest.fn(() => ({ attachAsync: jest.fn() })),
29  },
30}));
31
32jest.mock('../appleDevice/installOnDeviceAsync', () => ({
33  getAppDeltaDirectory: jest.fn(() => '/mock_delta'),
34  installOnDeviceAsync: jest.fn(async () => ''),
35}));
36
37const mockPlist = `<?xml version="1.0" encoding="UTF-8"?>
38<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
39<plist version="1.0">
40  <dict>
41  <key>CFBundleIdentifier</key>
42  <string>dev.bacon.myapp</string>
43  </dict>
44</plist>`;
45
46function getMockDevServerManager() {
47  return {
48    startAsync: jest.fn(),
49    getDefaultDevServer: jest.fn(() => ({
50      openCustomRuntimeAsync: jest.fn(),
51    })),
52  } as unknown as DevServerManager;
53}
54
55describe(launchAppAsync, () => {
56  afterEach(() => vol.reset());
57
58  it(`runs ios on simulator`, async () => {
59    vol.fromJSON({ ...rnFixture, '/path/to/app.ipa/Info.plist': mockPlist }, '/');
60
61    await launchAppAsync('/path/to/app.ipa', getMockDevServerManager(), {
62      device: {
63        name: 'simulator',
64        udid: '123',
65      },
66      isSimulator: true,
67      shouldStartBundler: true,
68    });
69
70    expect(SimulatorLogStreamer.getStreamer).toBeCalled();
71
72    expect(installOnDeviceAsync).not.toBeCalled();
73    expect(getAppDeltaDirectory).not.toBeCalled();
74  });
75  it(`does not streams logs on simulator if the dev server is skipped`, async () => {
76    vol.fromJSON({ ...rnFixture, '/path/to/app.ipa/Info.plist': mockPlist }, '/');
77    await launchAppAsync('/path/to/app.ipa', getMockDevServerManager(), {
78      device: {
79        name: 'simulator',
80        udid: '123',
81      },
82      isSimulator: true,
83      shouldStartBundler: false,
84    });
85
86    expect(SimulatorLogStreamer.getStreamer).not.toBeCalled();
87    expect(installOnDeviceAsync).not.toBeCalled();
88    expect(getAppDeltaDirectory).not.toBeCalled();
89  });
90
91  it(`runs ios on device`, async () => {
92    vol.fromJSON({ ...rnFixture, '/path/to/app.ipa/Info.plist': mockPlist }, '/');
93
94    await launchAppAsync('/path/to/app.ipa', getMockDevServerManager(), {
95      device: {
96        name: "Evan's phone",
97        udid: '00008101-001964A22629003A',
98      },
99      isSimulator: false,
100      shouldStartBundler: true,
101    });
102
103    expect(SimulatorLogStreamer.getStreamer).not.toBeCalled();
104
105    expect(installOnDeviceAsync).toBeCalled();
106    expect(getAppDeltaDirectory).toBeCalled();
107  });
108});
109