From 0410a059a9bc6fd32d40d4f663717d3d0782faa2 Mon Sep 17 00:00:00 2001 From: Philipp Fritsche Date: Wed, 11 May 2022 11:41:30 +0000 Subject: [PATCH 1/2] fix(selectOptions): wait after each click --- src/utility/selectOptions.ts | 12 +++++++++++- tests/react/index.tsx | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/utility/selectOptions.ts b/src/utility/selectOptions.ts index 04e44ef2..520c62d2 100644 --- a/src/utility/selectOptions.ts +++ b/src/utility/selectOptions.ts @@ -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( @@ -100,6 +106,8 @@ async function selectOptionsBase( if (withPointerEvents) { this.dispatchUIEvent(option, 'click') } + + await wait(this[Config]) } } else if (selectedOptions.length === 1) { const withPointerEvents = @@ -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`, diff --git a/tests/react/index.tsx b/tests/react/index.tsx index 1f87e886..60cab9c0 100644 --- a/tests/react/index.tsx +++ b/tests/react/index.tsx @@ -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' @@ -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([]) + + return ( + + ) + } + + render(