-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-utils.tsx
85 lines (80 loc) · 2.31 KB
/
test-utils.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
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
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable import/export */
import type { RenderOptions, RenderResult } from "@testing-library/react";
import { render as baseRender } from "@testing-library/react";
import type { ReactElement } from "react";
import { Fragment } from "react";
import {
DEFAULT_DESKTOP_LARGE_MIN_WIDTH,
DEFAULT_DESKTOP_MIN_WIDTH,
DEFAULT_PHONE_MAX_WIDTH,
DEFAULT_TABLET_MAX_WIDTH,
} from "react-md";
import { Providers } from "./components/Providers";
export * from "@testing-library/react";
export * from "@testing-library/user-event";
/**
* An updated render function that wraps the tests in the required providers.
*/
export function render(
ui: ReactElement,
{ wrapper: ProvidedWrapper = Fragment, ...options }: RenderOptions = {}
): RenderResult {
return baseRender(ui, {
...options,
wrapper: function TestWrapper({ children }) {
return (
<Providers>
<ProvidedWrapper>{children}</ProvidedWrapper>
</Providers>
);
},
});
}
/**
* A util to mock the window.matchMedia to a specific app size.
*
* @example
* Simple Example
* ```tsx
* it("should render correctly on mobile", () => {
* const media = mockMedia("phone"):
* const { container } = render(<MyComponent />);
* expect(container).toMatchSnapshot();
*
* media.mockRestore();
* });
* ```
*
* @param size - The app size to enforce
* @returns the matchMedia mock that **must** have `.mockRestore()` called at
* the end of the test
*/
export function mockMedia(
size: "phone" | "tablet" | "desktop"
): jest.SpyInstance<MediaQueryList, [query: string]> {
let regexp: RegExp;
switch (size) {
case "phone":
regexp = new RegExp(`max-width: ${DEFAULT_PHONE_MAX_WIDTH}`);
break;
case "tablet":
regexp = new RegExp(`max-width: ${DEFAULT_TABLET_MAX_WIDTH}`);
break;
case "desktop":
regexp = new RegExp(
`min-width: (${DEFAULT_DESKTOP_MIN_WIDTH}|${DEFAULT_DESKTOP_LARGE_MIN_WIDTH})`
);
break;
}
return jest.spyOn(window, "matchMedia").mockImplementation((query) => ({
media: query,
matches: regexp.test(query),
onchange: () => {},
addListener: () => {},
removeListener: () => {},
addEventListener: () => {},
removeEventListener: () => {},
dispatchEvent: () => false,
}));
}