Skip to content

Commit

Permalink
💚 [#445] Fix flakiness in Chromatic/interaction tests
Browse files Browse the repository at this point in the history
Doing a click + ArrowDown on a react select seems to have a race
condition where sometimes the *next* option gets 'focus', resulting in
it being higlighted with a different color, which in turn trips the
Chromatic snapshots as it's toggling between which option is focused.

Replacing this with an explicit focus + ArrowDown causes the menu to
open and seems to avoid the race condition.

The click used to be required in older versions of testing library and
react-select to properly focus the input/select when the browser window
itself doesn't have focus, e.g. when live-reloading saved code in SB
which would lead to a frustrating DX.
  • Loading branch information
sergei-maertens committed Mar 6, 2025
1 parent 3bfe705 commit 715eda1
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/components/appointments/fields/LocationSelect.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export const MultipleCandidates = {
const canvas = within(canvasElement);
await expect(canvas.queryByText('Open Gem')).not.toBeInTheDocument();
const dropdown = canvas.getByLabelText('Locatie');
await userEvent.click(dropdown);
dropdown.focus();
await userEvent.keyboard('[ArrowDown]');
await expect(await canvas.findByText('Open Gem')).toBeVisible();
await expect(await canvas.findByText('Bahamas')).toBeVisible();
expect(await canvas.findByRole('option', {name: 'Open Gem'})).toBeVisible();
expect(await canvas.findByRole('option', {name: 'Bahamas'})).toBeVisible();
},
};

Expand All @@ -52,7 +52,7 @@ export const SingleCandidate = {
await expect(canvas.queryByText('Bahamas')).not.toBeInTheDocument();

const dropdown = canvas.getByLabelText('Locatie');
await userEvent.click(dropdown);
dropdown.focus();
await userEvent.keyboard('[ArrowDown]');

// wait for locations to be loaded
Expand All @@ -61,8 +61,8 @@ export const SingleCandidate = {
expect(textNodes.length).toBeGreaterThan(0);
});

await expect(canvas.queryByText('Bahamas')).not.toBeInTheDocument();
expect(canvas.queryByText('Bahamas')).not.toBeInTheDocument();
await userEvent.keyboard('[Escape]');
await expect(await canvas.findByText('Open Gem')).toBeVisible();
expect(await canvas.findByText('Open Gem')).toBeVisible();
},
};

0 comments on commit 715eda1

Please sign in to comment.