Skip to content

Commit ba8d184

Browse files
rubennortefacebook-github-bot
authored andcommitted
Move render output tests to ReactNativeTester (#48140)
Summary: Pull Request resolved: #48140 Changelog: [internal] These tests test an API that's part of `ReactNativeTester` (will be renamed as `Fantom`) so it makes sense that they're in the same test file as the tests for the rest of the API. Reviewed By: javache Differential Revision: D66874226 fbshipit-source-id: f17e14c83cb5ca95ac619c5398c49ad84a27cfa5
1 parent b253b0f commit ba8d184

File tree

1 file changed

+205
-6
lines changed

1 file changed

+205
-6
lines changed

packages/react-native-fantom/src/__tests__/ReactNativeTester-itest.js

Lines changed: 205 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@
1010
*/
1111

1212
import 'react-native/Libraries/Core/InitializeCore';
13-
import * as ReactNativeTester from '..';
13+
import {createRoot, runTask} from '..';
14+
import * as React from 'react';
15+
import {Text, View} from 'react-native';
1416

1517
describe('ReactNativeTester', () => {
1618
describe('runTask', () => {
1719
it('should run a task synchronously', () => {
1820
const task = jest.fn();
1921

20-
ReactNativeTester.runTask(task);
22+
runTask(task);
2123

2224
expect(task).toHaveBeenCalledTimes(1);
2325
});
2426

2527
// TODO: fix error handling and make this pass
2628
it.skip('should re-throw errors from the task synchronously', () => {
2729
expect(() => {
28-
ReactNativeTester.runTask(() => {
30+
runTask(() => {
2931
throw new Error('test error');
3032
});
3133
}).toThrow('test error');
@@ -34,7 +36,7 @@ describe('ReactNativeTester', () => {
3436
it('should exhaust the microtask queue synchronously', () => {
3537
const lastMicrotask = jest.fn();
3638

37-
ReactNativeTester.runTask(() => {
39+
runTask(() => {
3840
queueMicrotask(() => {
3941
queueMicrotask(() => {
4042
queueMicrotask(() => {
@@ -50,7 +52,7 @@ describe('ReactNativeTester', () => {
5052
// TODO: fix error handling and make this pass
5153
it.skip('should re-throw errors from microtasks synchronously', () => {
5254
expect(() => {
53-
ReactNativeTester.runTask(() => {
55+
runTask(() => {
5456
queueMicrotask(() => {
5557
throw new Error('test error');
5658
});
@@ -61,12 +63,209 @@ describe('ReactNativeTester', () => {
6163
it('should run async tasks synchronously', () => {
6264
let completed = false;
6365

64-
ReactNativeTester.runTask(async () => {
66+
runTask(async () => {
6567
await Promise.resolve(6);
6668
completed = true;
6769
});
6870

6971
expect(completed).toBe(true);
7072
});
7173
});
74+
75+
describe('getRenderedOutput', () => {
76+
describe('toJSX', () => {
77+
it('default config', () => {
78+
const root = createRoot();
79+
80+
runTask(() => {
81+
root.render(
82+
<View style={{width: 100, height: 100}} collapsable={false} />,
83+
);
84+
});
85+
86+
expect(root.getRenderedOutput().toJSX()).toEqual(
87+
<rn-view height="100.000000" width="100.000000" />,
88+
);
89+
90+
root.destroy();
91+
});
92+
93+
it('default config, list of children', () => {
94+
const root = createRoot();
95+
96+
runTask(() => {
97+
root.render(
98+
<>
99+
<View style={{width: 100, height: 100}} collapsable={false} />
100+
<View style={{width: 100, height: 100}} collapsable={false} />
101+
</>,
102+
);
103+
});
104+
105+
expect(root.getRenderedOutput().toJSX()).toEqual(
106+
<>
107+
<rn-view width="100.000000" height="100.000000" />
108+
<rn-view width="100.000000" height="100.000000" />
109+
</>,
110+
);
111+
112+
root.destroy();
113+
});
114+
115+
it('include root', () => {
116+
const root = createRoot();
117+
118+
runTask(() => {
119+
root.render(
120+
<View style={{width: 100, height: 100}} collapsable={false} />,
121+
);
122+
});
123+
124+
expect(root.getRenderedOutput({includeRoot: true}).toJSX()).toEqual(
125+
<rn-rootView>
126+
<rn-view width="100.000000" height="100.000000" />
127+
</rn-rootView>,
128+
);
129+
130+
root.destroy();
131+
});
132+
133+
it('include layout metrics', () => {
134+
const root = createRoot();
135+
136+
runTask(() => {
137+
root.render(
138+
<View style={{width: 100, height: 100}} collapsable={false} />,
139+
);
140+
});
141+
142+
expect(
143+
root.getRenderedOutput({includeLayoutMetrics: true}).toJSX(),
144+
).toEqual(
145+
<rn-view
146+
height="100.000000"
147+
layoutMetrics-borderWidth="{top:0,right:0,bottom:0,left:0}"
148+
layoutMetrics-contentInsets="{top:0,right:0,bottom:0,left:0}"
149+
layoutMetrics-displayType="Flex"
150+
layoutMetrics-frame="{x:0,y:0,width:100,height:100}"
151+
layoutMetrics-layoutDirection="LeftToRight"
152+
layoutMetrics-overflowInset="{top:0,right:-0,bottom:-0,left:0}"
153+
layoutMetrics-pointScaleFactor="1"
154+
width="100.000000"
155+
/>,
156+
);
157+
158+
root.destroy();
159+
});
160+
161+
it('take props', () => {
162+
const root = createRoot();
163+
164+
runTask(() => {
165+
root.render(
166+
<View style={{width: 100, height: 100}} collapsable={false} />,
167+
);
168+
});
169+
170+
expect(
171+
root
172+
.getRenderedOutput({
173+
props: ['width'],
174+
})
175+
.toJSX(),
176+
).toEqual(<rn-view width="100.000000" />);
177+
178+
root.destroy();
179+
});
180+
181+
it('skip props', () => {
182+
const root = createRoot();
183+
184+
runTask(() => {
185+
root.render(
186+
<View style={{width: 100, height: 100}} collapsable={false} />,
187+
);
188+
});
189+
190+
expect(
191+
root
192+
.getRenderedOutput({
193+
props: ['!width'],
194+
})
195+
.toJSX(),
196+
).toEqual(<rn-view height="100.000000" />);
197+
198+
root.destroy();
199+
});
200+
201+
it('filter out all props', () => {
202+
const root = createRoot();
203+
204+
runTask(() => {
205+
root.render(
206+
<>
207+
<View style={{width: 100, height: 100}} collapsable={false} />
208+
<Text>hello world!</Text>
209+
<View style={{width: 200, height: 300}} collapsable={false} />
210+
</>,
211+
);
212+
});
213+
214+
expect(root.getRenderedOutput({props: []}).toJSX()).toEqual(
215+
<>
216+
<rn-view />
217+
<rn-paragraph>hello world!</rn-paragraph>
218+
<rn-view />
219+
</>,
220+
);
221+
222+
root.destroy();
223+
});
224+
});
225+
226+
describe('toJSON', () => {
227+
it('nested text', () => {
228+
const root = createRoot();
229+
230+
runTask(() => {
231+
root.render(
232+
<Text>
233+
Testing native{' '}
234+
<Text style={{color: 'red'}}>
235+
JSX is <Text style={{color: 'blue'}}>easy!</Text>
236+
</Text>
237+
</Text>,
238+
);
239+
});
240+
241+
expect(
242+
root.getRenderedOutput({props: ['foreground*']}).toJSON(),
243+
).toEqual({
244+
children: [
245+
'Testing native ',
246+
{
247+
children: 'JSX is ',
248+
props: {
249+
foregroundColor: 'rgba(255, 0, 0, 255)',
250+
},
251+
type: 'Text',
252+
},
253+
{
254+
children: 'easy!',
255+
props: {
256+
foregroundColor: 'rgba(0, 0, 255, 255)',
257+
},
258+
type: 'Text',
259+
},
260+
],
261+
props: {
262+
foregroundColor: 'rgba(255, 255, 255, 127)',
263+
},
264+
type: 'Paragraph',
265+
});
266+
267+
root.destroy();
268+
});
269+
});
270+
});
72271
});

0 commit comments

Comments
 (0)