1 | # Jest Utils
|
2 | Collection of helpers to be used in Jest tests code.
|
3 |
|
4 | **Why?** — We use Jest for unit testing of our code. Some common tests
|
5 | demand too much boilerplate code. To reduce amount of this boilerplate code,
|
6 | we provide collection of auxiliary functions that will help to speed-up writing
|
7 | of the tests, and ensure that everybody does everything in the same manner
|
8 | across our projects.
|
9 |
|
10 | **IMPORTANT:** This module is intended only for use inside Jest tests!
|
11 | Its code depends on Jest environment and development dependencies!
|
12 |
|
13 | This module provides the following functions:
|
14 |
|
15 | - **`findInDomByClass(dom, className)`** — Just an auxiliary alias for
|
16 | [`findRenderedDOMComponentWithClass(..)`](https://reactjs.org/docs/test-utils.html#findrendereddomcomponentwithclass)
|
17 | from `react-dom/test-utils`;
|
18 |
|
19 | - **`render(component)`** — Renders provided ReactJS component into
|
20 | JSON representation of the component tree, using
|
21 | [`react-test-renderer`](https://www.npmjs.com/package/react-test-renderer),
|
22 | e.g.:
|
23 |
|
24 | ```jsx
|
25 | import { JU } from 'topcoder-react-utils';
|
26 |
|
27 | console.log(JU.render(<div>Example</div>));
|
28 | ```
|
29 |
|
30 | - **`renderDom(component)`** — Renders given ReactJS component into DOM,
|
31 | using `react-dom/test-utils`. In many cases you will want to render and find
|
32 | a rendered node in the resulting tree, you can do it like this:
|
33 | ```jsx
|
34 | import React from 'react';
|
35 | import { JU } from 'topcoder-react-utils/jest-utils';
|
36 |
|
37 | const dom = JU.renderDom(
|
38 | <div>
|
39 | Example component, containing a button you want to find in the render.
|
40 | <button className="BUTTON">Click me!</button>
|
41 | </div>
|
42 | );
|
43 |
|
44 | const button = JU.findInDomByClass(dom, 'BUTTON');
|
45 | ```
|
46 |
|
47 | - **`shallowRender(component)`** — Generates a shallow render of
|
48 | the provided ReactJS component, using
|
49 | [react-test-renderer/shallow](https://reactjs.org/docs/shallow-renderer.html)
|
50 | and returns the result.
|
51 |
|
52 | ```jsx
|
53 | import { JU } from 'topcoder-react-utils/jest-utils';
|
54 |
|
55 | console.log(JU.shallowRender(<div>Example</div>));
|
56 | ```
|
57 |
|
58 | - **`shallowSnapshot(component)`** — Makes a shallow snapshot test of
|
59 | the given ReactJS component, and also returns JSON representation of
|
60 | the rendered component tree. Under the hood it uses `shallowRender(component)`
|
61 | to generate the render, then executes `expect(RENDER_RESULT).toMatchSnapshot()`,
|
62 | and finally returns the `RENDER_RESULT` to the caller.
|
63 |
|
64 | ```jsx
|
65 | // Sample Jest test.
|
66 |
|
67 | import { JU } from 'topcoder-react-utils/jest-utils';
|
68 |
|
69 | test('A snapshot test', () => {
|
70 | console.log(JU.shallowSnapshot(<div>Example</div>));
|
71 | });
|
72 | ```
|
73 |
|
74 | - **`simulate`** — Just an alias for the
|
75 | [`Simulate`](https://reactjs.org/docs/test-utils.html#simulate) from
|
76 | `react-dom/test-utils`.
|
77 |
|
78 | - **`snapshot(component)`** — Makes snapshot test of the given ReactJS
|
79 | component, and also returns JSON representation of the rendered component
|
80 | tree. Under the hood, it uses `render(component)` to render it, then executes
|
81 | `expect(RENDER_RESULT).toMatchSnapshot()`, and then returns the `RENDER_RESULT`.
|
82 |
|
83 | ```jsx
|
84 | // Sample Jest test.
|
85 |
|
86 | import { JU } from 'topcoder-react-utils/jest-utils';
|
87 |
|
88 | test('A snapshot test', () => {
|
89 | console.log(JU.snapshot(<div>Example</div>));
|
90 | });
|
91 | ```
|
92 |
|
\ | No newline at end of file |