Skip to content

Commit e618938

Browse files
authored
docs: add userEvent example code (#1283)
1 parent f9507e7 commit e618938

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

docs/user-event/api-convenience.mdx

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ click(element: Element): Promise<void>
1818
pointer([{target: element}, {keys: '[MouseLeft]', target: element}])
1919
```
2020

21+
```js
22+
test('click', async () => {
23+
const onChange = jest.fn()
24+
const user = userEvent.setup()
25+
26+
render(<input type="checkbox" onChange={onChange} />)
27+
28+
const checkbox = screen.getByRole('checkbox')
29+
30+
await user.click(checkbox)
31+
32+
expect(onChange).toHaveBeenCalledTimes(1)
33+
expect(checkbox).toBeChecked()
34+
})
35+
```
36+
2137
The first action might be skipped per [`skipHover`](options.mdx#skiphover).
2238

2339
### dblClick()
@@ -30,6 +46,22 @@ dblClick(element: Element): Promise<void>
3046
pointer([{target: element}, {keys: '[MouseLeft][MouseLeft]', target: element}])
3147
```
3248

49+
```js
50+
test('double click', async () => {
51+
const onChange = jest.fn()
52+
const user = userEvent.setup()
53+
54+
render(<input type="checkbox" onChange={onChange} />)
55+
56+
const checkbox = screen.getByRole('checkbox')
57+
58+
await user.dblClick(checkbox)
59+
60+
expect(onChange).toHaveBeenCalledTimes(2)
61+
expect(checkbox).not.toBeChecked()
62+
})
63+
```
64+
3365
### tripleClick()
3466

3567
```ts
@@ -43,6 +75,22 @@ pointer([
4375
])
4476
```
4577

78+
```js
79+
test('triple click', async () => {
80+
const onChange = jest.fn()
81+
const user = userEvent.setup()
82+
83+
render(<input type="checkbox" onChange={onChange} />)
84+
85+
const checkbox = screen.getByRole('checkbox')
86+
87+
await user.tripleClick(checkbox)
88+
89+
expect(onChange).toHaveBeenCalledTimes(3)
90+
expect(checkbox).toBeChecked()
91+
})
92+
```
93+
4694
## Mouse movement
4795

4896
### hover()
@@ -55,6 +103,33 @@ hover(element: Element): Promise<void>
55103
pointer({target: element})
56104
```
57105

106+
```js
107+
test('hover/unhover', async () => {
108+
const user = userEvent.setup()
109+
render(<div>Hover</div>)
110+
111+
const hoverBox = screen.getByText('Hover')
112+
let isHover = false
113+
114+
hoverBox.addEventListener('mouseover', () => {
115+
isHover = true
116+
})
117+
hoverBox.addEventListener('mouseout', () => {
118+
isHover = false
119+
})
120+
121+
expect(isHover).toBeFalsy()
122+
123+
await user.hover(hoverBox)
124+
125+
expect(isHover).toBeTruthy()
126+
127+
await user.unhover(hoverBox)
128+
129+
expect(isHover).toBeFalsy()
130+
})
131+
```
132+
58133
### unhover()
59134

60135
```ts
@@ -81,3 +156,43 @@ keyboard('{Shift>}{Tab}{/Shift}')
81156
// with shift=false
82157
keyboard('[/ShiftLeft][/ShiftRight]{Tab}')
83158
```
159+
160+
```js
161+
test('tab', async () => {
162+
const user = userEvent.setup()
163+
render(
164+
<div>
165+
<input type="checkbox" />
166+
<input type="radio" />
167+
<input type="number" />
168+
</div>,
169+
)
170+
171+
const checkbox = screen.getByRole('checkbox')
172+
const radio = screen.getByRole('radio')
173+
const number = screen.getByRole('spinbutton')
174+
175+
expect(document.body).toHaveFocus()
176+
177+
await user.tab()
178+
179+
expect(checkbox).toHaveFocus()
180+
181+
await user.tab()
182+
183+
expect(radio).toHaveFocus()
184+
185+
await user.tab()
186+
187+
expect(number).toHaveFocus()
188+
189+
await user.tab()
190+
191+
// cycle goes back to the body element
192+
expect(document.body).toHaveFocus()
193+
194+
await user.tab()
195+
196+
expect(checkbox).toHaveFocus()
197+
})
198+
```

0 commit comments

Comments
 (0)