MCPcopy
hub / github.com/callstack/react-native-testing-library / render

Function render

src/render.tsx:36–110  ·  view source on GitHub ↗
(element: React.ReactElement<T>, options: RenderOptions = {})

Source from the content-addressed store, hash-verified

34 * to assert on the output.
35 */
36export async function render<T>(element: React.ReactElement<T>, options: RenderOptions = {}) {
37 const { wrapper: Wrapper, ...rest } = options || {};
38 validateOptions('render', rest, render);
39
40 const rendererOptions: RootOptions = {
41 textComponentTypes: HOST_TEXT_NAMES,
42 publicTextComponentTypes: ['Text'],
43 transformHiddenInstanceProps: ({ props }) => ({
44 ...props,
45 style: withHiddenStyle(props.style as StyleProp<StyleLike>),
46 }),
47 };
48
49 const wrap = (element: React.ReactElement) => (Wrapper ? <Wrapper>{element}</Wrapper> : element);
50 const renderer = createRoot(rendererOptions);
51
52 await act(() => {
53 renderer.render(wrap(element));
54 });
55
56 const container = renderer.container;
57
58 const rerender = async (component: React.ReactElement) => {
59 await act(() => {
60 renderer.render(wrap(component));
61 });
62 };
63
64 const unmount = async () => {
65 await act(() => {
66 renderer.unmount();
67 });
68 };
69
70 const toJSON = (): JsonElement | null => {
71 const json = renderer.container.toJSON();
72 if (json?.children.length === 0) {
73 return null;
74 }
75
76 if (json?.children.length === 1 && typeof json.children[0] !== 'string') {
77 return json.children[0];
78 }
79
80 return json;
81 };
82
83 addToCleanupQueue(unmount);
84
85 const result = {
86 ...getQueriesForInstance(renderer.container),
87 rerender,
88 unmount,
89 toJSON,
90 debug: makeDebug(renderer),
91 get container(): TestInstance {
92 return renderer.container;
93 },

Callers 15

renderHookFunction · 0.90
setupComponentFunction · 0.50
input.tsxFile · 0.50
setupComponentFunction · 0.50
expected.tsxFile · 0.50
input.tsxFile · 0.50
expected.tsxFile · 0.50
input.tsxFile · 0.50
expected.tsxFile · 0.50
input.tsxFile · 0.50
expected.tsxFile · 0.50
input.tsxFile · 0.50

Calls 8

validateOptionsFunction · 0.90
actFunction · 0.90
addToCleanupQueueFunction · 0.90
setRenderResultFunction · 0.90
withHiddenStyleFunction · 0.85
wrapFunction · 0.85
makeDebugFunction · 0.85
renderMethod · 0.45

Tested by 2

renderWithAtomsFunction · 0.40
renderWithProvidersFunction · 0.40

Used in the wild real call sites across dependent graphs

searching dependent graphs…