|
1 | 1 | ## Writing Tests |
2 | 2 |
|
3 | | -Testing with `react-md` should not much more difficult than any other component |
4 | | -testing you are used to for the majority of your tests. The only times you might |
5 | | -encounter errors are when you use components that rely one one of the |
6 | | -configuration providers and determining the current app size. Since I am the |
7 | | -most familiar with [jest] as the test runner and [react-testing-library] as the |
8 | | -test renderer, this guide will be targeted towards these two libraries |
| 3 | +Testing an app with `react-md` components should not require many changes to |
| 4 | +your normal testing flow. The only times weird issues might occur are when using |
| 5 | +components that rely on one of the providers included by the [Configuration |
| 6 | +component]. |
| 7 | + |
| 8 | +This guide will provide a few suggestions for setting up an app with [jest] and |
| 9 | +[react-testing-library]. |
9 | 10 |
|
10 | 11 | ### Initializing window.matchMedia |
11 | 12 |
|
12 | | -If one of your components or a component from `react-md` uses the `useAppSize` |
13 | | -or `useMediaQuery` hooks, your tests might fail due to `window.matchMedia` being |
| 13 | +If a component from your app or `react-md` uses the `useAppSize` or |
| 14 | +`useMediaQuery` hooks, your tests might fail due to `window.matchMedia` being |
14 | 15 | `undefined`. In this case, you'll want to create or update a `testSetup.ts` (or |
15 | 16 | `testSetup.js`) to include a very simple polyfill. |
16 | 17 |
|
17 | 18 | Edit `testSetup.ts`: |
18 | 19 |
|
19 | 20 | ```diff |
20 | | -+const { |
| 21 | ++import { |
21 | 22 | + DEFAULT_DESKTOP_MIN_WIDTH, |
22 | 23 | + DEFAULT_DESKTOP_LARGE_MIN_WIDTH, |
23 | | -+} = require('@react-md/utils/lib/sizing/constants'); |
| 24 | ++} from '@react-md/utils'; |
24 | 25 | + |
25 | 26 | +if (typeof window.matchMedia !== 'function') { |
26 | 27 | + window.matchMedia = (query) => ({ |
@@ -49,7 +50,7 @@ Edit `jest.config.js`: |
49 | 50 | } |
50 | 51 | ``` |
51 | 52 |
|
52 | | -This default polyfill will make all your tests run in desktop mode by default. |
| 53 | +This polyfill will make all your tests run in desktop mode by default. |
53 | 54 |
|
54 | 55 | ### Testing different app sizes |
55 | 56 |
|
@@ -140,3 +141,4 @@ Edit `src/components/__tests__/Component.tsx`: |
140 | 141 | https://testing-library.com/docs/react-testing-library/intro |
141 | 142 | [custom renderer]: |
142 | 143 | https://testing-library.com/docs/react-testing-library/setup#custom-render |
| 144 | +[configuration component]: /guides/configuring-your-app |
0 commit comments