Skip to content

Commit 22cf9b4

Browse files
authored
refactor: refactoring userEvent utility example code (#1309)
1 parent f45aa5c commit 22cf9b4

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

docs/user-event/api-utility.mdx

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ This API can be used to easily clear an editable element.
2222

2323
```jsx
2424
test('clear', async () => {
25+
const user = userEvent.setup()
26+
2527
render(<textarea defaultValue="Hello, World!" />)
2628

27-
await userEvent.clear(screen.getByRole('textbox'))
29+
await user.clear(screen.getByRole('textbox'))
2830

2931
expect(screen.getByRole('textbox')).toHaveValue('')
3032
})
@@ -37,12 +39,12 @@ be selected.
3739

3840
```ts
3941
selectOptions(
40-
element: Element,
41-
values: HTMLElement | HTMLElement[] | string[] | string,
42+
element: Element,
43+
values: HTMLElement | HTMLElement[] | string[] | string,
4244
): Promise<void>
4345
deselectOptions(
44-
element: Element,
45-
values: HTMLElement | HTMLElement[] | string[] | string,
46+
element: Element,
47+
values: HTMLElement | HTMLElement[] | string[] | string,
4648
): Promise<void>
4749
```
4850

@@ -61,6 +63,8 @@ just provide the element. It also accepts an array of these.
6163
6264
```jsx
6365
test('selectOptions', async () => {
66+
const user = userEvent.setup()
67+
6468
render(
6569
<select multiple>
6670
<option value="1">A</option>
@@ -69,7 +73,7 @@ test('selectOptions', async () => {
6973
</select>,
7074
)
7175

72-
await userEvent.selectOptions(screen.getByRole('listbox'), ['1', 'C'])
76+
await user.selectOptions(screen.getByRole('listbox'), ['1', 'C'])
7377

7478
expect(screen.getByRole('option', {name: 'A'}).selected).toBe(true)
7579
expect(screen.getByRole('option', {name: 'B'}).selected).toBe(false)
@@ -79,6 +83,8 @@ test('selectOptions', async () => {
7983

8084
```jsx
8185
test('deselectOptions', async () => {
86+
const user = userEvent.setup()
87+
8288
render(
8389
<select multiple>
8490
<option value="1">A</option>
@@ -89,7 +95,7 @@ test('deselectOptions', async () => {
8995
</select>,
9096
)
9197

92-
await userEvent.deselectOptions(screen.getByRole('listbox'), '2')
98+
await user.deselectOptions(screen.getByRole('listbox'), '2')
9399

94100
expect(screen.getByText('B').selected).toBe(false)
95101
})
@@ -102,14 +108,14 @@ Note that this API triggers pointer events and is therefore subject to
102108

103109
```ts
104110
type(
105-
element: Element,
106-
text: KeyboardInput,
107-
options?: {
108-
skipClick?: boolean
109-
skipAutoClose?: boolean
110-
initialSelectionStart?: number
111-
initialSelectionEnd?: number
112-
}
111+
element: Element,
112+
text: KeyboardInput,
113+
options?: {
114+
skipClick?: boolean
115+
skipAutoClose?: boolean
116+
initialSelectionStart?: number
117+
initialSelectionEnd?: number
118+
}
113119
): Promise<void>
114120
```
115121

@@ -129,10 +135,12 @@ Type into an input element.
129135

130136
```jsx
131137
test('type into an input field', async () => {
132-
render(<input defaultValue="Hello,"/>)
138+
const user = userEvent.setup()
139+
140+
render(<input defaultValue="Hello," />)
133141
const input = screen.getByRole('textbox')
134142

135-
await userEvent.type(input, ' World!')
143+
await user.type(input, ' World!')
136144

137145
expect(input).toHaveValue('Hello, World!')
138146
})
@@ -142,8 +150,8 @@ test('type into an input field', async () => {
142150

143151
```ts
144152
upload(
145-
element: HTMLElement,
146-
fileOrFiles: File | File[],
153+
element: HTMLElement,
154+
fileOrFiles: File | File[],
147155
): Promise<void>
148156
```
149157

@@ -155,6 +163,8 @@ file upload dialog.
155163
156164
```jsx
157165
test('upload file', async () => {
166+
const user = userEvent.setup()
167+
158168
render(
159169
<div>
160170
<label htmlFor="file-uploader">Upload file:</label>
@@ -164,14 +174,16 @@ test('upload file', async () => {
164174
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
165175
const input = screen.getByLabelText(/upload file/i)
166176

167-
await userEvent.upload(input, file)
177+
await user.upload(input, file)
168178

169179
expect(input.files[0]).toBe(file)
170180
expect(input.files.item(0)).toBe(file)
171181
expect(input.files).toHaveLength(1)
172182
})
173183

174184
test('upload multiple files', async () => {
185+
const user = userEvent.setup()
186+
175187
render(
176188
<div>
177189
<label htmlFor="file-uploader">Upload file:</label>
@@ -184,7 +196,7 @@ test('upload multiple files', async () => {
184196
]
185197
const input = screen.getByLabelText(/upload file/i)
186198

187-
await userEvent.upload(input, files)
199+
await user.upload(input, files)
188200

189201
expect(input.files).toHaveLength(2)
190202
expect(input.files[0]).toBe(files[0])

0 commit comments

Comments
 (0)