This repository has been archived by the owner on Aug 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reactShallowStrictEqual.js
67 lines (61 loc) · 1.86 KB
/
reactShallowStrictEqual.js
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
/* @flow */
const { strictEqual } = require('assert')
const ShallowRenderer = require('react-test-renderer/shallow')
const reactElementToJSXString = require('react-element-to-jsx-string')
/**
* Elements are the smallest building blocks of React apps.
*
* @external ReactElement
* @see {@link https://reactjs.org/docs/rendering-elements.html|ReactElement}
*/
/**
* a unit testing helper for React components
*
* performs a **shallow** render of `actual` and compares the result to
* `expected`
*
* uses the native Node.js
* {@link https://nodejs.org/api/assert.html#assert_assert_strictequal_actual_expected_message|assert.strictEqual}
* to perform the assertion
*
* @param {ReactElement} actual
* @param {ReactElement} expected
* @param {string} [message]
* @throws {AssertionError} test failure
* @example
* // MyButton.js
* import classNames from 'classnames'
*
* export default ({children, className, ...props}) => (
* <button className={classNames('my-button', className)} {...props}>{children}</button>
* )
* @example
* // MyButton.test.js
* import reactShallowStrictEqual from 'secondwheel/reactShallowStrictEqual'
* import MyButton from './MyButton'
*
* describe('MyButton', () => {
* it('should render predictable result', () => {
* reactShallowStrictEqual(
* (<MyButton className="test-class" id="test-id">test children</MyButton>),
* (<button className="my-button test-class" id="test-id">test children</button>)
* )
* })
* })
*/
const reactShallowStrictEqual = (
actual/*: any */,
expected/*: any */,
message/*:: ?:string */
) => {
const renderer = new ShallowRenderer()
renderer.render(actual)
strictEqual(
/* @flowignore */
reactElementToJSXString(renderer.getRenderOutput()),
/* @flowignore */
reactElementToJSXString(expected),
message
)
}
module.exports = reactShallowStrictEqual