-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.ts
More file actions
62 lines (56 loc) · 1.69 KB
/
Copy pathjest.setup.ts
File metadata and controls
62 lines (56 loc) · 1.69 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
import "@testing-library/jest-dom";
// jsdom doesn't implement scrollIntoView — polyfill it
if (typeof window !== "undefined") {
if (!window.HTMLElement.prototype.scrollIntoView) {
window.HTMLElement.prototype.scrollIntoView = jest.fn();
}
if (!window.Element.prototype.scrollIntoView) {
window.Element.prototype.scrollIntoView = jest.fn();
}
// jsdom doesn't implement matchMedia
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
}
// jsdom doesn't implement fetch globally in older node versions, or Request/Response
if (typeof global.Request === 'undefined') {
global.Request = class Request {
url: string;
method: string;
headers: any;
body: any;
constructor(input: string, init?: any) {
this.url = input;
this.method = init?.method || 'GET';
this.headers = init?.headers || {};
this.body = init?.body || null;
}
json() { return Promise.resolve(JSON.parse(this.body)); }
} as any;
}
if (typeof global.Response === 'undefined') {
global.Response = class Response {
body: any;
status: number;
constructor(body?: any, init?: any) {
this.body = body;
this.status = init?.status || 200;
}
json() { return Promise.resolve(JSON.parse(this.body)); }
} as any;
}
if (!global.fetch) {
global.fetch = jest.fn(() =>
Promise.resolve({ ok: true, json: () => Promise.resolve({}) })
) as any;
}