-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
jest-global-mocks.ts
45 lines (39 loc) · 1.32 KB
/
jest-global-mocks.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
const mock = () => {
let storage: any = {};
return {
getItem: (key: any) => key in storage ? storage[key] : null,
setItem: (key: any, value: any) => storage[key] = value || '',
removeItem: (key: any) => delete storage[key],
clear: () => storage = {},
};
};
window.HTMLElement.prototype.scrollIntoView = () => { };
Object.defineProperty(window, 'localStorage', { value: mock() });
Object.defineProperty(window, 'sessionStorage', { value: mock() });
Object.defineProperty(window, 'getComputedStyle', {
value: () => ['-webkit-appearance']
});
Object.defineProperty(window, '__env', { value: { env: { backendUrl: 'mocked URL' } } });
Object.defineProperty(window, 'getComputedStyle', {
value: () => ({
getPropertyValue: () => {
return '';
}
})
});
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
// Jest has a hard time with MomentJS because they export as default, to bypass this problem we can mock the require .default
jest.mock('moment-tiny', () => {
const actual = jest.requireActual('moment-tiny');
return { __esModule: true, ...actual, default: actual };
});