Skip to content

Commit f2e5683

Browse files
committed
docs: Fix userEvent/convenience Example Code
1 parent 99e9e47 commit f2e5683

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

docs/user-event/api-convenience.mdx

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ pointer([{target: element}, {keys: '[MouseLeft]', target: element}])
2020

2121
```js
2222
test('click', async () => {
23+
const user = userEvent.setup()
2324
const onChange = jest.fn()
2425
render(<input type="checkbox" onChange={onChange} />)
2526

2627
const checkbox = screen.getByRole('checkbox')
2728

28-
await userEvent.click(checkbox)
29+
await user.click(checkbox)
2930

3031
expect(onChange).toHaveBeenCalledTimes(1)
3132
expect(checkbox).toBeChecked()
@@ -46,12 +47,13 @@ pointer([{target: element}, {keys: '[MouseLeft][MouseLeft]', target: element}])
4647

4748
```js
4849
test('double click', async () => {
50+
const user = userEvent.setup()
4951
const onChange = jest.fn()
5052
render(<input type="checkbox" onChange={onChange} />)
5153

5254
const checkbox = screen.getByRole('checkbox')
5355

54-
await userEvent.dblClick(checkbox)
56+
await user.dblClick(checkbox)
5557

5658
expect(onChange).toHaveBeenCalledTimes(2)
5759
expect(checkbox).not.toBeChecked()
@@ -73,12 +75,13 @@ pointer([
7375

7476
```js
7577
test('triple click', async () => {
78+
const user = userEvent.setup()
7679
const onChange = jest.fn()
7780
render(<input type="checkbox" onChange={onChange} />)
7881

7982
const checkbox = screen.getByRole('checkbox')
8083

81-
await userEvent.tripleClick(checkbox)
84+
await user.tripleClick(checkbox)
8285

8386
expect(onChange).toHaveBeenCalledTimes(3)
8487
expect(checkbox).toBeChecked()
@@ -102,28 +105,29 @@ const TestComponent = () => {
102105
const [isHover, setIsHover] = useState(false)
103106

104107
return (
105-
<div
106-
style={{backgroundColor: isHover ? 'red' : 'green'}}
108+
<p
109+
role="paragraph"
107110
onMouseEnter={() => setIsHover(true)}
108111
onMouseLeave={() => setIsHover(false)}
109112
>
110-
Hover
111-
</div>
113+
{isHover ? 'Hover' : 'UnHover'}
114+
</p>
112115
)
113116
}
114117

115118
test('hover/unhover', async () => {
119+
const user = userEvent.setup()
116120
render(<TestComponent />)
117121

118-
const hoverBox = screen.getByText('Hover')
122+
const paragraph = screen.getByRole('paragraph')
119123

120-
await userEvent.hover(hoverBox)
124+
await user.hover(paragraph)
121125

122-
expect(hoverBox).toHaveStyle({backgroundColor: 'red'})
126+
expect(paragraph).toHaveTextContent('Hover')
123127

124-
await userEvent.unhover(hoverBox)
128+
await user.unhover(paragraph)
125129

126-
expect(hoverBox).toHaveStyle({backgroundColor: 'green'})
130+
expect(paragraph).toHaveTextContent('UnHover')
127131
})
128132
```
129133

@@ -156,6 +160,7 @@ keyboard('[/ShiftLeft][/ShiftRight]{Tab}')
156160

157161
```js
158162
test('tab', async () => {
163+
const user = userEvent.setup()
159164
render(
160165
<div>
161166
<input type="checkbox" />
@@ -170,24 +175,24 @@ test('tab', async () => {
170175

171176
expect(document.body).toHaveFocus()
172177

173-
await userEvent.tab()
178+
await user.tab()
174179

175180
expect(checkbox).toHaveFocus()
176181

177-
await userEvent.tab()
182+
await user.tab()
178183

179184
expect(radio).toHaveFocus()
180185

181-
await userEvent.tab()
186+
await user.tab()
182187

183188
expect(number).toHaveFocus()
184189

185-
await userEvent.tab()
190+
await user.tab()
186191

187192
// cycle goes back to the body element
188193
expect(document.body).toHaveFocus()
189194

190-
await userEvent.tab()
195+
await user.tab()
191196

192197
expect(checkbox).toHaveFocus()
193198
})

0 commit comments

Comments
 (0)