diff --git a/test/e2e/specs/interactivity/directive-on-window.spec.ts b/test/e2e/specs/interactivity/directive-on-window.spec.ts index dd3a02e0bb400..3afd1a9942fa8 100644 --- a/test/e2e/specs/interactivity/directive-on-window.spec.ts +++ b/test/e2e/specs/interactivity/directive-on-window.spec.ts @@ -18,38 +18,50 @@ 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 + .getByTestId( 'isEventAttached' ) + .filter( { hasText: 'yes' } ) + .waitFor(); + + // 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 + .getByTestId( 'isEventAttached' ) + .filter( { hasText: 'no' } ) + .waitFor(); + // 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 + .getByTestId( 'isEventAttached' ) + .filter( { hasText: 'yes' } ) + .waitFor(); + // This resize should increase the counter. await page.setViewportSize( { width: 200, height: 600 } ); await expect( counter ).toHaveText( '2' ); } );