11import * as React from 'react'
2- import { suspend } from 'suspend-react'
3- import { vi , it , expect } from 'vitest'
4- import { act , render , createPortal , type HostContainer } from './index'
2+ import { it , expect } from 'vitest'
3+ import { render , createPortal , type HostContainer } from './index'
54
5+ // Let React know that we'll be testing effectful components
66declare global {
77 var IS_REACT_ACT_ENVIRONMENT : boolean
88}
9-
10- // Let React know that we'll be testing effectful components
11- global . IS_REACT_ACT_ENVIRONMENT = true
12-
13- // Mock scheduler to test React features
14- vi . mock ( 'scheduler' , ( ) => require ( 'scheduler/unstable_mock' ) )
9+ globalThis . IS_REACT_ACT_ENVIRONMENT = true
1510
1611interface ReactProps < T > {
1712 key ?: React . Key
1813 ref ?: React . Ref < T >
1914 children ?: React . ReactNode
2015}
2116
22- declare global {
17+ declare module 'react' {
2318 namespace JSX {
2419 interface IntrinsicElements {
2520 element : ReactProps < null > & Record < string , unknown >
@@ -32,13 +27,13 @@ it('should go through lifecycle', async () => {
3227
3328 function Test ( ) {
3429 lifecycle . push ( 'render' )
35- React . useImperativeHandle ( React . useRef ( ) , ( ) => void lifecycle . push ( 'ref' ) )
30+ React . useImperativeHandle ( React . useRef ( undefined ) , ( ) => void lifecycle . push ( 'ref' ) )
3631 React . useInsertionEffect ( ( ) => void lifecycle . push ( 'useInsertionEffect' ) , [ ] )
3732 React . useLayoutEffect ( ( ) => void lifecycle . push ( 'useLayoutEffect' ) , [ ] )
3833 React . useEffect ( ( ) => void lifecycle . push ( 'useEffect' ) , [ ] )
3934 return null
4035 }
41- const container : HostContainer = await act ( async ( ) => render ( < Test /> ) )
36+ const container : HostContainer = await React . act ( async ( ) => render ( < Test /> ) )
4237
4338 expect ( lifecycle ) . toStrictEqual ( [ 'render' , 'useInsertionEffect' , 'ref' , 'useLayoutEffect' , 'useEffect' ] )
4439 expect ( container . head ) . toBe ( null )
@@ -48,19 +43,19 @@ it('should render JSX', async () => {
4843 let container ! : HostContainer
4944
5045 // Mount
51- await act ( async ( ) => ( container = render ( < element key = { 1 } foo /> ) ) )
46+ await React . act ( async ( ) => ( container = render ( < element key = { 1 } foo /> ) ) )
5247 expect ( container . head ) . toStrictEqual ( { type : 'element' , props : { foo : true } , children : [ ] } )
5348
5449 // Remount
55- await act ( async ( ) => ( container = render ( < element bar /> ) ) )
50+ await React . act ( async ( ) => ( container = render ( < element bar /> ) ) )
5651 expect ( container . head ) . toStrictEqual ( { type : 'element' , props : { bar : true } , children : [ ] } )
5752
5853 // Mutate
59- await act ( async ( ) => ( container = render ( < element foo /> ) ) )
54+ await React . act ( async ( ) => ( container = render ( < element foo /> ) ) )
6055 expect ( container . head ) . toStrictEqual ( { type : 'element' , props : { foo : true } , children : [ ] } )
6156
6257 // Child mount
63- await act ( async ( ) => {
58+ await React . act ( async ( ) => {
6459 container = render (
6560 < element foo >
6661 < element />
@@ -74,21 +69,22 @@ it('should render JSX', async () => {
7469 } )
7570
7671 // Child unmount
77- await act ( async ( ) => ( container = render ( < element foo /> ) ) )
72+ await React . act ( async ( ) => ( container = render ( < element foo /> ) ) )
7873 expect ( container . head ) . toStrictEqual ( { type : 'element' , props : { foo : true } , children : [ ] } )
7974
8075 // Unmount
81- await act ( async ( ) => ( container = render ( < > </ > ) ) )
76+ await React . act ( async ( ) => ( container = render ( < > </ > ) ) )
8277 expect ( container . head ) . toBe ( null )
8378
8479 // Suspense
85- const Test = ( ) => ( suspend ( async ( ) => null , [ ] ) , ( < element bar /> ) )
86- await act ( async ( ) => ( container = render ( < Test /> ) ) )
80+ const promise = Promise . resolve ( null )
81+ const Test = ( ) => ( React . use ( promise ) , ( < element bar /> ) )
82+ await React . act ( async ( ) => ( container = render ( < Test /> ) ) )
8783 expect ( container . head ) . toStrictEqual ( { type : 'element' , props : { bar : true } , children : [ ] } )
8884
8985 // Portals
9086 const portalContainer : HostContainer = { head : null }
91- await act ( async ( ) => ( container = render ( createPortal ( < element /> , portalContainer ) ) ) )
87+ await React . act ( async ( ) => ( container = render ( createPortal ( < element /> , portalContainer ) ) ) )
9288 expect ( container . head ) . toBe ( null )
9389 expect ( portalContainer . head ) . toStrictEqual ( { type : 'element' , props : { } , children : [ ] } )
9490} )
@@ -97,29 +93,30 @@ it('should render text', async () => {
9793 let container ! : HostContainer
9894
9995 // Mount
100- await act ( async ( ) => ( container = render ( < > one</ > ) ) )
96+ await React . act ( async ( ) => ( container = render ( < > one</ > ) ) )
10197 expect ( container . head ) . toStrictEqual ( { type : 'text' , props : { value : 'one' } , children : [ ] } )
10298
10399 // Remount
104- await act ( async ( ) => ( container = render ( < > one</ > ) ) )
100+ await React . act ( async ( ) => ( container = render ( < > one</ > ) ) )
105101 expect ( container . head ) . toStrictEqual ( { type : 'text' , props : { value : 'one' } , children : [ ] } )
106102
107103 // Mutate
108- await act ( async ( ) => ( container = render ( < > two</ > ) ) )
104+ await React . act ( async ( ) => ( container = render ( < > two</ > ) ) )
109105 expect ( container . head ) . toStrictEqual ( { type : 'text' , props : { value : 'two' } , children : [ ] } )
110106
111107 // Unmount
112- await act ( async ( ) => ( container = render ( < > </ > ) ) )
108+ await React . act ( async ( ) => ( container = render ( < > </ > ) ) )
113109 expect ( container . head ) . toBe ( null )
114110
115111 // Suspense
116- const Test = ( ) => ( suspend ( async ( ) => null , [ ] ) , ( < > three</ > ) )
117- await act ( async ( ) => ( container = render ( < Test /> ) ) )
112+ const promise = Promise . resolve ( null )
113+ const Test = ( ) => ( React . use ( promise ) , ( < > three</ > ) )
114+ await React . act ( async ( ) => ( container = render ( < Test /> ) ) )
118115 expect ( container . head ) . toStrictEqual ( { type : 'text' , props : { value : 'three' } , children : [ ] } )
119116
120117 // Portals
121118 const portalContainer : HostContainer = { head : null }
122- await act ( async ( ) => ( container = render ( createPortal ( 'four' , portalContainer ) ) ) )
119+ await React . act ( async ( ) => ( container = render ( createPortal ( 'four' , portalContainer ) ) ) )
123120 expect ( container . head ) . toBe ( null )
124121 expect ( portalContainer . head ) . toStrictEqual ( { type : 'text' , props : { value : 'four' } , children : [ ] } )
125122} )
0 commit comments