-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interactivity: Ensure stores are initialized on client (#59842)
If a store call happens after interactivity has hydrated, subscriptions may not be made correctly which means that when a store is later added, the subscriptions do not exist and the client will not update. This is undesirable. We fix this by creating empty stores during directive resolution, ensuring subscriptions can be made so that subsequent store changes will update the client.
- Loading branch information
Showing
8 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/e2e-tests/plugins/interactive-blocks/deferred-store/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 2, | ||
"name": "test/deferred-store", | ||
"title": "E2E Interactivity tests - deferred store", | ||
"category": "text", | ||
"icon": "heart", | ||
"description": "", | ||
"supports": { | ||
"interactivity": true | ||
}, | ||
"textdomain": "e2e-interactivity", | ||
"viewScriptModule": "file:./view.js", | ||
"render": "file:./render.php" | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/e2e-tests/plugins/interactive-blocks/deferred-store/render.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
/** | ||
* HTML for testing scope restoration with generators. | ||
* | ||
* @package gutenberg-test-interactive-blocks | ||
*/ | ||
?> | ||
|
||
<div | ||
data-wp-interactive="test/deferred-store" | ||
<?php echo wp_interactivity_data_wp_context( array( 'text' => '!dlrow ,olleH' ) ); ?> | ||
> | ||
<span data-wp-text="state.reversedText" data-testid="result"></span> | ||
<span data-wp-text="state.reversedTextGetter" data-testid="result-getter"></span> | ||
</div> |
1 change: 1 addition & 0 deletions
1
packages/e2e-tests/plugins/interactive-blocks/deferred-store/view.asset.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php return array( 'dependencies' => array( '@wordpress/interactivity' ) ); |
20 changes: 20 additions & 0 deletions
20
packages/e2e-tests/plugins/interactive-blocks/deferred-store/view.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { store, getContext } from '@wordpress/interactivity'; | ||
|
||
document.addEventListener( 'DOMContentLoaded', () => { | ||
setTimeout( () => { | ||
store( 'test/deferred-store', { | ||
state: { | ||
reversedText() { | ||
return [ ...getContext().text ].reverse().join( '' ); | ||
}, | ||
|
||
get reversedTextGetter() { | ||
return [ ...getContext().text ].reverse().join( '' ); | ||
}, | ||
}, | ||
} ); | ||
}, 50 ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { test, expect } from './fixtures'; | ||
|
||
test.describe( 'deferred store', () => { | ||
test.beforeAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.activatePlugins(); | ||
await utils.addPostWithBlock( 'test/deferred-store' ); | ||
} ); | ||
test.beforeEach( async ( { interactivityUtils: utils, page } ) => { | ||
await page.goto( utils.getLink( 'test/deferred-store' ) ); | ||
} ); | ||
test.afterAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.deactivatePlugins(); | ||
await utils.deleteAllPosts(); | ||
} ); | ||
|
||
test( 'Ensure that a store can be subscribed to before it is initialized', async ( { | ||
page, | ||
} ) => { | ||
const resultInput = page.getByTestId( 'result' ); | ||
await expect( resultInput ).toHaveText( '' ); | ||
await expect( resultInput ).toHaveText( 'Hello, world!' ); | ||
} ); | ||
|
||
// There is a known issue for deferred getters right now. | ||
// eslint-disable-next-line playwright/no-skipped-test | ||
test.skip( 'Ensure that a state getter can be subscribed to before it is initialized', async ( { | ||
page, | ||
} ) => { | ||
const resultInput = page.getByTestId( 'result-getter' ); | ||
await expect( resultInput ).toHaveText( '' ); | ||
await expect( resultInput ).toHaveText( 'Hello, world!' ); | ||
} ); | ||
} ); |