deno.land / x / jotai@v1.8.4 / docs / guides / testing.mdx

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
---title: Testingdescription: How to test your code using Jotainav: 3.08---
We echo the [guiding principles of Testing library](https://testing-library.com/docs/guiding-principles/):
- "The more your tests resemble the way your software is used, the more confidence they can give you."We encourage you to write tests, like the user would interact with your atoms and components,therefore treating Jotai as an implementation detail.
Here's an example using [React testing library](https://github.com/testing-library/react-testing-library):
`Counter.tsx`:
```jsximport { atom, useAtom } from 'jotai'
const countAtom = atom(0)
export function Counter() { const [count, setCount] = useAtom(countAtom) return ( <h1> <p>{count}</p> <button onClick={() => setCount((c) => c + 1)}>one up</button> </h1> )}```
`Counter.test.ts`:
```jsximport React from 'react'import { render, screen, fireEvent } from '@testing-library/react'import { Counter } from './Counter'
test('should increment counter', () => { // Arrange render(<Counter />) const counter = screen.getByText('0') const incrementButton = screen.getByText('one up') // Act fireEvent.click(incrementButton) // Assert expect(counter.textContent).toEqual('1')})```
## Injected Values
You may want to inject arbitrary values to your atom before starting some tests.Maybe the counter should be limited to 100. Let's see how to test that it doesn't increase after reaching 100.In order to do that, simply use a [Provider](..api/core#provider), and export your atom to be filled-in.
```tsximport React from 'react'import { render, screen, fireEvent } from '@testing-library/react'import { countAtom, Counter } from './Counter'import { Provider } from 'jotai'
const CounterProvider = () => { return ( <Provider initialValues={[[countAtom, 100]]}> <Counter /> </Provider> )}
test('should not increment on max (100)', () => { render(<CounterProvider />) const counter = screen.getByText('100') const incrementButton = screen.getByText('one up') fireEvent.click(incrementButton) expect(counter.textContent).toEqual('100')})```
## Custom hooks
If you have complex atoms, sometimes you want to test them in isolation.
For that, you can use [React Hooks Testing Library](https://github.com/testing-library/react-hooks-testing-library).Here's an example below:
`countAtom.ts`:
```tsimport { useAtom } from 'jotai'import { atomWithReducer } from 'jotai/utils'
const reducer = (state: number, action?: 'INCREASE' | 'DECREASE') => { switch (action) { case 'INCREASE': return state + 1 case 'DECREASE': return state - 1 case undefined: return state }}export const countAtom = atomWithReducer(0, reducer)```
`countAtom.test.ts`:
```tsimport { renderHook, act } from '@testing-library/react-hooks'import { useAtom } from 'jotai'import { countAtom } from './countAtom'
test('should increment counter', () => { const { result } = renderHook(() => useAtom(countAtom)) act(() => { result.current[1]('INCREASE') }) expect(result.current[0]).toBe(1)})```
## Example with React-Native
Of course, you can test React-Native components too the same way, with or without `Provider`.
```tsximport React from 'react'import { render, fireEvent } from '@testing-library/react-native'import { Counter } from './counter'
test('should increment counter', () => { // Arrange const { getByText } = render(<Counter />) const counter = getByText('0') const incrementButton = getByText('one up') // Act fireEvent.press(incrementButton) // Assert expect(counter.props.children.toString()).toEqual('1')})```
jotai

Version Info

Tagged at
a year ago