Skip to content

Commit

Permalink
Tabs: Fix flaky unit tests (#58629)
Browse files Browse the repository at this point in the history
* waitFor controlled mode changes to finish rendering

* add missing assertion

* use `sleep()` to ensure rendering completion

Co-authored-by: chad1008 <shireling@git.wordpress.org>
Co-authored-by: ciampo <mciampini@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
Co-authored-by: diegohaz <hazdiego@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
  • Loading branch information
6 people authored Feb 14, 2024
1 parent b801b1c commit c35d6ff
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/components/src/tabs/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { render, screen, waitFor } from '@testing-library/react';
import { press, click } from '@ariakit/test';
import { press, click, sleep } from '@ariakit/test';

/**
* WordPress dependencies
Expand Down Expand Up @@ -1185,10 +1185,18 @@ describe( 'Tabs', () => {
/>
);

// Due to the timing of the component re-rendering, we
// need to force a delay to ensure the test doesn't run
// the upcoming assertions too early.
// see https://github.com/WordPress/gutenberg/pull/58629#issuecomment-1924875249
await sleep();

// Tab key should focus the currently selected tab, which is Beta.
await press.Tab();
expect( await getSelectedTab() ).toHaveTextContent(
'Beta'
await waitFor( async () =>
expect( await getSelectedTab() ).toHaveTextContent(
'Beta'
)
);
expect( await getSelectedTab() ).toHaveFocus();

Expand All @@ -1201,9 +1209,11 @@ describe( 'Tabs', () => {
);

// When the selected tab is changed, it should not automatically receive focus.

expect( await getSelectedTab() ).toHaveTextContent(
'Gamma'
);

expect(
screen.getByRole( 'tab', { name: 'Beta' } )
).toHaveFocus();
Expand Down Expand Up @@ -1247,9 +1257,11 @@ describe( 'Tabs', () => {
);

// When the selected tab is changed, it should not automatically receive focus.

expect( await getSelectedTab() ).toHaveTextContent(
'Gamma'
);

expect(
screen.getByRole( 'tab', { name: 'Beta' } )
).toHaveFocus();
Expand Down

1 comment on commit c35d6ff

@github-actions
Copy link

@github-actions github-actions bot commented on c35d6ff Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in c35d6ff.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7900617800
📝 Reported issues:

Please sign in to comment.