Skip to content

Commit 7ea7a77

Browse files
authoredMay 12, 2022
fix(selectOptions): wait after each click (#951)
1 parent 6f55fee commit 7ea7a77

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed
 

‎src/utility/selectOptions.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import {getConfig} from '@testing-library/dom'
2-
import {focus, hasPointerEvents, isDisabled, isElementType} from '../utils'
2+
import {
3+
focus,
4+
hasPointerEvents,
5+
isDisabled,
6+
isElementType,
7+
wait,
8+
} from '../utils'
39
import {Config, Instance} from '../setup'
410

511
export async function selectOptions(
@@ -100,6 +106,8 @@ async function selectOptionsBase(
100106
if (withPointerEvents) {
101107
this.dispatchUIEvent(option, 'click')
102108
}
109+
110+
await wait(this[Config])
103111
}
104112
} else if (selectedOptions.length === 1) {
105113
const withPointerEvents =
@@ -124,6 +132,8 @@ async function selectOptionsBase(
124132
this.dispatchUIEvent(select, 'mouseup')
125133
this.dispatchUIEvent(select, 'click')
126134
}
135+
136+
await wait(this[Config])
127137
} else {
128138
throw getConfig().getElementError(
129139
`Cannot select multiple options on a non-multiple select`,

‎tests/react/_env/setup-env.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ if (global.REACT_VERSION) {
66
jest.mock('react-dom', () =>
77
jest.requireActual(`reactDom${global.REACT_VERSION}`),
88
)
9+
jest.mock('react-dom/test-utils', () =>
10+
jest.requireActual(`reactDom${global.REACT_VERSION}/test-utils`),
11+
)
912
jest.mock('react-is', () =>
1013
jest.requireActual(`reactIs${global.REACT_VERSION}`),
1114
)

‎tests/react/index.tsx

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {useState} from 'react'
2-
import {render, screen} from '@testing-library/react'
2+
import {render, screen, waitFor} from '@testing-library/react'
33
import userEvent from '#src'
44
import {getUIValue} from '#src/document'
55
import {addListeners} from '#testHelpers'
@@ -173,3 +173,36 @@ describe('typing in a formatted input', () => {
173173
expect(element).toHaveValue('$234')
174174
})
175175
})
176+
177+
test('change select with delayed state update', async () => {
178+
function Select() {
179+
const [selected, setSelected] = useState<string[]>([])
180+
181+
return (
182+
<select
183+
multiple
184+
value={selected}
185+
onChange={e => {
186+
const values = Array.from(e.target.selectedOptions).map(o => o.value)
187+
setTimeout(() => setSelected(values))
188+
}}
189+
>
190+
<option>Chrome</option>
191+
<option>Firefox</option>
192+
<option>Opera</option>
193+
</select>
194+
)
195+
}
196+
197+
render(<Select />)
198+
199+
await userEvent.selectOptions(
200+
screen.getByRole('listbox'),
201+
['Chrome', 'Firefox'],
202+
{delay: 10},
203+
)
204+
205+
await waitFor(() => {
206+
expect(screen.getByRole('listbox')).toHaveValue(['Chrome', 'Firefox'])
207+
})
208+
})

0 commit comments

Comments
 (0)
Please sign in to comment.