-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathrender.tsx
190 lines (164 loc) · 5.2 KB
/
render.tsx
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import TestRenderer from 'react-test-renderer';
import type { ReactTestInstance, ReactTestRenderer } from 'react-test-renderer';
import * as React from 'react';
import { Profiler } from 'react';
import act from './act';
import { addToCleanupQueue } from './cleanup';
import debugShallow from './helpers/debugShallow';
import debugDeep, { DebugOptions } from './helpers/debugDeep';
import { getQueriesForElement } from './within';
import { setRenderResult, screen } from './screen';
import { validateStringsRenderedWithinText } from './helpers/stringValidation';
import { getConfig } from './config';
import { getHostChildren } from './helpers/component-tree';
import { configureHostComponentNamesIfNeeded } from './helpers/host-component-names';
export type RenderOptions = {
wrapper?: React.ComponentType<any>;
createNodeMock?: (element: React.ReactElement) => any;
unstable_validateStringsRenderedWithinText?: boolean;
};
type TestRendererOptions = {
createNodeMock: (element: React.ReactElement) => any;
};
export type RenderResult = ReturnType<typeof render>;
/**
* Renders test component deeply using react-test-renderer and exposes helpers
* to assert on the output.
*/
export default function render<T>(
component: React.ReactElement<T>,
{
wrapper: Wrapper,
createNodeMock,
unstable_validateStringsRenderedWithinText,
}: RenderOptions = {}
) {
configureHostComponentNamesIfNeeded();
if (unstable_validateStringsRenderedWithinText) {
return renderWithStringValidation(component, {
wrapper: Wrapper,
createNodeMock,
});
}
const wrap = (element: React.ReactElement) =>
Wrapper ? <Wrapper>{element}</Wrapper> : element;
const renderer = renderWithAct(
wrap(component),
createNodeMock ? { createNodeMock } : undefined
);
return buildRenderResult(renderer, wrap);
}
function renderWithStringValidation<T>(
component: React.ReactElement<T>,
{
wrapper: Wrapper,
createNodeMock,
}: Omit<RenderOptions, 'unstable_validateStringsRenderedWithinText'> = {}
) {
const handleRender: React.ProfilerProps['onRender'] = (_, phase) => {
if (phase === 'update') {
validateStringsRenderedWithinText(screen.toJSON());
}
};
const wrap = (element: React.ReactElement) => (
<Profiler id="renderProfiler" onRender={handleRender}>
{Wrapper ? <Wrapper>{element}</Wrapper> : element}
</Profiler>
);
const renderer = renderWithAct(
wrap(component),
createNodeMock ? { createNodeMock } : undefined
);
validateStringsRenderedWithinText(renderer.toJSON());
return buildRenderResult(renderer, wrap);
}
function buildRenderResult(
renderer: ReactTestRenderer,
wrap: (element: React.ReactElement) => JSX.Element
) {
const update = updateWithAct(renderer, wrap);
const instance = renderer.root;
const unmount = () => {
act(() => {
renderer.unmount();
});
};
addToCleanupQueue(unmount);
const result = {
...getQueriesForElement(instance),
update,
unmount,
rerender: update, // alias for `update`
toJSON: renderer.toJSON,
debug: debug(instance, renderer),
get root() {
return getHostChildren(instance)[0];
},
UNSAFE_root: instance,
get container() {
if (!getConfig().useBreakingChanges) {
// eslint-disable-next-line no-console
console.warn(
"'container' property is deprecated and has been renamed to 'UNSAFE_root'.\n\n" +
"Consider using 'root' property which returns root host element."
);
return instance;
}
throw new Error(
"'container' property has been renamed to 'UNSAFE_root'.\n\n" +
"Consider using 'root' property which returns root host element."
);
},
};
setRenderResult(result);
return result;
}
function renderWithAct(
component: React.ReactElement,
options?: TestRendererOptions
): ReactTestRenderer {
let renderer: ReactTestRenderer;
act(() => {
renderer = TestRenderer.create(component, options);
});
// @ts-ignore act is sync, so renderer is always initialised here
return renderer;
}
function updateWithAct(
renderer: ReactTestRenderer,
wrap: (innerElement: React.ReactElement) => React.ReactElement
) {
return function (component: React.ReactElement) {
act(() => {
renderer.update(wrap(component));
});
};
}
interface DebugFunction {
(options?: DebugOptions | string): void;
shallow: (message?: string) => void;
}
function debug(
instance: ReactTestInstance,
renderer: ReactTestRenderer
): DebugFunction {
function debugImpl(options?: DebugOptions | string) {
const { defaultDebugOptions } = getConfig();
const debugOptions =
typeof options === 'string'
? { ...defaultDebugOptions, message: options }
: { ...defaultDebugOptions, ...options };
if (typeof options === 'string') {
// eslint-disable-next-line no-console
console.warn(
'Using debug("message") is deprecated and will be removed in future release, please use debug({ message; "message" }) instead.'
);
}
const json = renderer.toJSON();
if (json) {
return debugDeep(json, debugOptions);
}
}
debugImpl.shallow = (message?: string) => debugShallow(instance, message);
return debugImpl;
}