Skip to content

Commit

Permalink
Use explicit wait for the animated elements
Browse files Browse the repository at this point in the history
  • Loading branch information
WunderBart committed Aug 24, 2022
1 parent b12a396 commit 6abc644
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
22 changes: 17 additions & 5 deletions test/storybook-playwright/specs/font-size-picker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ import { test, expect } from '@wordpress/e2e-test-utils-playwright';
/**
* Internal dependencies
*/
import { gotoStoryId, waitForFMAnimation } from '../utils';
import { gotoStoryId } from '../utils';

const waitUntilButtonHighlightStable = async ( page ) => {
const handle = await page
.locator( '[aria-label="Font size"] > div[role=presentation]' )
.elementHandle();

await handle?.waitForElementState( 'visible' );
await handle?.waitForElementState( 'stable' );

return handle;
};

test.describe.parallel( 'FontSizePicker', () => {
test.beforeEach( async ( { page } ) => {
Expand All @@ -16,7 +27,8 @@ test.describe.parallel( 'FontSizePicker', () => {
test( 'Renders with "Normal" size by default', async ( { page } ) => {
const button = await page.locator( 'button[aria-label="Normal"]' );

await waitForFMAnimation( { page } );
await waitUntilButtonHighlightStable( page );

expect( button ).toHaveAttribute( 'aria-checked', 'true' );
expect( await page.screenshot() ).toMatchSnapshot();
} );
Expand All @@ -25,8 +37,8 @@ test.describe.parallel( 'FontSizePicker', () => {
const button = await page.locator( 'button[aria-label="Small"]' );

await button.click();
await waitUntilButtonHighlightStable( page );

await waitForFMAnimation( { page } );
expect( button ).toHaveAttribute( 'aria-checked', 'true' );
expect( await page.screenshot() ).toMatchSnapshot();
} );
Expand All @@ -35,8 +47,8 @@ test.describe.parallel( 'FontSizePicker', () => {
const button = await page.locator( 'button[aria-label="Big"]' );

await button.click();
await waitUntilButtonHighlightStable( page );

await waitForFMAnimation( { page } );
expect( button ).toHaveAttribute( 'aria-checked', 'true' );
expect( await page.screenshot() ).toMatchSnapshot();
} );
Expand All @@ -45,8 +57,8 @@ test.describe.parallel( 'FontSizePicker', () => {
const button = await page.locator( 'button[aria-label="Normal"]' );

await button.click();
await waitUntilButtonHighlightStable( page );

await waitForFMAnimation( { page } );
expect( button ).toHaveAttribute( 'aria-checked', 'true' );
expect( await page.screenshot() ).toMatchSnapshot();
} );
Expand Down
5 changes: 3 additions & 2 deletions test/storybook-playwright/specs/popover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import { test, expect } from '@wordpress/e2e-test-utils-playwright';
/**
* Internal dependencies
*/
import { gotoStoryId, waitForFMAnimation } from '../utils';
import { gotoStoryId } from '../utils';

test.describe( 'Popover', () => {
test( 'should render', async ( { page } ) => {
await gotoStoryId( page, 'components-popover--default' );

await page.click( 'role=button' );
const popover = await page.waitForSelector( '.components-popover' );
await popover.waitForElementState( 'stable' );

await waitForFMAnimation( { page } );
expect( await page.screenshot() ).toMatchSnapshot();
} );
} );
23 changes: 0 additions & 23 deletions test/storybook-playwright/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,3 @@ export const gotoStoryId = ( page: Page, storyId: string ) =>
`http://localhost:${ STORYBOOK_PORT }/iframe.html?id=${ storyId }`,
{ waitUntil: 'load' }
);

export const waitForFMAnimation = async ( { page } ) =>
await page.evaluate(
() =>
new Promise( ( resolve, reject ) => {
const rootEl = document.getElementById( 'root' ) as HTMLElement;
if ( ! rootEl ) {
reject( 'Could not find #root element' );
}

let mutationTimer = setTimeout( resolve, 200 );
const observer = new MutationObserver( () => {
clearTimeout( mutationTimer );
mutationTimer = setTimeout( resolve, 200 );
} );

observer.observe( rootEl, {
subtree: true,
childList: true,
attributes: true,
} );
} )
);

0 comments on commit 6abc644

Please sign in to comment.