Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(selectOptions): wait after each click #951

Merged
merged 3 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/utility/selectOptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import {getConfig} from '@testing-library/dom'
import {focus, hasPointerEvents, isDisabled, isElementType} from '../utils'
import {
focus,
hasPointerEvents,
isDisabled,
isElementType,
wait,
} from '../utils'
import {Config, Instance} from '../setup'

export async function selectOptions(
Expand Down Expand Up @@ -100,6 +106,8 @@ async function selectOptionsBase(
if (withPointerEvents) {
this.dispatchUIEvent(option, 'click')
}

await wait(this[Config])
}
} else if (selectedOptions.length === 1) {
const withPointerEvents =
Expand All @@ -124,6 +132,8 @@ async function selectOptionsBase(
this.dispatchUIEvent(select, 'mouseup')
this.dispatchUIEvent(select, 'click')
}

await wait(this[Config])
} else {
throw getConfig().getElementError(
`Cannot select multiple options on a non-multiple select`,
Expand Down
3 changes: 3 additions & 0 deletions tests/react/_env/setup-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ if (global.REACT_VERSION) {
jest.mock('react-dom', () =>
jest.requireActual(`reactDom${global.REACT_VERSION}`),
)
jest.mock('react-dom/test-utils', () =>
jest.requireActual(`reactDom${global.REACT_VERSION}/test-utils`),
)
jest.mock('react-is', () =>
jest.requireActual(`reactIs${global.REACT_VERSION}`),
)
Expand Down
35 changes: 34 additions & 1 deletion tests/react/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {useState} from 'react'
import {render, screen} from '@testing-library/react'
import {render, screen, waitFor} from '@testing-library/react'
import userEvent from '#src'
import {getUIValue} from '#src/document'
import {addListeners} from '#testHelpers'
Expand Down Expand Up @@ -173,3 +173,36 @@ describe('typing in a formatted input', () => {
expect(element).toHaveValue('$234')
})
})

test('change select with delayed state update', async () => {
function Select() {
const [selected, setSelected] = useState<string[]>([])

return (
<select
multiple
value={selected}
onChange={e => {
const values = Array.from(e.target.selectedOptions).map(o => o.value)
setTimeout(() => setSelected(values))
}}
>
<option>Chrome</option>
<option>Firefox</option>
<option>Opera</option>
</select>
)
}

render(<Select />)

await userEvent.selectOptions(
screen.getByRole('listbox'),
['Chrome', 'Firefox'],
{delay: 10},
)

await waitFor(() => {
expect(screen.getByRole('listbox')).toHaveValue(['Chrome', 'Firefox'])
})
})