/
runner.test.ts
140 lines (122 loc) 路 5.3 KB
/
runner.test.ts
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
import { getAndroidTargets, composeDevicesArray, connectToWifiDevice, checkForActiveEmulator } from '../deviceManager';
import { getAndroidDeviceToRunOn } from '../runner';
import { createRnvContext, getContext, inquirerPrompt } from '@rnv/core';
import net from 'net';
jest.mock('../deviceManager');
jest.mock('@rnv/core');
jest.mock('net');
beforeEach(() => {
createRnvContext();
});
afterEach(() => {
jest.resetAllMocks();
});
describe('getAndroidDeviceToRunOn', () => {
it('should fail if a device is provided but no active device exists', async () => {
//GIVEN
const ctx = getContext();
ctx.platform = 'android';
ctx.program.target = true;
ctx.program.device = 'device1';
ctx.runtime.target = 'defaultTarget';
const mockFoundDevice = { name: 'simulator1', isActive: false, udid: '', isDevice: false };
jest.mocked(getAndroidTargets).mockResolvedValue([mockFoundDevice]);
//WHEN
await expect(getAndroidDeviceToRunOn()).resolves.toBe(undefined);
//THEN
});
it('should fail if targetToConnectWiFi is not a valid IP address - npx rnv -p android -t -d <invalidIPAdress>', async () => {
//GIVEN
const targetToConnectWiFi = 'invalidIPAdress';
const ctx = getContext();
ctx.platform = 'android';
ctx.program.target = true;
ctx.runtime.target = 'defaultTarget';
ctx.program.device = targetToConnectWiFi;
const mockFoundDevice = { name: 'simulator1', isActive: false, udid: '', isDevice: false };
net.isIP = jest.fn().mockReturnValue(false);
jest.mocked(getAndroidTargets).mockResolvedValue([mockFoundDevice]);
//WHEN
await expect(getAndroidDeviceToRunOn()).resolves.toBe(undefined);
//THEN
expect(connectToWifiDevice).not.toHaveBeenCalled();
});
it('should connect to WiFi device if targetToConnectWiFi is a string and a valid IP address', async () => {
//GIVEN
const targetToConnectWiFi = '192.168.0.1';
const ctx = getContext();
ctx.platform = 'android';
ctx.program.target = true;
ctx.runtime.target = 'defaultTarget';
ctx.program.device = targetToConnectWiFi;
net.isIP = jest.fn().mockReturnValue(true);
jest.mocked(connectToWifiDevice).mockResolvedValue(true);
const mockFoundDevice = { name: '192.168.0.1', isActive: true, udid: '', isDevice: true };
jest.mocked(getAndroidTargets).mockResolvedValue([mockFoundDevice]);
jest.mocked(checkForActiveEmulator).mockResolvedValue(mockFoundDevice);
//WHEN
const result = await getAndroidDeviceToRunOn();
//THEN
expect(connectToWifiDevice).toHaveBeenCalledWith(targetToConnectWiFi);
expect(result).toEqual(mockFoundDevice);
});
it('should return defaultTarget if it exists and -t is not specified', async () => {
//GIVEN
const ctx = getContext();
ctx.platform = 'android';
ctx.program.target = undefined;
ctx.runtime.target = 'defaultTarget';
const mockFoundDevice = { name: 'defaultTarget', isActive: true, udid: '', isDevice: false };
jest.mocked(getAndroidTargets).mockResolvedValue([mockFoundDevice]);
const result = await getAndroidDeviceToRunOn();
//THEN
expect(result).toEqual(mockFoundDevice);
});
it('should return found sim if target is provided and found - npx rnv -p android -t <target>', async () => {
//GIVEN
const ctx = getContext();
ctx.platform = 'android';
ctx.program.target = 'existingTarget';
ctx.runtime.target = 'defaultTarget';
const mockFoundDevice = { name: 'existingTarget', isActive: true, udid: '' };
jest.mocked(getAndroidTargets).mockResolvedValueOnce([mockFoundDevice]);
//WHEN
const result = await getAndroidDeviceToRunOn();
//THEN
expect(result).toEqual(mockFoundDevice);
});
it('should ask devices and sims if no target is specified and the available target list does not include defaultTarget - npx rnv -p android - npx rnv -p android', async () => {
//GIVEN
const ctx = getContext();
ctx.platform = 'android';
ctx.program.target = false;
ctx.runtime.target = 'defaultTarget';
const mockDevicesAndEmulators = [
{ name: 'simulator1', udid: 'udid1', isActive: true },
{ name: 'simulator2', udid: 'udid2', isActive: false },
];
jest.mocked(getAndroidTargets).mockResolvedValue(mockDevicesAndEmulators);
jest.mocked(composeDevicesArray)
.mockReturnValueOnce([
{
key: 'simulator1',
name: 'simulator1',
value: 'simulator1',
icon: 'Phone 馃摫',
},
])
.mockReturnValueOnce([
{
key: 'simulator2',
name: 'simulator2',
value: 'simulator2',
icon: 'Phone 馃摫',
},
]);
jest.mocked(inquirerPrompt).mockResolvedValue({ chosenTarget: 'simulator1' });
//WHEN
const result = await getAndroidDeviceToRunOn();
//THEN
expect(result).toEqual(mockDevicesAndEmulators[0]);
});
});