-
Notifications
You must be signed in to change notification settings - Fork 10
/
setup-test-env.tsx
29 lines (25 loc) · 1016 Bytes
/
setup-test-env.tsx
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
import '@testing-library/jest-dom';
import './src/test-utils/fail-test-on-console-error';
const getMediaQuerySize = (mediaFeature: string, mediaQuery: string, defaultSize: number): number => {
const regExp = new RegExp(`\\(${mediaFeature}: (\\d+)px\\)`);
const [, size = defaultSize] = mediaQuery.match(regExp) || [];
return +size;
};
window.matchMedia =
window.matchMedia ||
jest.fn((query) => {
const minWidth = getMediaQuerySize('min-width', query, 0);
const maxWidth = getMediaQuerySize('max-width', query, Infinity);
const matches = query.includes('(prefers-color-scheme: dark)')
? false
: global.window.innerWidth >= minWidth && global.window.innerWidth <= maxWidth;
return {
matches,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
media: query,
onchange: null,
};
});