Skip to content

Commit

Permalink
Components: Remove unnecessary act() from Popover tests (#47690)
Browse files Browse the repository at this point in the history
* Components: Remove unnecessary act() from Popover tests

* No need to await for the focusing
  • Loading branch information
tyxla authored Feb 3, 2023
1 parent b1a6aaf commit 12791ec
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions packages/components/src/popover/test/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* External dependencies
*/
import { act, render, screen } from '@testing-library/react';
import type { CSSProperties, ReactElement } from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import type { CSSProperties } from 'react';

/**
* WordPress dependencies
Expand Down Expand Up @@ -91,25 +91,21 @@ const ALL_POSITIONS_TO_EXPECTED_PLACEMENTS: PositionToPlacementTuple[] = [

describe( 'Popover', () => {
describe( 'Component', () => {
// Render UI and then wait for the `floating-ui` effects inside `Popover` to finish running
// See also: https://floating-ui.com/docs/react-dom#testing
async function renderAsync( ui: ReactElement ) {
const view = render( ui );
await act( () => Promise.resolve() );
return view;
}

describe( 'basic behavior', () => {
it( 'should render content', async () => {
await renderAsync( <Popover>Hello</Popover> );
render( <Popover>Hello</Popover> );

expect( screen.getByText( 'Hello' ) ).toBeInTheDocument();
await waitFor( () =>
expect( screen.getByText( 'Hello' ) ).toBeVisible()
);
} );

it( 'should forward additional props to portaled element', async () => {
await renderAsync( <Popover role="tooltip">Hello</Popover> );
render( <Popover role="tooltip">Hello</Popover> );

expect( screen.getByRole( 'tooltip' ) ).toBeInTheDocument();
await waitFor( () =>
expect( screen.getByRole( 'tooltip' ) ).toBeVisible()
);
} );
} );

Expand All @@ -129,32 +125,44 @@ describe( 'Popover', () => {
);
};

await renderAsync(
render(
<PopoverWithAnchor>Popover content</PopoverWithAnchor>
);

expect(
screen.getByText( 'Popover content' )
).toBeInTheDocument();
await waitFor( () =>
expect(
screen.getByText( 'Popover content' )
).toBeVisible()
);
} );
} );

describe( 'focus behavior', () => {
it( 'should focus the popover by default when opened', async () => {
await renderAsync(
render(
<Popover data-testid="popover-element">
Popover content
</Popover>
);

expect( screen.getByTestId( 'popover-element' ) ).toHaveFocus();
const popover = screen.getByTestId( 'popover-element' );

await waitFor( () => expect( popover ).toBeVisible() );

expect( popover ).toHaveFocus();
} );

it( 'should allow focus-on-open behavior to be disabled', async () => {
await renderAsync(
render(
<Popover focusOnMount={ false }>Popover content</Popover>
);

await waitFor( () =>
expect(
screen.getByText( 'Popover content' )
).toBeVisible()
);

expect( document.body ).toHaveFocus();
} );
} );
Expand Down

1 comment on commit 12791ec

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 12791ec.
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/4083177353
📝 Reported issues:

Please sign in to comment.