Repository navigation
Expand file tree
/
Copy pathjest-setup.ts
More file actions
98 lines (82 loc) · 2.39 KB
/
Copy pathjest-setup.ts
File metadata and controls
98 lines (82 loc) · 2.39 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
import '@testing-library/jest-dom';
import { subtle } from 'node:crypto';
import { TextDecoder, TextEncoder } from 'node:util';
Object.defineProperties(global, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
});
enableSVGElementMocks();
Object.defineProperty(window, 'fetch', {
writable: true,
value: jest.fn(),
});
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // Deprecated
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
Object.defineProperty(window, 'ResizeObserver', {
writable: true,
value: jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
})),
});
jest
.spyOn(global, 'crypto', 'get')
.mockImplementation(() => ({ getRandomValues: () => [12345678], subtle }) as unknown as Crypto);
jest.spyOn(console, 'warn').mockImplementation((...args) => {
if (
args[0].toString().includes('[mobx-react-lite] importing batchingForReactDom is no longer needed') ||
args[0].toString().includes('NODE_ENV is not defined')
) {
return;
}
console.log(...args);
});
const fetchMock = jest.spyOn(window, 'fetch');
beforeEach(() => {
fetchMock.mockResolvedValue(null as unknown as Response);
});
Element.prototype.scrollIntoView = jest.fn();
function enableSVGElementMocks() {
/**
* Mocking the following SVG methods to avoid errors when running tests
*
* Taken from the following comment:
* https://github.com/apexcharts/react-apexcharts/issues/52#issuecomment-844757362
*/
Object.defineProperty(global.SVGElement.prototype, 'getScreenCTM', {
writable: true,
value: jest.fn(),
});
Object.defineProperty(global.SVGElement.prototype, 'getBBox', {
writable: true,
value: jest.fn().mockReturnValue({
x: 0,
y: 0,
}),
});
Object.defineProperty(global.SVGElement.prototype, 'getComputedTextLength', {
writable: true,
value: jest.fn().mockReturnValue(0),
});
Object.defineProperty(global.SVGElement.prototype, 'createSVGMatrix', {
writable: true,
value: jest.fn().mockReturnValue({
x: 10,
y: 10,
inverse: () => {},
multiply: () => {},
}),
});
}