10
10
*/
11
11
12
12
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' ;
14
16
15
17
describe ( 'ReactNativeTester' , ( ) => {
16
18
describe ( 'runTask' , ( ) => {
17
19
it ( 'should run a task synchronously' , ( ) => {
18
20
const task = jest . fn ( ) ;
19
21
20
- ReactNativeTester . runTask ( task ) ;
22
+ runTask ( task ) ;
21
23
22
24
expect ( task ) . toHaveBeenCalledTimes ( 1 ) ;
23
25
} ) ;
24
26
25
27
// TODO: fix error handling and make this pass
26
28
it . skip ( 'should re-throw errors from the task synchronously' , ( ) => {
27
29
expect ( ( ) => {
28
- ReactNativeTester . runTask ( ( ) => {
30
+ runTask ( ( ) => {
29
31
throw new Error ( 'test error' ) ;
30
32
} ) ;
31
33
} ) . toThrow ( 'test error' ) ;
@@ -34,7 +36,7 @@ describe('ReactNativeTester', () => {
34
36
it ( 'should exhaust the microtask queue synchronously' , ( ) => {
35
37
const lastMicrotask = jest . fn ( ) ;
36
38
37
- ReactNativeTester . runTask ( ( ) => {
39
+ runTask ( ( ) => {
38
40
queueMicrotask ( ( ) => {
39
41
queueMicrotask ( ( ) => {
40
42
queueMicrotask ( ( ) => {
@@ -50,7 +52,7 @@ describe('ReactNativeTester', () => {
50
52
// TODO: fix error handling and make this pass
51
53
it . skip ( 'should re-throw errors from microtasks synchronously' , ( ) => {
52
54
expect ( ( ) => {
53
- ReactNativeTester . runTask ( ( ) => {
55
+ runTask ( ( ) => {
54
56
queueMicrotask ( ( ) => {
55
57
throw new Error ( 'test error' ) ;
56
58
} ) ;
@@ -61,12 +63,209 @@ describe('ReactNativeTester', () => {
61
63
it ( 'should run async tasks synchronously' , ( ) => {
62
64
let completed = false ;
63
65
64
- ReactNativeTester . runTask ( async ( ) => {
66
+ runTask ( async ( ) => {
65
67
await Promise . resolve ( 6 ) ;
66
68
completed = true ;
67
69
} ) ;
68
70
69
71
expect ( completed ) . toBe ( true ) ;
70
72
} ) ;
71
73
} ) ;
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
+ } ) ;
72
271
} ) ;
0 commit comments