11import { Component , StrictMode , Suspense } from 'react'
22import type { ReactNode } from 'react'
3- import { act , render , screen } from '@testing-library/react'
4- import userEvent from '@testing-library/user-event'
5- import { expect , it } from 'vitest'
3+ import { act , fireEvent , render , screen } from '@testing-library/react'
4+ import { afterEach , beforeEach , expect , it , vi } from 'vitest'
65import { useAtomValue , useSetAtom } from 'jotai/react'
76import { atom } from 'jotai/vanilla'
87
8+ beforeEach ( ( ) => {
9+ vi . useFakeTimers ( )
10+ } )
11+
12+ afterEach ( ( ) => {
13+ vi . useRealTimers ( )
14+ } )
15+
916it ( 'useAtomValue basic test' , async ( ) => {
1017 const countAtom = atom ( 0 )
1118
@@ -27,31 +34,9 @@ it('useAtomValue basic test', async () => {
2734 </ StrictMode > ,
2835 )
2936
30- expect ( await screen . findByText ( 'count: 0' ) ) . toBeInTheDocument ( )
31- await userEvent . click ( screen . getByText ( 'dispatch' ) )
32- expect ( await screen . findByText ( 'count: 1' ) ) . toBeInTheDocument ( )
33- } )
34-
35- it ( 'useAtomValue with async atom (promise)' , async ( ) => {
36- const asyncAtom = atom ( async ( ) => 42 )
37-
38- const AsyncComponent = ( ) => {
39- const value = useAtomValue ( asyncAtom )
40-
41- return < div > value: { value } </ div >
42- }
43-
44- await act ( async ( ) => {
45- render (
46- < StrictMode >
47- < Suspense fallback = "loading" >
48- < AsyncComponent />
49- </ Suspense >
50- </ StrictMode > ,
51- )
52- } )
53-
54- expect ( await screen . findByText ( 'value: 42' ) ) . toBeInTheDocument ( )
37+ expect ( screen . getByText ( 'count: 0' ) ) . toBeInTheDocument ( )
38+ fireEvent . click ( screen . getByText ( 'dispatch' ) )
39+ expect ( screen . getByText ( 'count: 1' ) ) . toBeInTheDocument ( )
5540} )
5641
5742class ErrorBoundary extends Component <
@@ -96,7 +81,7 @@ it('useAtomValue with error throwing atom', async () => {
9681 </ StrictMode > ,
9782 )
9883
99- expect ( await screen . findByText ( 'error: fail' ) ) . toBeInTheDocument ( )
84+ expect ( screen . getByText ( 'error: fail' ) ) . toBeInTheDocument ( )
10085} )
10186
10287it ( 'useAtomValue with atom returning object' , async ( ) => {
@@ -118,5 +103,33 @@ it('useAtomValue with atom returning object', async () => {
118103 </ StrictMode > ,
119104 )
120105
121- expect ( await screen . findByText ( 'obj: 1,2' ) ) . toBeInTheDocument ( )
106+ expect ( screen . getByText ( 'obj: 1,2' ) ) . toBeInTheDocument ( )
107+ } )
108+
109+ it ( 'useAtomValue with async atom (promise)' , async ( ) => {
110+ const asyncAtom = atom ( async ( ) => {
111+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) )
112+ return 42
113+ } )
114+
115+ const AsyncComponent = ( ) => {
116+ const value = useAtomValue ( asyncAtom )
117+
118+ return < div > value: { value } </ div >
119+ }
120+
121+ await act ( ( ) =>
122+ render (
123+ < StrictMode >
124+ < Suspense fallback = { < div > loading</ div > } >
125+ < AsyncComponent />
126+ </ Suspense >
127+ </ StrictMode > ,
128+ ) ,
129+ )
130+
131+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( )
132+
133+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 10 ) )
134+ expect ( screen . getByText ( 'value: 42' ) ) . toBeInTheDocument ( )
122135} )
0 commit comments