-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvitest.setup.ts
106 lines (93 loc) · 2.7 KB
/
vitest.setup.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
import { getPasswordProtectedProfileId } from './src/tests/fixtures';
import { bootEnvironmentWithProfileFixtures } from './src/tests/helpers';
import { env } from './src/tests/mocks';
import { MockInstance, afterAll, afterEach, beforeAll, beforeEach } from 'vitest';
import { vi } from 'vitest';
import * as ProfileMock from './src/lib/context/Profile';
process.env.REACT_APP_IS_UNIT = '1';
vi.mock('webextension-polyfill', () => {
const browser = {
runtime: {
getPlatformInfo: vi.fn().mockReturnValue({ os: 'mac' }),
onMessage: {
addListener: vi.fn(),
},
onInstalled: {
addListener: vi.fn(),
},
onConnect: {
addListener: vi.fn(),
},
onStartup: {
addListener: vi.fn(),
},
onSuspend: {
addListener: vi.fn(),
},
getURL: vi.fn(),
sendMessage: vi.fn().mockImplementation((args) => {
if (args.type === 'CHECK_LOCK') {
return Promise.resolve({ isLocked: false });
}
return Promise.resolve({});
}),
},
};
return {
default: browser,
storage: {
local: {
get: async () => ({
localStorageKey: {
hasOnboarded: true,
},
}),
},
},
};
});
// Mock the environment module
vi.mock('./src/lib/utils/environment', () => ({
initializeEnvironment: () => env,
}));
vi.mock('./src/routing', () => ({
navigationRoutes: [],
default: [],
}));
vi.mock('./src/lib/utils/localStorage', () => ({
KEY: 'localStorageKey',
getLocalValues: () => ({
autoLockTimer: 60,
}),
setLocalValue: vi.fn(),
clearLocalStorage: vi.fn(),
AutoLockTimer: {
FIFTEEN_MINUTES: 15,
ONE_HOUR: 60,
TWELVE_HOURS: 720,
TWENTY_FOUR_HOURS: 1440,
},
}));
beforeAll(async () => {
await bootEnvironmentWithProfileFixtures({ env, shouldRestoreDefaultProfile: true });
});
let useProfileContextMock: MockInstance;
beforeEach(() => {
const profile = env.profiles().findById(getPasswordProtectedProfileId());
useProfileContextMock = vi.spyOn(ProfileMock, 'useProfileContext').mockReturnValue({
profile,
initProfile: vi.fn(),
convertedBalance: 0,
importProfile: vi.fn(),
isProfileReady: true,
});
});
afterEach(() => {
useProfileContextMock.mockRestore();
});
afterAll(() => {
// Run garbage collector after each test file is finished.
if (global.gc) {
global.gc();
}
});