Skip to content

Commit

Permalink
Block Editor: Wait for popover positioning in MediaReplaceFlow tests (
Browse files Browse the repository at this point in the history
#45863)

* Block Editor: Wait for popover positioning in MediaReplaceFlow tests

* Remove an extra backtick

* Use Element.closest() to locate closest popover

* Fix comment

* Remove an excess assertion

* Reorder assertions

* Introduce a less-specific way to assert positioning

* Use the right element when locating the popover

* Clarify element.style usage

* Wait for the menu to get visible
  • Loading branch information
tyxla committed Nov 21, 2022
1 parent 1906e39 commit bbc5489
Showing 1 changed file with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

/**
Expand Down Expand Up @@ -32,6 +32,34 @@ function TestWrapper() {
);
}

/**
* Returns the first found popover element up the DOM tree.
*
* @param {HTMLElement} element Element to start with.
* @return {HTMLElement|null} Popover element, or `null` if not found.
*/
function getWrappingPopoverElement( element ) {
return element.closest( '.components-popover' );
}

/**
* Asserts that the specified popover has already been positioned.
* Necessary because it will be positioned a bit later after it's displayed.
*
* We're intentionally not using `.toHaveStyle()` because we want to be
* less specific and avoid specific values for better test flexibility.
*
* @async
*
* @param {HTMLElement} popover Popover element.
*/
async function popoverIsPositioned( popover ) {
/* eslint-disable jest-dom/prefer-to-have-style */
await waitFor( () => expect( popover.style.top ).not.toBe( '' ) );
await waitFor( () => expect( popover.style.left ).not.toBe( '' ) );
/* eslint-enable jest-dom/prefer-to-have-style */
}

describe( 'General media replace flow', () => {
it( 'renders successfully', () => {
render( <TestWrapper /> );
Expand All @@ -57,11 +85,11 @@ describe( 'General media replace flow', () => {
name: 'Replace',
} )
);

const uploadMenu = screen.getByRole( 'menu' );

expect( uploadMenu ).toBeInTheDocument();
expect( uploadMenu ).not.toBeVisible();
await popoverIsPositioned( getWrappingPopoverElement( uploadMenu ) );

await waitFor( () => expect( uploadMenu ).toBeVisible() );
} );

it( 'displays media URL', async () => {
Expand All @@ -78,11 +106,13 @@ describe( 'General media replace flow', () => {
} )
);

expect(
screen.getByRole( 'link', {
name: 'example.media (opens in a new tab)',
} )
).toHaveAttribute( 'href', 'https://example.media' );
const link = screen.getByRole( 'link', {
name: 'example.media (opens in a new tab)',
} );

await popoverIsPositioned( getWrappingPopoverElement( link ) );

expect( link ).toHaveAttribute( 'href', 'https://example.media' );
} );

it( 'edits media URL', async () => {
Expand All @@ -99,6 +129,14 @@ describe( 'General media replace flow', () => {
} )
);

await popoverIsPositioned(
getWrappingPopoverElement(
screen.getByRole( 'link', {
name: 'example.media (opens in a new tab)',
} )
)
);

await user.click(
screen.getByRole( 'button', {
name: 'Edit',
Expand Down

0 comments on commit bbc5489

Please sign in to comment.