-
Notifications
You must be signed in to change notification settings - Fork 429
/
Copy pathsnapshot-helpers.jsx
44 lines (38 loc) · 1.21 KB
/
snapshot-helpers.jsx
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
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import jsBeautify from 'js-beautify';
import renderer from 'react-test-renderer';
import Settings from './settings';
/*
* Render React components to DOM state as a String
*
* Please note, Component is the non-JSX component object.
*/
const renderDOM = (Component, props) =>
renderer.create(React.createElement(Component, props), null).toJSON();
/*
* Render React components to markup in order to compare to a Jest Snapshot.
*
* Please note, Component is the non-JSX component object.
*/
const renderMarkup = (Component, props) =>
String(
jsBeautify.html(
ReactDOMServer.renderToStaticMarkup(
React.createElement(Component, props)
),
Settings.jsBeautify
)
);
const testDOMandHTML = ({ name, test, Component, props }) => {
test(`${name} DOM Snapshot`, () => {
expect(renderDOM(Component, props)).toMatchSnapshot();
});
test(`${name} HTML Snapshot`, () => {
expect(renderMarkup(Component, props)).toMatchSnapshot();
});
};
// eslint-disable-line import/prefer-default-export
// eslint-disable-line import/prefer-default-export
// eslint-disable-line import/prefer-default-export
export { renderDOM, renderMarkup, testDOMandHTML };