-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathcore.test.ts
More file actions
102 lines (89 loc) · 3.1 KB
/
core.test.ts
File metadata and controls
102 lines (89 loc) · 3.1 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
import { describe, expect, test, mock } from 'bun:test';
// In Bun, top-level imports are cached.
// We can use mock.module to change the implementation of a module,
// but if a module has already been executed (like core.ts),
// re-importing it might not re-run the top-level code unless we use some tricks
// or run tests in isolation.
// Actually, bun test runs each file in its own environment usually,
// BUT if we run multiple test files in one process, they might share the cache.
describe('core info parsing', () => {
test('should call error when currentVersionInfo is invalid JSON', async () => {
const mockError = mock(() => {});
mock.module('react-native', () => ({
Platform: {
OS: 'ios',
Version: 13,
},
NativeModules: {
Pushy: {
currentVersionInfo: '{invalid}',
downloadRootDir: '/tmp',
packageVersion: '1.0.0',
currentVersion: 'hash1',
isFirstTime: false,
rolledBackVersion: '',
buildTime: '2023-01-01',
uuid: 'existing-uuid',
setLocalHashInfo: mock(() => {}),
getLocalHashInfo: mock(() => Promise.resolve('{}')),
setUuid: mock(() => {}),
},
},
NativeEventEmitter: class {
addListener = mock(() => ({ remove: mock(() => {}) }));
},
}));
mock.module('react-native/Libraries/Core/ReactNativeVersion', () => ({
version: { major: 0, minor: 73, patch: 0 },
}));
mock.module('nanoid/non-secure', () => ({
nanoid: () => 'mock-uuid',
}));
mock.module('../utils', () => ({
error: mockError,
log: mock(() => {}),
emptyModule: {},
}));
// Use a unique query parameter to bypass cache if supported, or just rely on fresh environment per file.
// In Bun, you can sometimes use a cache buster if it's dynamic import.
await import('../core?error');
expect(mockError).toHaveBeenCalledWith(
expect.stringContaining('error_parse_version_info')
);
});
test('should not call error when currentVersionInfo is valid JSON', async () => {
const mockError = mock(() => {});
const mockSetLocalHashInfo = mock(() => {});
mock.module('react-native', () => ({
Platform: {
OS: 'ios',
Version: 13,
},
NativeModules: {
Pushy: {
currentVersionInfo: JSON.stringify({ name: 'v1', debugChannel: true }),
downloadRootDir: '/tmp',
packageVersion: '1.0.0',
currentVersion: 'hash1',
isFirstTime: false,
rolledBackVersion: '',
buildTime: '2023-01-01',
uuid: 'existing-uuid',
setLocalHashInfo: mockSetLocalHashInfo,
getLocalHashInfo: mock(() => Promise.resolve('{}')),
setUuid: mock(() => {}),
},
},
NativeEventEmitter: class {
addListener = mock(() => ({ remove: mock(() => {}) }));
},
}));
mock.module('../utils', () => ({
error: mockError,
log: mock(() => {}),
emptyModule: {},
}));
await import('../core?success');
expect(mockError).not.toHaveBeenCalled();
});
});