Skip to content

Commit 3d880ff

Browse files
fix(test): stabilize flaky ResultsSearchQueryBuilder spec (#111916)
Applies a trio of strategies I've used in other flaky test cleanups: * Instead of broadly typing with `userEvent.type`, switches to calling `userEvent.click` to focus elements, `screen.findByRole` to wait until the listbox is populated, and then manually calling `userEvent.keyboard` to type. The granular events, each awaited, give more explicit waiting for async operations in-between each. * `delay:null` for `userEvent.keyboard`, to minimize the number of renders triggered by typing * After tests, waiting until the `combobox` role is found, to make sure async setup from the components is done. Fixes ENG-7201 Made with [Cursor](https://cursor.com) --------- Co-authored-by: Claude Sonnet 4 <noreply@anthropic.com>
1 parent ba5329c commit 3d880ff

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

static/app/views/discover/results/resultsSearchQueryBuilder.spec.tsx

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import {OrganizationFixture} from 'sentry-fixture/organization';
22

3-
import {render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
3+
import {
4+
render,
5+
screen,
6+
userEvent,
7+
waitFor,
8+
within,
9+
} from 'sentry-test/reactTestingLibrary';
410

511
import {SavedSearchType} from 'sentry/types/group';
612
import type {Organization} from 'sentry/types/organization';
@@ -51,12 +57,17 @@ describe('ResultsSearchQueryBuilder', () => {
5157

5258
// Focus the input and type "has:p" to simulate a search for p50
5359
const input = await screen.findByRole('combobox');
54-
await userEvent.type(input, 'has:p');
60+
await userEvent.click(input);
61+
await screen.findByRole('listbox');
62+
await userEvent.keyboard('has:p', {delay: null});
5563

5664
// Check that "p50" (a function tag) is NOT in the dropdown
57-
expect(
58-
within(await screen.findByRole('listbox')).queryByText('p50')
59-
).not.toBeInTheDocument();
65+
const listbox = await screen.findByRole('listbox');
66+
expect(within(listbox).queryByText('p50')).not.toBeInTheDocument();
67+
68+
await waitFor(() => {
69+
expect(screen.getByRole('combobox')).toBeInTheDocument();
70+
});
6071
});
6172

6273
it.isKnownFlake('shows normal tags, e.g. transaction, in the dropdown', async () => {
@@ -81,14 +92,20 @@ describe('ResultsSearchQueryBuilder', () => {
8192
}
8293
);
8394

84-
// Check that a normal tag (e.g. "transaction") IS in the dropdown
95+
// Focus the input and type "transact" to simulate a search for transaction
8596
const input = await screen.findByRole('combobox');
86-
await userEvent.type(input, 'transact');
97+
await userEvent.click(input);
98+
await screen.findByRole('listbox');
99+
await userEvent.keyboard('transact', {delay: null});
87100

101+
// Check that a normal tag (e.g. "transaction") IS in the dropdown
102+
const listbox = await screen.findByRole('listbox');
88103
expect(
89-
await within(screen.getByRole('listbox')).findByRole('option', {
90-
name: 'transaction',
91-
})
104+
await within(listbox).findByRole('option', {name: 'transaction'})
92105
).toBeInTheDocument();
106+
107+
await waitFor(() => {
108+
expect(screen.getByRole('combobox')).toBeInTheDocument();
109+
});
93110
});
94111
});

0 commit comments

Comments
 (0)