forked from callstack/react-native-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.js
36 lines (33 loc) · 1.1 KB
/
render.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
// @flow
import * as React from 'react';
import TestRenderer from 'react-test-renderer'; // eslint-disable-line import/no-extraneous-dependencies
import { getByAPI } from './helpers/getByAPI';
import { queryByAPI } from './helpers/queryByAPI';
import debugShallow from './helpers/debugShallow';
import debugDeep from './helpers/debugDeep';
/**
* Renders test component deeply using react-test-renderer and exposes helpers
* to assert on the output.
*/
export default function render(
component: React.Element<any>,
options?: { createNodeMock: (element: React.Element<any>) => any }
) {
const renderer = TestRenderer.create(component, options);
const instance = renderer.root;
return {
...getByAPI(instance),
...queryByAPI(instance),
update: renderer.update,
unmount: renderer.unmount,
toJSON: renderer.toJSON,
debug: debug(instance, renderer),
};
}
function debug(instance: ReactTestInstance, renderer) {
function debugImpl(message?: string) {
return debugDeep(renderer.toJSON(), message);
}
debugImpl.shallow = message => debugShallow(instance, message);
return debugImpl;
}