|
1 | 1 | // @flow |
2 | | -import * as React from 'react'; |
3 | | -import { isValidElementType } from 'react-is'; |
4 | | -import TestRenderer from 'react-test-renderer'; // eslint-disable-line import/no-extraneous-dependencies |
5 | | -import ShallowRenderer from 'react-test-renderer/shallow'; // eslint-disable-line import/no-extraneous-dependencies |
6 | | -import prettyFormat, { plugins } from 'pretty-format'; // eslint-disable-line import/no-extraneous-dependencies |
7 | | - |
8 | | -const getNodeByName = (node, name) => |
9 | | - node.type.name === name || |
10 | | - node.type.displayName === name || |
11 | | - node.type === name; |
12 | | - |
13 | | -const getNodeByText = (node, text) => |
14 | | - (getNodeByName(node, 'Text') || getNodeByName(node, 'TextInput')) && |
15 | | - (typeof text === 'string' |
16 | | - ? text === node.props.children |
17 | | - : text.test(node.props.children)); |
18 | | - |
19 | | -/** |
20 | | - * Wait for microtasks queue to flush |
21 | | - */ |
22 | | -export const flushMicrotasksQueue = (): Promise<any> => |
23 | | - new Promise(resolve => setImmediate(resolve)); |
24 | | - |
25 | | -/** |
26 | | - * Renders test component deeply using react-test-renderer and exposes helpers |
27 | | - * to assert on the output. |
28 | | - */ |
29 | | -export const render = ( |
30 | | - component: React.Element<*>, |
31 | | - options?: { createNodeMock: (element: React.Element<*>) => any } |
32 | | -) => { |
33 | | - const renderer = TestRenderer.create(component, options); |
34 | | - const instance = renderer.root; |
35 | | - |
36 | | - const getByName = (name: string | React.Element<*>) => { |
37 | | - try { |
38 | | - return instance.find(node => getNodeByName(node, name)); |
39 | | - } catch (error) { |
40 | | - throw new ErrorWithStack(`Error: Component not found.`, getByName); |
41 | | - } |
42 | | - }; |
43 | | - |
44 | | - const getByText = (text: string | RegExp) => { |
45 | | - try { |
46 | | - return instance.find(node => getNodeByText(node, text)); |
47 | | - } catch (error) { |
48 | | - throw new ErrorWithStack(`Error: Component not found.`, getByText); |
49 | | - } |
50 | | - }; |
51 | | - |
52 | | - const getByProps = (props: { [propName: string]: any }) => { |
53 | | - try { |
54 | | - return instance.findByProps(props); |
55 | | - } catch (error) { |
56 | | - throw new ErrorWithStack(`Error: Component not found.`, getByProps); |
57 | | - } |
58 | | - }; |
59 | | - |
60 | | - return { |
61 | | - getByTestId: (testID: string) => instance.findByProps({ testID }), |
62 | | - getByName, |
63 | | - getAllByName: (name: string | React.Element<*>) => |
64 | | - instance.findAll(node => getNodeByName(node, name)), |
65 | | - getByText, |
66 | | - getAllByText: (text: string | RegExp) => |
67 | | - instance.findAll(node => getNodeByText(node, text)), |
68 | | - getByProps, |
69 | | - getAllByProps: (props: { [propName: string]: any }) => |
70 | | - instance.findAllByProps(props), |
71 | | - update: renderer.update, |
72 | | - unmount: renderer.unmount, |
73 | | - }; |
74 | | -}; |
75 | | - |
76 | | -/** |
77 | | - * Renders test component shallowly using react-test-renderer/shallow |
78 | | - */ |
79 | | -export const shallow = (instance: ReactTestInstance | React.Element<*>) => { |
80 | | - const renderer = new ShallowRenderer(); |
81 | | - if (isValidElementType(instance)) { |
82 | | - // $FlowFixMe - instance is React.Element<*> in this branch |
83 | | - renderer.render(instance); |
84 | | - } else { |
85 | | - renderer.render(React.createElement(instance.type, instance.props)); |
86 | | - } |
87 | | - const output = renderer.getRenderOutput(); |
88 | | - |
89 | | - return { |
90 | | - output, |
91 | | - }; |
92 | | -}; |
93 | | - |
94 | | -/** |
95 | | - * Log pretty-printed shallow test component instance |
96 | | - */ |
97 | | -export const debug = ( |
98 | | - instance: ReactTestInstance | React.Element<*>, |
99 | | - message?: any |
100 | | -) => { |
101 | | - const { output } = shallow(instance); |
102 | | - // eslint-disable-next-line no-console |
103 | | - console.log(format(output), message || ''); |
104 | | -}; |
105 | | - |
106 | | -const format = input => |
107 | | - prettyFormat(input, { |
108 | | - plugins: [plugins.ReactTestComponent, plugins.ReactElement], |
109 | | - }); |
110 | | - |
111 | | -class ErrorWithStack extends Error { |
112 | | - constructor(message: ?string, callsite: Function) { |
113 | | - super(message); |
114 | | - if (Error.captureStackTrace) { |
115 | | - Error.captureStackTrace(this, callsite); |
116 | | - } |
117 | | - } |
118 | | -} |
| 2 | +import render from './render'; |
| 3 | +import shallow from './shallow'; |
| 4 | +import flushMicrotasksQueue from './flushMicrotasksQueue'; |
| 5 | +import debug from './debug'; |
| 6 | + |
| 7 | +export { render }; |
| 8 | +export { shallow }; |
| 9 | +export { flushMicrotasksQueue }; |
| 10 | +export { debug }; |
0 commit comments