Skip to content

Commit 5d220f5

Browse files
committed
test(react/useAtomValue): replace 'userEvent' with 'fireEvent' and add fake timers
1 parent 93723d9 commit 5d220f5

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

tests/react/useAtomValue.test.tsx

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import { Component, StrictMode, Suspense } from 'react'
22
import 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'
65
import { useAtomValue, useSetAtom } from 'jotai/react'
76
import { atom } from 'jotai/vanilla'
87

8+
beforeEach(() => {
9+
vi.useFakeTimers()
10+
})
11+
12+
afterEach(() => {
13+
vi.useRealTimers()
14+
})
15+
916
it('useAtomValue basic test', async () => {
1017
const countAtom = atom(0)
1118

@@ -21,37 +28,11 @@ it('useAtomValue basic test', async () => {
2128
)
2229
}
2330

24-
render(
25-
<StrictMode>
26-
<Counter />
27-
</StrictMode>,
28-
)
31+
render(<Counter />)
2932

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()
33+
expect(screen.getByText('count: 0')).toBeInTheDocument()
34+
fireEvent.click(screen.getByText('dispatch'))
35+
expect(screen.getByText('count: 1')).toBeInTheDocument()
5536
})
5637

5738
class ErrorBoundary extends Component<
@@ -96,7 +77,7 @@ it('useAtomValue with error throwing atom', async () => {
9677
</StrictMode>,
9778
)
9879

99-
expect(await screen.findByText('error: fail')).toBeInTheDocument()
80+
expect(screen.getByText('error: fail')).toBeInTheDocument()
10081
})
10182

10283
it('useAtomValue with atom returning object', async () => {
@@ -118,5 +99,31 @@ it('useAtomValue with atom returning object', async () => {
11899
</StrictMode>,
119100
)
120101

121-
expect(await screen.findByText('obj: 1,2')).toBeInTheDocument()
102+
expect(screen.getByText('obj: 1,2')).toBeInTheDocument()
103+
})
104+
105+
it('useAtomValue with async atom (promise)', async () => {
106+
const asyncAtom = atom(async () => {
107+
await new Promise((resolve) => setTimeout(resolve, 10))
108+
return 42
109+
})
110+
111+
const AsyncComponent = () => {
112+
const value = useAtomValue(asyncAtom)
113+
114+
return <div>value: {value}</div>
115+
}
116+
117+
await act(() =>
118+
render(
119+
<Suspense fallback={<div>loading</div>}>
120+
<AsyncComponent />
121+
</Suspense>,
122+
),
123+
)
124+
125+
expect(screen.getByText('loading')).toBeInTheDocument()
126+
127+
await act(() => vi.advanceTimersByTimeAsync(10))
128+
expect(screen.getByText('value: 42')).toBeInTheDocument()
122129
})

0 commit comments

Comments
 (0)