-
-
Notifications
You must be signed in to change notification settings - Fork 567
/
atomWithStore.test.tsx
50 lines (42 loc) 路 1.19 KB
/
atomWithStore.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { StrictMode } from 'react'
import { act, fireEvent, render } from '@testing-library/react'
import create from 'zustand/vanilla'
import { useAtom } from 'jotai'
import { atomWithStore } from 'jotai/zustand'
import { getTestProvider } from '../testUtils'
const Provider = getTestProvider()
it('count state', async () => {
const store = create(() => ({ count: 0 }))
const stateAtom = atomWithStore(store)
store.setState((prev) => ({ count: prev.count + 1 }))
const Counter = () => {
const [state, setState] = useAtom(stateAtom)
return (
<>
count: {state.count}
<button
onClick={() =>
setState((prev) => ({ ...prev, count: prev.count + 1 }))
}>
button
</button>
</>
)
}
const { findByText, getByText } = render(
<StrictMode>
<Provider>
<Counter />
</Provider>
</StrictMode>
)
await findByText('count: 1')
fireEvent.click(getByText('button'))
await findByText('count: 2')
expect(store.getState().count).toBe(2)
act(() => {
store.setState((prev) => ({ count: prev.count + 1 }))
})
await findByText('count: 3')
expect(store.getState().count).toBe(3)
})