-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest.setup.ts
70 lines (57 loc) · 1.45 KB
/
jest.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
import "@testing-library/jest-dom";
import { configure } from "@testing-library/dom";
class LocalStorageMock {
store: Record<string, string>;
constructor() {
this.store = {};
}
clear() {
this.store = {};
}
getItem(key: string) {
return this.store[key] || null;
}
setItem(key: string, value: string) {
this.store[key] = value;
}
removeItem(key: string) {
delete this.store[key];
}
}
Object.defineProperty(window, "localStorage", {
value: new LocalStorageMock(),
});
Object.defineProperty(window, "IntersectionObserver", {
writable: true,
value: jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
})),
});
Object.defineProperty(window, "matchMedia", {
writable: true,
value: jest.fn().mockImplementation((query: string) => ({
matches: matchingMediaQueries.includes(query),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
})),
});
window.HTMLElement.prototype.scrollIntoView = jest.fn();
const matchingMediaQueries: string[] = [];
Object.defineProperty(window, "ResizeObserver", {
writable: true,
value: jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
})),
});
configure({
getElementError: (message) => {
const error = new Error(message ?? undefined);
error.name = "TestingLibraryElementError";
error.stack = undefined;
return error;
},
});