1
- import React , { Component , ErrorInfo , useLayoutEffect } from "react"
1
+ import React , { Component , ErrorInfo , useEffect } from "react"
2
2
import { Observable , from , throwError } from "rxjs"
3
3
import { delay , startWith } from "rxjs/operators"
4
- import { bind } from "@react-rxjs/core"
4
+ import { bind , Subscribe } from "@react-rxjs/core"
5
5
import { batchUpdates } from "./"
6
6
import { render , screen } from "@testing-library/react"
7
7
8
8
const wait = ( ms : number ) => new Promise ( ( res ) => setTimeout ( res , ms ) )
9
9
10
- const [ useLatestNumber ] = bind ( ( id : string , batched : boolean ) =>
10
+ const [ useLatestNumber , latestNumber$ ] = bind ( ( id : string , batched : boolean ) =>
11
11
( id === "error"
12
12
? throwError ( "controlled error" )
13
13
: from ( [ 1 , 2 , 3 , 4 , 5 ] )
@@ -54,13 +54,13 @@ interface Props {
54
54
}
55
55
const Grandson : React . FC < Props > = ( { onRender, batched, id } ) => {
56
56
const latestNumber = useLatestNumber ( id , batched )
57
- useLayoutEffect ( onRender )
57
+ useEffect ( onRender )
58
58
return < div > Grandson { latestNumber } </ div >
59
59
}
60
60
61
61
const Son : React . FC < Props > = ( props ) => {
62
62
const latestNumber = useLatestNumber ( props . id , props . batched )
63
- useLayoutEffect ( props . onRender )
63
+ useEffect ( props . onRender )
64
64
return (
65
65
< div >
66
66
Son { latestNumber }
@@ -71,7 +71,7 @@ const Son: React.FC<Props> = (props) => {
71
71
72
72
const Father : React . FC < Props > = ( props ) => {
73
73
const latestNumber = useLatestNumber ( props . id , props . batched )
74
- useLayoutEffect ( props . onRender )
74
+ useEffect ( props . onRender )
75
75
return (
76
76
< div >
77
77
Father { latestNumber }
@@ -97,7 +97,11 @@ describe("batchUpdates", () => {
97
97
98
98
test ( "it triggers nested updates when batchUpdates is not used" , async ( ) => {
99
99
const mockFn = jest . fn ( )
100
- render ( < Father id = "noBatching" batched = { false } onRender = { mockFn } /> )
100
+ render (
101
+ < Subscribe source$ = { latestNumber$ ( "noBatching" , false ) } >
102
+ < Father id = "noBatching" batched = { false } onRender = { mockFn } />
103
+ </ Subscribe > ,
104
+ )
101
105
expect ( screen . queryByText ( "Father 0" ) ) . not . toBeNull ( )
102
106
expect ( screen . queryByText ( "Son 0" ) ) . not . toBeNull ( )
103
107
expect ( screen . queryByText ( "Grandson 0" ) ) . not . toBeNull ( )
@@ -113,7 +117,12 @@ describe("batchUpdates", () => {
113
117
114
118
test ( "batchUpdates prevents unnecessary updates" , async ( ) => {
115
119
const mockFn = jest . fn ( )
116
- render ( < Father id = "batchingAndComplete" batched = { true } onRender = { mockFn } /> )
120
+ render (
121
+ < Subscribe source$ = { latestNumber$ ( "batchingAndComplete" , true ) } >
122
+ < Father id = "batchingAndComplete" batched onRender = { mockFn } />
123
+ </ Subscribe > ,
124
+ )
125
+
117
126
expect ( screen . queryByText ( "Father 0" ) ) . not . toBeNull ( )
118
127
expect ( screen . queryByText ( "Son 0" ) ) . not . toBeNull ( )
119
128
expect ( screen . queryByText ( "Grandson 0" ) ) . not . toBeNull ( )
0 commit comments