1
1
/* global describe, it */
2
2
3
- import React from 'react' ;
4
- import ReactDOM from 'react-dom' ;
3
+ import React , { act } from 'react' ;
4
+ import { createRoot } from 'react-dom/client ' ;
5
5
import { ParallaxController } from 'parallax-controller' ;
6
6
7
7
import { render } from '@testing-library/react' ;
@@ -18,16 +18,15 @@ describe('A <ParallaxProvider>', () => {
18
18
return < div /> ;
19
19
} ;
20
20
21
- const render = ( ) => {
22
- ReactDOM . render (
21
+ const root = createRoot ( node ) ;
22
+
23
+ act ( ( ) => {
24
+ root . render (
23
25
< ParallaxProvider >
24
26
< Child />
25
- </ ParallaxProvider > ,
26
- node
27
+ </ ParallaxProvider >
27
28
) ;
28
- } ;
29
-
30
- render ( ) ;
29
+ } ) ;
31
30
32
31
expect ( child ) . toHaveBeenCalled ( ) ;
33
32
} ) ;
@@ -155,32 +154,46 @@ describe('A <ParallaxProvider>', () => {
155
154
const node1 = document . createElement ( 'div' ) ;
156
155
const node2 = document . createElement ( 'div' ) ;
157
156
158
- const render = ( node : HTMLDivElement ) => {
159
- let instance : ParallaxController | null = null ;
160
- const GetInstance = ( ) => {
161
- instance = useParallaxController ( ) ;
162
- return null ;
163
- } ;
164
- ReactDOM . render (
165
- // @ts -ignore
166
- < ParallaxProvider >
167
- < GetInstance />
168
- </ ParallaxProvider > ,
169
- node
170
- ) ;
171
- return instance ;
157
+ // Use a different approach - capture instances after rendering
158
+ let instance1 : ParallaxController | null = null ;
159
+ let instance2 : ParallaxController | null = null ;
160
+
161
+ const GetInstance1 = ( ) => {
162
+ instance1 = useParallaxController ( ) ;
163
+ return null ;
164
+ } ;
165
+
166
+ const GetInstance2 = ( ) => {
167
+ instance2 = useParallaxController ( ) ;
168
+ return null ;
172
169
} ;
173
170
174
171
// first instance mounted
175
- const instance1 = render ( node1 ) ;
172
+ const root1 = createRoot ( node1 ) ;
173
+ act ( ( ) => {
174
+ root1 . render (
175
+ // @ts -ignore
176
+ < ParallaxProvider >
177
+ < GetInstance1 />
178
+ </ ParallaxProvider >
179
+ ) ;
180
+ } ) ;
176
181
expect ( instance1 ) . toBeInstanceOf ( ParallaxController ) ;
177
182
178
183
// second instance mounted
179
- const instance2 = render ( node2 ) ;
184
+ const root2 = createRoot ( node2 ) ;
185
+ act ( ( ) => {
186
+ root2 . render (
187
+ // @ts -ignore
188
+ < ParallaxProvider >
189
+ < GetInstance2 />
190
+ </ ParallaxProvider >
191
+ ) ;
192
+ } ) ;
180
193
expect ( instance2 ) . toBeInstanceOf ( ParallaxController ) ;
181
194
182
195
// unmount first instance
183
- ReactDOM . unmountComponentAtNode ( node1 ) ;
196
+ root1 . unmount ( ) ;
184
197
185
198
// this must still be defined
186
199
expect ( instance2 ) . toBeInstanceOf ( ParallaxController ) ;
0 commit comments