-
Notifications
You must be signed in to change notification settings - Fork 3
/
storage.test.ts
103 lines (95 loc) · 2.64 KB
/
storage.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
import { init } from './storage';
import { describe, vi, expect, test, beforeEach } from 'vitest';
import { Config } from '../types/config';
import * as networkLayer from './networkLayer';
import * as scheduler from './scheduler';
import * as location from './location';
import * as identity from './identity';
import * as device from './device';
vi.mock('./networkLayer', async () => {
const actual = await vi.importActual('./networkLayer');
return {
...actual,
createNetworkLayer: vi.fn().mockImplementation(() => {
return {
sendEvents: vi.fn(),
sendMetrics: vi.fn(),
};
}),
};
});
vi.mock('./scheduler', async () => {
const actual = await vi.importActual('./scheduler');
return {
...actual,
createScheduler: vi.fn(),
};
});
vi.mock('./location', async () => {
const actual = await vi.importActual('./location');
return {
...actual,
createLocation: vi.fn(),
};
});
vi.mock('./identity', async () => {
const actual = await vi.importActual('./identity');
return {
...actual,
createIdentity: vi.fn(),
};
});
vi.mock('./device', async () => {
const actual = await vi.importActual('./device');
return {
...actual,
createDevice: vi.fn(),
};
});
describe('storage', () => {
let config: Config;
beforeEach(() => {
config = {
platform: 'web',
projectName: 'test',
apiEndpoint: 'http://localhost:8080',
eventPath: '/event',
metricPath: '/metric',
onError: vi.fn(),
reset: vi.fn(),
};
});
describe('init', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('default init', () => {
init(config);
expect(networkLayer.createNetworkLayer).toHaveBeenCalled();
expect(scheduler.createScheduler).toHaveBeenCalled();
expect(identity.createIdentity).toHaveBeenCalled();
expect(device.createDevice).toHaveBeenCalled();
expect(location.createLocation).toHaveBeenCalled();
});
test('override init', () => {
const overrides = {
createNetworkLayer: vi.fn().mockImplementation(() => {
return {
sendEvents: vi.fn(),
sendMetrics: vi.fn(),
};
}),
createScheduler: vi.fn(),
createIdentity: vi.fn(),
createDevice: vi.fn(),
createLocation: vi.fn(),
};
init(config, overrides);
expect(overrides.createNetworkLayer).toHaveBeenCalled();
expect(overrides.createScheduler).toHaveBeenCalled();
expect(overrides.createIdentity).toHaveBeenCalled();
expect(overrides.createDevice).toHaveBeenCalled();
expect(overrides.createLocation).toHaveBeenCalled();
});
});
});