-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.unit.test.js
64 lines (56 loc) · 1.5 KB
/
index.unit.test.js
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
jest.mock('https');
const {
handler
} = require('./index');
jest.useFakeTimers();
function flushPromises() {
return new Promise(resolve => setImmediate(resolve));
}
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.resetAllMocks();
});
test('single click', async () => {
process.env.USERNAME = 'test';
process.env.PASSWORD = 'test';
process.env.VEHICLE_ID = 'abc';
process.env.SINGLE_CLICK = 'OpenChargingPort&OpenTrunk,1000,FlashLights';
let caughtError;
handler({
clickType: 'SINGLE'
}, null, error => caughtError = error);
// ¯\_(ツ)_/¯
await flushPromises();
jest.runAllTimers();
await flushPromises();
jest.runAllTimers();
expect(caughtError).toBeFalsy();
expect(setTimeout).toHaveBeenCalledTimes(2);
});
test('double click', async () => {
process.env.USERNAME = 'test';
process.env.PASSWORD = 'test';
process.env.VEHICLE_ID = 'abc';
process.env.DOUBLE_CLICK = 'UnlockDoors,1000,FlashLights,HonkHorn,AutoConditioningStart';
let caughtError;
handler({
clickType: 'DOUBLE'
}, null, error => caughtError = error);
// ¯\_(ツ)_/¯
await flushPromises();
jest.runAllTimers();
await flushPromises();
jest.runAllTimers();
expect(caughtError).toBeFalsy();
expect(setTimeout).toHaveBeenCalledTimes(2);
});
test('invalid login', async () => {
process.env.USERNAME = 'est';
process.env.PASSWORD = 'est';
process.env.VEHICLE_ID = 'abc';
const s = handler({
clickType: 'SINGLE'
}, null, error => expect(error).toBeTruthy());
});