Skip to content

Commit

Permalink
Fix: Flakey deferred store test (WordPress#62571)
Browse files Browse the repository at this point in the history
* Use events to proceed test when ready

* Initialize once on the event

* Use window not globalThis in test

* Fix event dispatch in page

Co-authored-by: sirreal <jonsurrell@git.wordpress.org>
Co-authored-by: cbravobernal <cbravobernal@git.wordpress.org>
  • Loading branch information
3 people authored and patil-vipul committed Jun 17, 2024
1 parent 3c52da7 commit a7426a8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
*/
import { store, getContext } from '@wordpress/interactivity';

document.addEventListener( 'DOMContentLoaded', () => {
setTimeout( () => {
window.addEventListener(
'_test_proceed_',
() => {
store( 'test/deferred-store', {
state: {
reversedText() {
Expand All @@ -16,5 +17,6 @@ document.addEventListener( 'DOMContentLoaded', () => {
},
},
} );
}, 100 );
} );
},
{ once: true }
);
6 changes: 6 additions & 0 deletions test/e2e/specs/interactivity/deferred-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ test.describe( 'deferred store', () => {
} ) => {
const resultInput = page.getByTestId( 'result' );
await expect( resultInput ).toHaveText( '' );
await page.evaluate( () => {
window.dispatchEvent( new Event( '_test_proceed_' ) );
} );
await expect( resultInput ).toHaveText( 'Hello, world!' );
} );

Expand All @@ -31,6 +34,9 @@ test.describe( 'deferred store', () => {
} ) => {
const resultInput = page.getByTestId( 'result-getter' );
await expect( resultInput ).toHaveText( '' );
await page.evaluate( () => {
window.dispatchEvent( new Event( '_test_proceed_' ) );
} );
await expect( resultInput ).toHaveText( 'Hello, world!' );
} );
} );

0 comments on commit a7426a8

Please sign in to comment.