Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky test of data-wp-on-window directive #58642

Merged
merged 3 commits into from Feb 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 21 additions & 12 deletions test/e2e/specs/interactivity/directive-on-window.spec.ts
Expand Up @@ -18,38 +18,47 @@ test.describe( 'data-wp-on-window', () => {
await utils.deleteAllPosts();
} );

test( 'callbacks should run whenever the specified event is dispatched', async ( {
page,
} ) => {
await page.setViewportSize( { width: 600, height: 600 } );
const counter = page.getByTestId( 'counter' );
await expect( counter ).toHaveText( '1' );
} );
test( 'the event listener is removed when the element is removed', async ( {
test( 'the event listener is attached when the element is added', async ( {
page,
} ) => {
const counter = page.getByTestId( 'counter' );
const isEventAttached = page.getByTestId( 'isEventAttached' );
const visibilityButton = page.getByTestId( 'visibility' );

// Initial value.
await expect( counter ).toHaveText( '0' );
await expect( isEventAttached ).toHaveText( 'yes' );

// Make sure the event listener is attached.
await page.waitForSelector(
'[data-testid="isEventAttached"]:has-text("yes")'
);
luisherranz marked this conversation as resolved.
Show resolved Hide resolved

// Change the viewport size.
await page.setViewportSize( { width: 600, height: 600 } );
await expect( counter ).toHaveText( '1' );

// Remove the element.
await visibilityButton.click();

// Make sure the event listener is not attached.
await page.waitForSelector(
'[data-testid="isEventAttached"]:has-text("no")'
);

// This resize should not increase the counter.
await page.setViewportSize( { width: 300, height: 600 } );

// Add the element back.
await visibilityButton.click();

// The counter should have the same value as before.
await expect( counter ).toHaveText( '1' );

// Wait until the effects run again.
await expect( isEventAttached ).toHaveText( 'yes' );
// Make sure the event listener is re-attached.
await page.waitForSelector(
'[data-testid="isEventAttached"]:has-text("yes")'
);

// This resize should increase the counter.
await page.setViewportSize( { width: 200, height: 600 } );
await expect( counter ).toHaveText( '2' );
} );
Expand Down