-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathrender.tsx
165 lines (140 loc) · 4.83 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
import * as React from 'react';
import type {
ReactTestInstance,
ReactTestRenderer,
TestRendererOptions,
} from 'react-test-renderer';
import act from './act';
import { addToCleanupQueue } from './cleanup';
import { getConfig } from './config';
import { getHostSelves } from './helpers/component-tree';
import type { DebugOptions } from './helpers/debug';
import { debug } from './helpers/debug';
import { validateStringsRenderedWithinText } from './helpers/string-validation';
import { renderWithAct } from './render-act';
import { setRenderResult } from './screen';
import { getQueriesForElement } from './within';
export interface RenderOptions {
/**
* Pass a React Component as the wrapper option to have it rendered around the inner element. This is most useful for creating
* reusable custom render functions for common data providers.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
wrapper?: React.ComponentType<any>;
/**
* Set to `false` to disable concurrent rendering.
* Otherwise `render` will default to concurrent rendering.
*/
concurrentRoot?: boolean;
createNodeMock?: (element: React.ReactElement) => unknown;
unstable_validateStringsRenderedWithinText?: boolean;
}
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>, options: RenderOptions = {}) {
return renderInternal(component, options);
}
export function renderInternal<T>(component: React.ReactElement<T>, options?: RenderOptions) {
const {
wrapper: Wrapper,
concurrentRoot,
unstable_validateStringsRenderedWithinText,
...rest
} = options || {};
const testRendererOptions: TestRendererOptions = {
...rest,
// @ts-expect-error incomplete typing on RTR package
unstable_isConcurrent: concurrentRoot ?? getConfig().concurrentRoot,
};
if (unstable_validateStringsRenderedWithinText) {
return renderWithStringValidation(component, {
wrapper: Wrapper,
...testRendererOptions,
});
}
const wrap = (element: React.ReactElement) => (Wrapper ? <Wrapper>{element}</Wrapper> : element);
const renderer = renderWithAct(wrap(component), testRendererOptions);
return buildRenderResult(renderer, wrap);
}
function renderWithStringValidation<T>(
component: React.ReactElement<T>,
options: Omit<RenderOptions, 'unstable_validateStringsRenderedWithinText'> = {},
) {
const { wrapper: Wrapper, ...testRendererOptions } = options ?? {};
const wrap = (element: React.ReactElement) => (
<React.Profiler id="renderProfiler" onRender={handleRender}>
{Wrapper ? <Wrapper>{element}</Wrapper> : element}
</React.Profiler>
);
const handleRender: React.ProfilerOnRenderCallback = (_, phase) => {
if (renderer && phase === 'update') {
validateStringsRenderedWithinText(renderer.toJSON());
}
};
const renderer: ReactTestRenderer = renderWithAct(wrap(component), testRendererOptions);
validateStringsRenderedWithinText(renderer.toJSON());
return buildRenderResult(renderer, wrap);
}
function buildRenderResult(
renderer: ReactTestRenderer,
wrap: (element: React.ReactElement) => React.JSX.Element,
) {
const update = updateWithAct(renderer, wrap);
const instance = renderer.root;
const unmount = () => {
void act(() => {
renderer.unmount();
});
};
addToCleanupQueue(unmount);
const result = {
...getQueriesForElement(instance),
update,
unmount,
rerender: update, // alias for `update`
toJSON: renderer.toJSON,
debug: makeDebug(renderer),
get root(): ReactTestInstance {
return getHostSelves(instance)[0];
},
UNSAFE_root: instance,
};
// Add as non-enumerable property, so that it's safe to enumerate
// `render` result, e.g. using destructuring rest syntax.
Object.defineProperty(result, 'container', {
enumerable: false,
get() {
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 updateWithAct(
renderer: ReactTestRenderer,
wrap: (innerElement: React.ReactElement) => React.ReactElement,
) {
return function (component: React.ReactElement) {
void act(() => {
renderer.update(wrap(component));
});
};
}
export type DebugFunction = (options?: DebugOptions) => void;
function makeDebug(renderer: ReactTestRenderer): DebugFunction {
function debugImpl(options?: DebugOptions) {
const { defaultDebugOptions } = getConfig();
const debugOptions = { ...defaultDebugOptions, ...options };
const json = renderer.toJSON();
if (json) {
return debug(json, debugOptions);
}
}
return debugImpl;
}