1
1
import {
2
+ act ,
2
3
act as componentAct ,
3
4
fireEvent ,
4
5
render ,
6
+ renderHook ,
5
7
screen ,
6
8
} from "@testing-library/react"
7
- import { act , renderHook } from "@testing-library/react"
8
9
import React , { FC , StrictMode , Suspense , useEffect , useState } from "react"
10
+ import { renderToPipeableStream } from "react-dom/server"
9
11
import {
10
12
defer ,
11
13
EMPTY ,
@@ -26,17 +28,16 @@ import {
26
28
startWith ,
27
29
switchMapTo ,
28
30
} from "rxjs/operators"
29
- import { describe , it , beforeAll , afterAll , expect , vi } from "vitest"
31
+ import { afterAll , beforeAll , describe , expect , it , vi } from "vitest"
30
32
import {
31
33
bind ,
32
34
sinkSuspense ,
33
35
Subscribe ,
34
36
SUSPENSE ,
35
37
useStateObservable ,
36
38
} from "../"
37
- import { TestErrorBoundary } from "../test-helpers/TestErrorBoundary"
38
- import { renderToPipeableStream } from "react-dom/server"
39
39
import { pipeableStreamToObservable } from "../test-helpers/pipeableStreamToObservable"
40
+ import { TestErrorBoundary } from "../test-helpers/TestErrorBoundary"
40
41
41
42
const wait = ( ms : number ) => new Promise ( ( res ) => setTimeout ( res , ms ) )
42
43
@@ -93,8 +94,13 @@ describe("connectObservable", () => {
93
94
94
95
await wait ( 110 )
95
96
96
- expect ( screen . queryByText ( "Result 1" ) ) . not . toBeNull ( )
97
- expect ( screen . queryByText ( "Waiting" ) ) . toBeNull ( )
97
+ vi . waitFor (
98
+ ( ) => {
99
+ expect ( screen . queryByText ( "Result 1" ) ) . not . toBeNull ( )
100
+ expect ( screen . queryByText ( "Waiting" ) ) . toBeNull ( )
101
+ } ,
102
+ { timeout : 2000 } ,
103
+ )
98
104
sub . unsubscribe ( )
99
105
} )
100
106
@@ -118,8 +124,13 @@ describe("connectObservable", () => {
118
124
119
125
await wait ( 110 )
120
126
121
- expect ( screen . queryByText ( "Result 1" ) ) . not . toBeNull ( )
122
- expect ( screen . queryByText ( "Waiting" ) ) . toBeNull ( )
127
+ vi . waitFor (
128
+ ( ) => {
129
+ expect ( screen . queryByText ( "Result 1" ) ) . not . toBeNull ( )
130
+ expect ( screen . queryByText ( "Waiting" ) ) . toBeNull ( )
131
+ } ,
132
+ { timeout : 2000 } ,
133
+ )
123
134
sub . unsubscribe ( )
124
135
} )
125
136
@@ -183,9 +194,7 @@ describe("connectObservable", () => {
183
194
const [ useNumber ] = bind ( numberStream , 1 )
184
195
const [ useString ] = bind ( stringStream , "a" )
185
196
186
- const BatchComponent : FC < {
187
- onUpdate : ( ) => void
188
- } > = ( { onUpdate } ) => {
197
+ const BatchComponent : FC < { onUpdate : ( ) => void } > = ( { onUpdate } ) => {
189
198
const number = useNumber ( )
190
199
const string = useString ( )
191
200
useEffect ( onUpdate )
@@ -329,8 +338,8 @@ describe("connectObservable", () => {
329
338
{ value === null
330
339
? "default"
331
340
: value instanceof Promise
332
- ? "promise"
333
- : "wtf?" }
341
+ ? "promise"
342
+ : "wtf?" }
334
343
</ div >
335
344
)
336
345
}
@@ -387,13 +396,17 @@ describe("connectObservable", () => {
387
396
388
397
await wait ( 10 )
389
398
390
- expect ( screen . queryByText ( "Waiting" ) ) . toBeNull ( )
391
- expect ( screen . queryByText ( "Result 1" ) ) . not . toBeNull ( )
399
+ vi . waitFor ( ( ) => {
400
+ expect ( screen . queryByText ( "Waiting" ) ) . toBeNull ( )
401
+ expect ( screen . queryByText ( "Result 1" ) ) . not . toBeNull ( )
402
+ } )
392
403
393
404
fireEvent . click ( screen . getByText ( / N e x t V a l / i) )
394
405
395
- expect ( screen . queryByText ( "Result 2" ) ) . not . toBeNull ( )
396
- expect ( screen . queryByText ( "Waiting" ) ) . toBeNull ( )
406
+ vi . waitFor ( ( ) => {
407
+ expect ( screen . queryByText ( "Result 2" ) ) . not . toBeNull ( )
408
+ expect ( screen . queryByText ( "Waiting" ) ) . toBeNull ( )
409
+ } )
397
410
398
411
fireEvent . click ( screen . getByText ( / N e x t K e y / i) )
399
412
@@ -405,17 +418,21 @@ describe("connectObservable", () => {
405
418
406
419
await wait ( 10 )
407
420
408
- expect ( screen . queryByText ( "Result 1" ) ) . not . toBeNull ( )
409
- expect ( screen . queryByText ( "Waiting" ) ) . toBeNull ( )
421
+ vi . waitFor ( ( ) => {
422
+ expect ( screen . queryByText ( "Result 1" ) ) . not . toBeNull ( )
423
+ expect ( screen . queryByText ( "Waiting" ) ) . toBeNull ( )
424
+ } )
410
425
411
426
fireEvent . click ( screen . getByText ( / N e x t V a l / i) )
412
427
413
- expect ( screen . queryByText ( "Result 2" ) ) . not . toBeNull ( )
414
- expect ( screen . queryByText ( "Waiting" ) ) . toBeNull ( )
428
+ vi . waitFor ( ( ) => {
429
+ expect ( screen . queryByText ( "Result 2" ) ) . not . toBeNull ( )
430
+ expect ( screen . queryByText ( "Waiting" ) ) . toBeNull ( )
431
+ } )
415
432
} )
416
433
417
434
it ( "allows errors to be caught in error boundaries" , ( ) => {
418
- const errStream = new Subject ( )
435
+ const errStream = new Subject < any > ( )
419
436
const [ useError ] = bind ( errStream , 1 )
420
437
421
438
const ErrorComponent = ( ) => {
@@ -441,7 +458,7 @@ describe("connectObservable", () => {
441
458
} )
442
459
443
460
it ( "allows sync errors to be caught in error boundaries with suspense, using source$" , ( ) => {
444
- const errStream = new Observable ( ( observer ) =>
461
+ const errStream = new Observable < any > ( ( observer ) =>
445
462
observer . error ( "controlled error" ) ,
446
463
)
447
464
const [ useError , errStream$ ] = bind ( errStream )
@@ -468,7 +485,7 @@ describe("connectObservable", () => {
468
485
} )
469
486
470
487
it ( "allows sync errors to be caught in error boundaries with suspense, without using source$" , ( ) => {
471
- const errStream = new Observable ( ( observer ) =>
488
+ const errStream = new Observable < any > ( ( observer ) =>
472
489
observer . error ( "controlled error" ) ,
473
490
)
474
491
const [ useError ] = bind ( errStream )
@@ -495,7 +512,7 @@ describe("connectObservable", () => {
495
512
} )
496
513
497
514
it ( "allows sync errors to be caught in error boundaries when there is a default value" , ( ) => {
498
- const errStream = new Observable ( ( observer ) =>
515
+ const errStream = new Observable < any > ( ( observer ) =>
499
516
observer . error ( "controlled error" ) ,
500
517
)
501
518
const [ useError , errStream$ ] = bind ( errStream , 0 )
@@ -522,7 +539,7 @@ describe("connectObservable", () => {
522
539
} )
523
540
524
541
it ( "allows async errors to be caught in error boundaries with suspense" , async ( ) => {
525
- const errStream = new Subject ( )
542
+ const errStream = new Subject < any > ( )
526
543
const [ useError , errStream$ ] = bind ( errStream )
527
544
const errStream$WithoutErrors = errStream$ . pipe ( catchError ( ( ) => NEVER ) )
528
545
@@ -702,7 +719,7 @@ describe("connectObservable", () => {
702
719
} )
703
720
704
721
it ( "should throw an error if the stream completes without emitting while on SUSPENSE" , async ( ) => {
705
- const subject = new Subject ( )
722
+ const subject = new Subject < any > ( )
706
723
const [ useValue , value$ ] = bind ( subject )
707
724
const errorCallback = vi . fn ( )
708
725
0 commit comments