Skip to content

Commit a261fe0

Browse files
authored
Fix Autocomplete tests
1 parent 5c26f0b commit a261fe0

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/__tests__/Autocomplete.test.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import {render} from '../utils/testing'
3-
import {render as HTMLRender, fireEvent} from '@testing-library/react'
3+
import {render as HTMLRender, fireEvent, waitFor} from '@testing-library/react'
44
import {toHaveNoViolations} from 'jest-axe'
55
import 'babel-polyfill'
66
import Autocomplete, {AutocompleteInputProps} from '../Autocomplete'
@@ -267,7 +267,7 @@ describe('Autocomplete', () => {
267267
expect(inputNode.getAttribute('aria-expanded')).toBe('true')
268268
})
269269

270-
it('closes the menu when the input is blurred', () => {
270+
it('closes the menu when the input is blurred', async () => {
271271
const {getByLabelText} = HTMLRender(<LabelledAutocomplete menuProps={{items: [], selectedItemIds: []}} />)
272272
const inputNode = getByLabelText(AUTOCOMPLETE_LABEL)
273273

@@ -278,9 +278,7 @@ describe('Autocomplete', () => {
278278
fireEvent.blur(inputNode)
279279

280280
// wait a tick for blur to finish
281-
setTimeout(() => {
282-
expect(inputNode.getAttribute('aria-expanded')).not.toBe('true')
283-
}, 0)
281+
await waitFor(() => expect(inputNode.getAttribute('aria-expanded')).not.toBe('true'))
284282
})
285283

286284
it('sets the input value to the suggested item text and highlights the untyped part of the word', async () => {
@@ -306,10 +304,10 @@ describe('Autocomplete', () => {
306304
expect(getByDisplayValue('zero')).toBeDefined()
307305
expect((inputNode as HTMLInputElement).selectionStart).toBe(2)
308306
expect((inputNode as HTMLInputElement).selectionEnd).toBe(4)
309-
inputNode && (await user.type(inputNode, '{backspace}'))
307+
inputNode && (await user.keyboard('{backspace}'))
310308
expect((inputNode as HTMLInputElement).selectionStart).toBe(2)
311309
expect(getByDisplayValue('ze')).toBeDefined()
312-
inputNode && (await user.type(inputNode, 'r'))
310+
inputNode && (await user.keyboard('r'))
313311
expect((inputNode as HTMLInputElement).selectionStart).toBe(3)
314312
expect((inputNode as HTMLInputElement).selectionEnd).toBe(4)
315313
expect(getByDisplayValue('zero')).toBeDefined()
@@ -323,7 +321,7 @@ describe('Autocomplete', () => {
323321
expect(inputNode?.getAttribute('aria-expanded')).not.toBe('true')
324322
inputNode && (await user.type(inputNode, 'ze'))
325323
expect(inputNode?.getAttribute('aria-expanded')).toBe('true')
326-
inputNode && (await user.type(inputNode, '{esc}'))
324+
inputNode && (await user.keyboard('{escape}'))
327325
expect(inputNode?.getAttribute('aria-expanded')).not.toBe('true')
328326
})
329327

@@ -367,9 +365,7 @@ describe('Autocomplete', () => {
367365
}
368366

369367
// wait a tick for blur to finish
370-
setTimeout(() => {
371-
expect(sortOnCloseFnMock).toHaveBeenCalledTimes(mockItems.length)
372-
}, 0)
368+
await waitFor(() => expect(sortOnCloseFnMock).toHaveBeenCalled())
373369
})
374370

375371
it("calls onOpenChange with the menu's open state", async () => {
@@ -380,7 +376,7 @@ describe('Autocomplete', () => {
380376
)
381377
const inputNode = container.querySelector('#autocompleteInput')
382378

383-
inputNode && (await user.type(inputNode, 'ze'))
379+
await user.type(inputNode!, 'ze')
384380
expect(onOpenChangeMock).toHaveBeenCalled()
385381
})
386382

0 commit comments

Comments
 (0)