11/// <reference types="react/canary" />
22
33import ReactExports , { StrictMode , Suspense } from 'react'
4- import { act , fireEvent , render , screen , waitFor } from '@testing-library/react'
5- import { expect , it } from 'vitest'
4+ import { act , fireEvent , render , screen } from '@testing-library/react'
5+ import { afterEach , beforeEach , expect , it , vi } from 'vitest'
66import { proxy , useSnapshot } from 'valtio'
77
8+ beforeEach ( ( ) => {
9+ vi . useFakeTimers ( )
10+ } )
11+
12+ afterEach ( ( ) => {
13+ vi . useRealTimers ( )
14+ } )
15+
816const sleep = ( ms : number ) =>
917 new Promise ( ( resolve ) => {
1018 setTimeout ( resolve , ms )
@@ -39,11 +47,16 @@ it.skipIf(typeof use === 'undefined')('delayed increment', async () => {
3947 </ StrictMode > ,
4048 )
4149
42- expect ( await screen . findByText ( 'count: 0' ) ) . toBeInTheDocument ( )
43-
50+ await vi . waitFor ( ( ) =>
51+ expect ( screen . getByText ( 'count: 0' ) ) . toBeInTheDocument ( ) ,
52+ )
4453 fireEvent . click ( screen . getByText ( 'button' ) )
45- expect ( await screen . findByText ( 'loading' ) ) . toBeInTheDocument ( )
46- expect ( await screen . findByText ( 'count: 1' ) ) . toBeInTheDocument ( )
54+ await vi . waitFor ( ( ) =>
55+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( ) ,
56+ )
57+ await vi . waitFor ( ( ) =>
58+ expect ( screen . getByText ( 'count: 1' ) ) . toBeInTheDocument ( ) ,
59+ )
4760} )
4861
4962it . skipIf ( typeof use === 'undefined' ) ( 'delayed object' , async ( ) => {
@@ -70,11 +83,17 @@ it.skipIf(typeof use === 'undefined')('delayed object', async () => {
7083 </ StrictMode > ,
7184 )
7285
73- expect ( await screen . findByText ( 'text: none' ) ) . toBeInTheDocument ( )
86+ await vi . waitFor ( ( ) =>
87+ expect ( screen . getByText ( 'text: none' ) ) . toBeInTheDocument ( ) ,
88+ )
7489
7590 fireEvent . click ( screen . getByText ( 'button' ) )
76- expect ( await screen . findByText ( 'loading' ) ) . toBeInTheDocument ( )
77- expect ( await screen . findByText ( 'text: hello' ) ) . toBeInTheDocument ( )
91+ await vi . waitFor ( ( ) =>
92+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( ) ,
93+ )
94+ await vi . waitFor ( ( ) =>
95+ expect ( screen . getByText ( 'text: hello' ) ) . toBeInTheDocument ( ) ,
96+ )
7897} )
7998
8099it . skipIf ( typeof use === 'undefined' ) (
@@ -100,26 +119,30 @@ it.skipIf(typeof use === 'undefined')(
100119 )
101120 }
102121
103- await act ( async ( ) => {
122+ await act ( ( ) =>
104123 render (
105124 < StrictMode >
106125 < Suspense fallback = "loading" >
107126 < Counter />
108127 </ Suspense >
109128 </ StrictMode > ,
110- )
111- } )
129+ ) ,
130+ )
112131
113- expect ( await screen . findByText ( 'loading' ) ) . toBeInTheDocument ( )
114- await waitFor ( ( ) => {
132+ await vi . waitFor ( ( ) =>
133+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( ) ,
134+ )
135+ await vi . waitFor ( ( ) => {
115136 expect ( screen . getByText ( 'text: counter' ) ) . toBeInTheDocument ( )
116137 expect ( screen . getByText ( 'count: 0' ) ) . toBeInTheDocument ( )
117138 } )
118139
119140 fireEvent . click ( screen . getByText ( 'button' ) )
120141
121- expect ( await screen . findByText ( 'loading' ) ) . toBeInTheDocument ( )
122- await waitFor ( ( ) => {
142+ await vi . waitFor ( ( ) =>
143+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( ) ,
144+ )
145+ await vi . waitFor ( ( ) => {
123146 expect ( screen . getByText ( 'text: counter' ) ) . toBeInTheDocument ( )
124147 expect ( screen . getByText ( 'count: 1' ) ) . toBeInTheDocument ( )
125148 } )
@@ -150,9 +173,15 @@ it.skipIf(typeof use === 'undefined')('delayed falsy value', async () => {
150173 </ StrictMode > ,
151174 )
152175
153- expect ( await screen . findByText ( 'value: true' ) ) . toBeInTheDocument ( )
176+ await vi . waitFor ( ( ) =>
177+ expect ( screen . getByText ( 'value: true' ) ) . toBeInTheDocument ( ) ,
178+ )
154179
155180 fireEvent . click ( screen . getByText ( 'button' ) )
156- expect ( await screen . findByText ( 'loading' ) ) . toBeInTheDocument ( )
157- expect ( await screen . findByText ( 'value: null' ) ) . toBeInTheDocument ( )
181+ await vi . waitFor ( ( ) =>
182+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( ) ,
183+ )
184+ await vi . waitFor ( ( ) =>
185+ expect ( screen . getByText ( 'value: null' ) ) . toBeInTheDocument ( ) ,
186+ )
158187} )
0 commit comments