-
Notifications
You must be signed in to change notification settings - Fork 935
Expand file tree
/
Copy pathcreateRun.test.ts
More file actions
172 lines (150 loc) · 4.66 KB
/
createRun.test.ts
File metadata and controls
172 lines (150 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/// <reference types="jest" />
import {Config} from '@react-native-community/cli-types';
import createRun from '../createRun';
import listDevices from '../../../tools/listDevices';
import {runOnSimulator} from '../runOnSimulator';
import {runOnDevice} from '../runOnDevice';
import {getXcodeProjectAndDir} from '../../buildCommand/getXcodeProjectAndDir';
import {getConfiguration} from '../../buildCommand/getConfiguration';
import {getFallbackSimulator} from '../getFallbackSimulator';
import {Device} from '../../../types';
import path from 'path';
const packageRoot = path.resolve(__dirname, '../../../../');
jest.mock('../../../tools/listDevices');
jest.mock('../runOnSimulator');
jest.mock('../runOnDevice');
jest.mock('../../buildCommand/getXcodeProjectAndDir');
jest.mock('../../buildCommand/getConfiguration');
jest.mock('../getFallbackSimulator');
const fallbackSimulator: Device = {
name: 'iPhone 14',
udid: 'FALLBACK-SIM-UDID',
type: 'simulator',
state: 'Shutdown',
version: '17.0',
};
const bootedSimulator: Device = {
name: 'iPhone 17',
udid: 'BOOTED-SIM-UDID',
type: 'simulator',
state: 'Booted',
version: '26.2',
};
const physicalDevice: Device = {
name: 'Stefan’s iPhone 16',
udid: 'PHYSICAL-DEVICE-UDID',
type: 'device',
};
function buildCtx(): Config {
return {
root: packageRoot,
reactNativePath: '',
reactNativeVersion: 'unknown',
project: {
ios: {
sourceDir: packageRoot,
automaticPodsInstallation: false,
},
},
dependencies: {},
} as unknown as Config;
}
function buildArgs(overrides: Record<string, unknown> = {}) {
return {
packager: false,
port: 8081,
terminal: undefined,
listDevices: false,
interactive: false,
onlyPods: false,
forcePods: false,
...overrides,
} as any;
}
describe('createRun no-flag default targeting (issue #2765)', () => {
let chdirSpy: jest.SpyInstance;
beforeEach(() => {
jest.clearAllMocks();
chdirSpy = jest.spyOn(process, 'chdir').mockImplementation(() => {});
(getXcodeProjectAndDir as jest.Mock).mockReturnValue({
xcodeProject: {name: 'Demo', isWorkspace: true},
sourceDir: packageRoot,
});
(getConfiguration as jest.Mock).mockResolvedValue({
mode: 'Debug',
scheme: 'Demo',
});
(getFallbackSimulator as jest.Mock).mockReturnValue(fallbackSimulator);
(runOnSimulator as jest.Mock).mockResolvedValue(undefined);
(runOnDevice as jest.Mock).mockResolvedValue(undefined);
});
afterEach(() => {
chdirSpy.mockRestore();
});
test('connected iPhone + no booted simulator + no flags -> launches fallback simulator only', async () => {
(listDevices as jest.Mock).mockResolvedValue([physicalDevice]);
await createRun({platformName: 'ios'})([], buildCtx(), buildArgs());
expect(runOnSimulator).toHaveBeenCalledTimes(1);
expect(runOnSimulator).toHaveBeenCalledWith(
expect.anything(),
'ios',
'Debug',
'Demo',
expect.anything(),
fallbackSimulator,
);
expect(runOnDevice).not.toHaveBeenCalled();
});
test('connected iPhone + booted simulator + no flags -> launches booted simulator only', async () => {
(listDevices as jest.Mock).mockResolvedValue([
physicalDevice,
bootedSimulator,
]);
await createRun({platformName: 'ios'})([], buildCtx(), buildArgs());
expect(runOnSimulator).toHaveBeenCalledTimes(1);
expect(runOnSimulator).toHaveBeenCalledWith(
expect.anything(),
'ios',
'Debug',
'Demo',
expect.anything(),
bootedSimulator,
);
expect(runOnDevice).not.toHaveBeenCalled();
});
test('connected iPhone + --device "name" -> runs on the physical device', async () => {
(listDevices as jest.Mock).mockResolvedValue([
physicalDevice,
bootedSimulator,
]);
await createRun({platformName: 'ios'})(
[],
buildCtx(),
buildArgs({device: physicalDevice.name}),
);
expect(runOnDevice).toHaveBeenCalledTimes(1);
expect(runOnDevice).toHaveBeenCalledWith(
physicalDevice,
'ios',
'Debug',
'Demo',
expect.anything(),
expect.anything(),
);
expect(runOnSimulator).not.toHaveBeenCalled();
});
test('no devices, no flags -> launches fallback simulator', async () => {
(listDevices as jest.Mock).mockResolvedValue([fallbackSimulator]);
await createRun({platformName: 'ios'})([], buildCtx(), buildArgs());
expect(runOnSimulator).toHaveBeenCalledTimes(1);
expect(runOnSimulator).toHaveBeenCalledWith(
expect.anything(),
'ios',
'Debug',
'Demo',
expect.anything(),
fallbackSimulator,
);
expect(runOnDevice).not.toHaveBeenCalled();
});
});