@@ -2,6 +2,9 @@ let React;
2
2
let Suspense ;
3
3
let ReactTestRenderer ;
4
4
let ReactFeatureFlags ;
5
+ let Random ;
6
+
7
+ const SEED = 0 ;
5
8
6
9
const prettyFormatPkg = require ( 'pretty-format' ) ;
7
10
@@ -24,6 +27,7 @@ describe('ReactSuspenseFuzz', () => {
24
27
React = require ( 'react' ) ;
25
28
Suspense = React . Suspense ;
26
29
ReactTestRenderer = require ( 'react-test-renderer' ) ;
30
+ Random = require ( 'random-seed' ) ;
27
31
} ) ;
28
32
29
33
function createFuzzer ( ) {
@@ -171,29 +175,32 @@ describe('ReactSuspenseFuzz', () => {
171
175
const expectedOutput = renderToRoot ( expectedRoot , children , {
172
176
shouldSuspend : false ,
173
177
} ) ;
178
+ expectedRoot . unmount ( ) ;
174
179
175
180
resetCache ( ) ;
176
181
const syncRoot = ReactTestRenderer . create ( null ) ;
177
182
const syncOutput = renderToRoot ( syncRoot , children ) ;
178
183
expect ( syncOutput ) . toEqual ( expectedOutput ) ;
184
+ syncRoot . unmount ( ) ;
179
185
180
186
resetCache ( ) ;
181
187
const concurrentRoot = ReactTestRenderer . create ( null , {
182
188
unstable_isConcurrent : true ,
183
189
} ) ;
184
190
const concurrentOutput = renderToRoot ( concurrentRoot , children ) ;
185
191
expect ( concurrentOutput ) . toEqual ( expectedOutput ) ;
192
+ concurrentRoot . unmount ( ) ;
193
+ concurrentRoot . unstable_flushAll ( ) ;
186
194
187
195
ReactTestRenderer . unstable_clearYields ( ) ;
188
196
}
189
197
190
- function pickRandomWeighted ( options ) {
198
+ function pickRandomWeighted ( rand , options ) {
191
199
let totalWeight = 0 ;
192
200
for ( let i = 0 ; i < options . length ; i ++ ) {
193
201
totalWeight += options [ i ] . weight ;
194
202
}
195
- const randomNumber = Math . random ( ) * totalWeight ;
196
- let remainingWeight = randomNumber ;
203
+ let remainingWeight = rand . floatBetween ( 0 , totalWeight ) ;
197
204
for ( let i = 0 ; i < options . length ; i ++ ) {
198
205
const { value, weight} = options [ i ] ;
199
206
remainingWeight -= weight ;
@@ -203,13 +210,7 @@ describe('ReactSuspenseFuzz', () => {
203
210
}
204
211
}
205
212
206
- function randomInteger ( min , max ) {
207
- min = Math . ceil ( min ) ;
208
- max = Math . floor ( max ) ;
209
- return Math . floor ( Math . random ( ) * ( max - min ) ) + min ;
210
- }
211
-
212
- function generateTestCase ( numberOfElements ) {
213
+ function generateTestCase ( rand , numberOfElements ) {
213
214
let remainingElements = numberOfElements ;
214
215
215
216
function createRandomChild ( hasSibling ) {
@@ -223,13 +224,13 @@ describe('ReactSuspenseFuzz', () => {
223
224
possibleActions . push ( { value : 'suspense' , weight : 1 } ) ;
224
225
}
225
226
226
- const action = pickRandomWeighted ( possibleActions ) ;
227
+ const action = pickRandomWeighted ( rand , possibleActions ) ;
227
228
228
229
switch ( action ) {
229
230
case 'text' : {
230
231
remainingElements -- ;
231
232
232
- const numberOfUpdates = pickRandomWeighted ( [
233
+ const numberOfUpdates = pickRandomWeighted ( rand , [
233
234
{ value : 0 , weight : 8 } ,
234
235
{ value : 1 , weight : 4 } ,
235
236
{ value : 2 , weight : 1 } ,
@@ -238,21 +239,21 @@ describe('ReactSuspenseFuzz', () => {
238
239
let updates = [ ] ;
239
240
for ( let i = 0 ; i < numberOfUpdates ; i ++ ) {
240
241
updates . push ( {
241
- beginAfter : randomInteger ( 0 , 10000 ) ,
242
- suspendFor : randomInteger ( 0 , 10000 ) ,
242
+ beginAfter : rand . intBetween ( 0 , 10000 ) ,
243
+ suspendFor : rand . intBetween ( 0 , 10000 ) ,
243
244
} ) ;
244
245
}
245
246
246
247
return (
247
248
< Text
248
249
text = { ( remainingElements + 9 ) . toString ( 36 ) . toUpperCase ( ) }
249
- initialDelay = { randomInteger ( 0 , 10000 ) }
250
+ initialDelay = { rand . intBetween ( 0 , 10000 ) }
250
251
updates = { updates }
251
252
/>
252
253
) ;
253
254
}
254
255
case 'container' : {
255
- const numberOfUpdates = pickRandomWeighted ( [
256
+ const numberOfUpdates = pickRandomWeighted ( rand , [
256
257
{ value : 0 , weight : 8 } ,
257
258
{ value : 1 , weight : 4 } ,
258
259
{ value : 2 , weight : 1 } ,
@@ -261,7 +262,7 @@ describe('ReactSuspenseFuzz', () => {
261
262
let updates = [ ] ;
262
263
for ( let i = 0 ; i < numberOfUpdates ; i ++ ) {
263
264
updates . push ( {
264
- remountAfter : randomInteger ( 0 , 10000 ) ,
265
+ remountAfter : rand . intBetween ( 0 , 10000 ) ,
265
266
} ) ;
266
267
}
267
268
@@ -273,12 +274,12 @@ describe('ReactSuspenseFuzz', () => {
273
274
remainingElements -- ;
274
275
const children = createRandomChildren ( 3 ) ;
275
276
276
- const maxDuration = pickRandomWeighted ( [
277
+ const maxDuration = pickRandomWeighted ( rand , [
277
278
{ value : undefined , weight : 1 } ,
278
- { value : randomInteger ( 0 , 5000 ) , weight : 1 } ,
279
+ { value : rand . intBetween ( 0 , 5000 ) , weight : 1 } ,
279
280
] ) ;
280
281
281
- const fallbackType = pickRandomWeighted ( [
282
+ const fallbackType = pickRandomWeighted ( rand , [
282
283
{ value : 'none' , weight : 1 } ,
283
284
{ value : 'normal' , weight : 1 } ,
284
285
{ value : 'nested suspense' , weight : 1 } ,
@@ -361,11 +362,13 @@ describe('ReactSuspenseFuzz', () => {
361
362
it ( 'generative tests' , ( ) => {
362
363
const { generateTestCase, testResolvedOutput} = createFuzzer ( ) ;
363
364
365
+ const rand = Random . create ( SEED ) ;
366
+
364
367
const NUMBER_OF_TEST_CASES = 500 ;
365
- const ELEMENTS_PER_CASE = 8 ;
368
+ const ELEMENTS_PER_CASE = 12 ;
366
369
367
370
for ( let i = 0 ; i < NUMBER_OF_TEST_CASES ; i ++ ) {
368
- const randomTestCase = generateTestCase ( ELEMENTS_PER_CASE ) ;
371
+ const randomTestCase = generateTestCase ( rand , ELEMENTS_PER_CASE ) ;
369
372
try {
370
373
testResolvedOutput ( randomTestCase ) ;
371
374
} catch ( e ) {
0 commit comments