Skip to content

Commit 5c26f0b

Browse files
authored
Upgrade userEvent to v14
1 parent 62dbc98 commit 5c26f0b

13 files changed

+151
-112
lines changed

package-lock.json

Lines changed: 9 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
"@testing-library/jest-dom": "5.16.4",
122122
"@testing-library/react": "12.1.5",
123123
"@testing-library/react-hooks": "7.0.2",
124-
"@testing-library/user-event": "13.1.9",
124+
"@testing-library/user-event": "^14.3.0",
125125
"@types/chroma-js": "2.1.3",
126126
"@types/enzyme": "3.10.9",
127127
"@types/jest": "27.0.2",

src/SegmentedControl/SegmentedControl.test.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ describe('SegmentedControl', () => {
153153
}
154154
})
155155

156-
it('calls onChange with index of clicked segment button', () => {
156+
it('calls onChange with index of clicked segment button', async () => {
157+
const user = userEvent.setup()
157158
const handleChange = jest.fn()
158159
const {getByText} = render(
159160
<SegmentedControl aria-label="File view" onChange={handleChange}>
@@ -169,12 +170,13 @@ describe('SegmentedControl', () => {
169170

170171
expect(handleChange).not.toHaveBeenCalled()
171172
if (buttonToClick) {
172-
userEvent.click(buttonToClick)
173+
await user.click(buttonToClick)
173174
}
174175
expect(handleChange).toHaveBeenCalledWith(1)
175176
})
176177

177-
it('calls segment button onClick if it is passed', () => {
178+
it('calls segment button onClick if it is passed', async () => {
179+
const user = userEvent.setup()
178180
const handleClick = jest.fn()
179181
const {getByText} = render(
180182
<SegmentedControl aria-label="File view">
@@ -190,12 +192,13 @@ describe('SegmentedControl', () => {
190192

191193
expect(handleClick).not.toHaveBeenCalled()
192194
if (buttonToClick) {
193-
userEvent.click(buttonToClick)
195+
await user.click(buttonToClick)
194196
}
195197
expect(handleClick).toHaveBeenCalled()
196198
})
197199

198-
it('focuses the selected button first', () => {
200+
it('focuses the selected button first', async () => {
201+
const user = userEvent.setup()
199202
const {getByRole} = render(
200203
<>
201204
<button>Before</button>
@@ -212,8 +215,8 @@ describe('SegmentedControl', () => {
212215

213216
expect(document.activeElement?.id).not.toEqual(initialFocusButtonNode.id)
214217

215-
userEvent.tab() // focus the button before the segmented control
216-
userEvent.tab() // move focus into the segmented control
218+
await user.tab() // focus the button before the segmented control
219+
await user.tab() // move focus into the segmented control
217220

218221
expect(document.activeElement?.id).toEqual(initialFocusButtonNode.id)
219222
})

src/__tests__/Autocomplete.test.tsx

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ describe('Autocomplete', () => {
193193
})
194194

195195
describe('Autocomplete.Input', () => {
196-
it('calls onChange', () => {
196+
it('calls onChange', async () => {
197+
const user = userEvent.setup()
197198
const onChangeMock = jest.fn()
198199
const {container} = HTMLRender(
199200
<LabelledAutocomplete
@@ -204,7 +205,7 @@ describe('Autocomplete', () => {
204205
const inputNode = container.querySelector('#autocompleteInput')
205206

206207
expect(onChangeMock).not.toHaveBeenCalled()
207-
inputNode && userEvent.type(inputNode, 'z')
208+
inputNode && (await user.type(inputNode, 'z'))
208209
expect(onChangeMock).toHaveBeenCalled()
209210
})
210211

@@ -244,15 +245,16 @@ describe('Autocomplete', () => {
244245
expect(onKeyUpMock).toHaveBeenCalled()
245246
})
246247

247-
it('calls onKeyPress', () => {
248+
it('calls onKeyPress', async () => {
249+
const user = userEvent.setup()
248250
const onKeyPressMock = jest.fn()
249251
const {getByLabelText} = HTMLRender(
250252
<LabelledAutocomplete inputProps={{onKeyPress: onKeyPressMock}} menuProps={{items: [], selectedItemIds: []}} />
251253
)
252254
const inputNode = getByLabelText(AUTOCOMPLETE_LABEL)
253255

254256
expect(onKeyPressMock).not.toHaveBeenCalled()
255-
userEvent.type(inputNode, '{enter}')
257+
await user.type(inputNode, '{enter}')
256258
expect(onKeyPressMock).toHaveBeenCalled()
257259
})
258260

@@ -281,44 +283,47 @@ describe('Autocomplete', () => {
281283
}, 0)
282284
})
283285

284-
it('sets the input value to the suggested item text and highlights the untyped part of the word', () => {
286+
it('sets the input value to the suggested item text and highlights the untyped part of the word', async () => {
287+
const user = userEvent.setup()
285288
const {container, getByDisplayValue} = HTMLRender(
286289
<LabelledAutocomplete menuProps={{items: mockItems, selectedItemIds: []}} />
287290
)
288291
const inputNode = container.querySelector('#autocompleteInput')
289292

290-
inputNode && userEvent.type(inputNode, 'ze')
293+
inputNode && (await user.type(inputNode, 'ze'))
291294
expect(getByDisplayValue('zero')).toBeDefined()
292295
})
293296

294-
it('does not show or highlight suggestion text after the user hits Backspace until they hit another key', () => {
297+
it('does not show or highlight suggestion text after the user hits Backspace until they hit another key', async () => {
298+
const user = userEvent.setup()
295299
const {container, getByDisplayValue} = HTMLRender(
296300
<LabelledAutocomplete menuProps={{items: mockItems, selectedItemIds: []}} />
297301
)
298302
const inputNode = container.querySelector('#autocompleteInput')
299303

300304
expect((inputNode as HTMLInputElement).selectionStart).toBe(0)
301-
inputNode && userEvent.type(inputNode, 'ze')
305+
inputNode && (await user.type(inputNode, 'ze'))
302306
expect(getByDisplayValue('zero')).toBeDefined()
303307
expect((inputNode as HTMLInputElement).selectionStart).toBe(2)
304308
expect((inputNode as HTMLInputElement).selectionEnd).toBe(4)
305-
inputNode && userEvent.type(inputNode, '{backspace}')
309+
inputNode && (await user.type(inputNode, '{backspace}'))
306310
expect((inputNode as HTMLInputElement).selectionStart).toBe(2)
307311
expect(getByDisplayValue('ze')).toBeDefined()
308-
inputNode && userEvent.type(inputNode, 'r')
312+
inputNode && (await user.type(inputNode, 'r'))
309313
expect((inputNode as HTMLInputElement).selectionStart).toBe(3)
310314
expect((inputNode as HTMLInputElement).selectionEnd).toBe(4)
311315
expect(getByDisplayValue('zero')).toBeDefined()
312316
})
313317

314-
it('clears the input value when the user hits Escape', () => {
318+
it('clears the input value when the user hits Escape', async () => {
319+
const user = userEvent.setup()
315320
const {container} = HTMLRender(<LabelledAutocomplete menuProps={{items: mockItems, selectedItemIds: []}} />)
316321
const inputNode = container.querySelector('#autocompleteInput')
317322

318323
expect(inputNode?.getAttribute('aria-expanded')).not.toBe('true')
319-
inputNode && userEvent.type(inputNode, 'ze')
324+
inputNode && (await user.type(inputNode, 'ze'))
320325
expect(inputNode?.getAttribute('aria-expanded')).toBe('true')
321-
inputNode && userEvent.type(inputNode, '{esc}')
326+
inputNode && (await user.type(inputNode, '{esc}'))
322327
expect(inputNode?.getAttribute('aria-expanded')).not.toBe('true')
323328
})
324329

@@ -332,18 +337,20 @@ describe('Autocomplete', () => {
332337
})
333338

334339
describe('Autocomplete.Menu', () => {
335-
it('calls a custom filter function', () => {
340+
it('calls a custom filter function', async () => {
341+
const user = userEvent.setup()
336342
const filterFnMock = jest.fn()
337343
const {container} = HTMLRender(
338344
<LabelledAutocomplete menuProps={{items: mockItems, selectedItemIds: [], filterFn: filterFnMock}} />
339345
)
340346
const inputNode = container.querySelector('#autocompleteInput')
341347

342-
inputNode && userEvent.type(inputNode, 'ze')
348+
inputNode && (await user.type(inputNode, 'ze'))
343349
expect(filterFnMock).toHaveBeenCalled()
344350
})
345351

346-
it('calls a custom sort function when the menu closes', () => {
352+
it('calls a custom sort function when the menu closes', async () => {
353+
const user = userEvent.setup()
347354
const sortOnCloseFnMock = jest.fn()
348355
const {container} = HTMLRender(
349356
<LabelledAutocomplete menuProps={{items: mockItems, selectedItemIds: [], sortOnCloseFn: sortOnCloseFnMock}} />
@@ -354,7 +361,7 @@ describe('Autocomplete', () => {
354361
// current sort order matches the result of `sortOnCloseFnMock`
355362
expect(sortOnCloseFnMock).toHaveBeenCalledTimes(mockItems.length - 1)
356363
if (inputNode) {
357-
userEvent.type(inputNode, 'ze')
364+
await user.type(inputNode, 'ze')
358365
// eslint-disable-next-line github/no-blur
359366
fireEvent.blur(inputNode)
360367
}
@@ -365,18 +372,20 @@ describe('Autocomplete', () => {
365372
}, 0)
366373
})
367374

368-
it("calls onOpenChange with the menu's open state", () => {
375+
it("calls onOpenChange with the menu's open state", async () => {
376+
const user = userEvent.setup()
369377
const onOpenChangeMock = jest.fn()
370378
const {container} = HTMLRender(
371379
<LabelledAutocomplete menuProps={{items: mockItems, selectedItemIds: [], onOpenChange: onOpenChangeMock}} />
372380
)
373381
const inputNode = container.querySelector('#autocompleteInput')
374382

375-
inputNode && userEvent.type(inputNode, 'ze')
383+
inputNode && (await user.type(inputNode, 'ze'))
376384
expect(onOpenChangeMock).toHaveBeenCalled()
377385
})
378386

379-
it('calls onSelectedChange with the data for the selected items', () => {
387+
it('calls onSelectedChange with the data for the selected items', async () => {
388+
const user = userEvent.setup()
380389
const onSelectedChangeMock = jest.fn()
381390
const {container} = HTMLRender(
382391
<LabelledAutocomplete
@@ -388,7 +397,7 @@ describe('Autocomplete', () => {
388397
expect(onSelectedChangeMock).not.toHaveBeenCalled()
389398
if (inputNode) {
390399
fireEvent.focus(inputNode)
391-
userEvent.type(inputNode, '{enter}')
400+
await user.type(inputNode, '{enter}')
392401
}
393402

394403
// wait a tick for the keyboard event to be dispatched to the menu item
@@ -397,7 +406,8 @@ describe('Autocomplete', () => {
397406
}, 0)
398407
})
399408

400-
it('does not close the menu when clicking an item in the menu if selectionVariant=multiple', () => {
409+
it('does not close the menu when clicking an item in the menu if selectionVariant=multiple', async () => {
410+
const user = userEvent.setup()
401411
const {getByText, container} = HTMLRender(
402412
<LabelledAutocomplete menuProps={{items: mockItems, selectedItemIds: [], selectionVariant: 'multiple'}} />
403413
)
@@ -408,7 +418,7 @@ describe('Autocomplete', () => {
408418
inputNode && fireEvent.focus(inputNode)
409419
expect(inputNode?.getAttribute('aria-expanded')).toBe('true')
410420
fireEvent.click(itemToClickNode)
411-
inputNode && userEvent.type(inputNode, '{enter}')
421+
inputNode && (await user.type(inputNode, '{enter}'))
412422
expect(inputNode?.getAttribute('aria-expanded')).toBe('true')
413423
})
414424

src/__tests__/Checkbox.test.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,20 @@ describe('Checkbox', () => {
4545
expect(checkbox.checked).toEqual(true)
4646
})
4747

48-
it('accepts a change handler that can alter the checkbox state', () => {
48+
it('accepts a change handler that can alter the checkbox state', async () => {
49+
const user = userEvent.setup()
4950
const handleChange = jest.fn()
5051
const {getByRole} = render(<Checkbox onChange={handleChange} />)
5152

5253
const checkbox = getByRole('checkbox') as HTMLInputElement
5354

5455
expect(checkbox.checked).toEqual(false)
5556

56-
userEvent.click(checkbox)
57+
await user.click(checkbox)
5758
expect(handleChange).toHaveBeenCalled()
5859
expect(checkbox.checked).toEqual(true)
5960

60-
userEvent.click(checkbox)
61+
await user.click(checkbox)
6162
expect(handleChange).toHaveBeenCalled()
6263
expect(checkbox.checked).toEqual(false)
6364
})
@@ -72,7 +73,8 @@ describe('Checkbox', () => {
7273
expect(checkbox.checked).toEqual(false)
7374
})
7475

75-
it('renders an inactive checkbox state correctly', () => {
76+
it('renders an inactive checkbox state correctly', async () => {
77+
const user = userEvent.setup()
7678
const handleChange = jest.fn()
7779
const {getByRole, rerender} = render(<Checkbox disabled onChange={handleChange} />)
7880

@@ -82,7 +84,7 @@ describe('Checkbox', () => {
8284
expect(checkbox.checked).toEqual(false)
8385
expect(checkbox).toHaveAttribute('aria-disabled', 'true')
8486

85-
userEvent.click(checkbox)
87+
await user.click(checkbox)
8688

8789
expect(checkbox.disabled).toEqual(true)
8890
expect(checkbox.checked).toEqual(false)
@@ -94,14 +96,15 @@ describe('Checkbox', () => {
9496
expect(checkbox).toHaveAttribute('aria-disabled', 'false')
9597
})
9698

97-
it('renders an uncontrolled component correctly', () => {
99+
it('renders an uncontrolled component correctly', async () => {
100+
const user = userEvent.setup()
98101
const {getByRole} = render(<Checkbox defaultChecked />)
99102

100103
const checkbox = getByRole('checkbox') as HTMLInputElement
101104

102105
expect(checkbox.checked).toEqual(true)
103106

104-
userEvent.click(checkbox)
107+
await user.click(checkbox)
105108

106109
expect(checkbox.checked).toEqual(false)
107110
})

src/__tests__/CheckboxGroup.test.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ describe('CheckboxGroup', () => {
9191

9292
expect(requiredIndicator).toBeInTheDocument()
9393
})
94-
it('calls onChange handlers passed to CheckboxGroup and Checkbox', () => {
94+
it('calls onChange handlers passed to CheckboxGroup and Checkbox', async () => {
95+
const user = userEvent.setup()
9596
const handleParentChange = jest.fn()
9697
const handleCheckboxChange = jest.fn()
9798
const {getByLabelText} = render(
@@ -115,11 +116,12 @@ describe('CheckboxGroup', () => {
115116

116117
expect(handleParentChange).not.toHaveBeenCalled()
117118
expect(handleCheckboxChange).not.toHaveBeenCalled()
118-
userEvent.click(checkbox)
119+
await user.click(checkbox)
119120
expect(handleParentChange).toHaveBeenCalled()
120121
expect(handleCheckboxChange).toHaveBeenCalled()
121122
})
122-
it('calls onChange handler on CheckboxGroup with selected values', () => {
123+
it('calls onChange handler on CheckboxGroup with selected values', async () => {
124+
const user = userEvent.setup()
123125
const handleParentChange = jest.fn()
124126
const {getByLabelText} = render(
125127
<CheckboxGroup onChange={handleParentChange}>
@@ -142,7 +144,7 @@ describe('CheckboxGroup', () => {
142144
const checkbox = getByLabelText('Choice one') as HTMLInputElement
143145

144146
expect(handleParentChange).not.toHaveBeenCalled()
145-
userEvent.click(checkbox)
147+
await user.click(checkbox)
146148
expect(handleParentChange).toHaveBeenCalledWith(
147149
['two', 'one'],
148150
expect.objectContaining({
@@ -151,7 +153,7 @@ describe('CheckboxGroup', () => {
151153
})
152154
})
153155
)
154-
userEvent.click(checkbox)
156+
await user.click(checkbox)
155157
expect(handleParentChange).toHaveBeenCalledWith(
156158
['two'],
157159
expect.objectContaining({

0 commit comments

Comments
 (0)