-
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.
…65151) * Expose client and server context from provider * Create `getServerContext` * Add simple test for server context * Implement `getServerState` * Add tests for read-only state proxies * Add e2e tests for `getServerState()` * Avoid PHPCS UndefinedVariable error * Add e2e tests for `getServerContext` WIP * Finish e2e tests for `getServerContext()` * Update `getServerState()` tests * Revert "Add simple test for server context" This reverts commit 7e6f530. * Update TSDocs Co-authored-by: DAreRodz <[email protected]> Co-authored-by: luisherranz <[email protected]> Co-authored-by: sethrubenstein <[email protected]> Co-authored-by: michalczaplinski <[email protected]>
- Loading branch information
1 parent
eb715c2
commit 001241b
Showing
18 changed files
with
858 additions
and
25 deletions.
There are no files selected for viewing
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
15 changes: 15 additions & 0 deletions
15
packages/e2e-tests/plugins/interactive-blocks/get-server-context/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/get-server-context", | ||
"title": "E2E Interactivity tests - getServerContext", | ||
"category": "text", | ||
"icon": "heart", | ||
"description": "", | ||
"supports": { | ||
"interactivity": true | ||
}, | ||
"textdomain": "e2e-interactivity", | ||
"viewScriptModule": "file:./view.js", | ||
"render": "file:./render.php" | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/e2e-tests/plugins/interactive-blocks/get-server-context/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,51 @@ | ||
<?php | ||
/** | ||
* HTML for testing the getServerContext() function. | ||
* | ||
* @package gutenberg-test-interactive-blocks | ||
* | ||
* @phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable | ||
*/ | ||
|
||
$link1 = $attributes['links']['modified']; | ||
$link2 = $attributes['links']['newProps']; | ||
$parent_ctx = $attributes['parentContext']; | ||
$child_ctx = $attributes['childContext']; | ||
?> | ||
|
||
<nav | ||
data-testid="navigate" | ||
data-wp-interactive="test/get-server-context" | ||
data-wp-on--click="actions.navigate" | ||
> | ||
<a data-testid="modified" href="<?php echo esc_url( $link1 ); ?>">modified</a> | ||
<a data-testid="newProps" href="<?php echo esc_url( $link2 ); ?>">newProps</a> | ||
</nav> | ||
|
||
<div | ||
data-wp-interactive="test/get-server-context" | ||
data-wp-router-region="server-context" | ||
data-wp-watch="callbacks.updateServerContextParent" | ||
<?php echo wp_interactivity_data_wp_context( $parent_ctx ); ?> | ||
> | ||
<div | ||
data-wp-watch="callbacks.updateServerContextChild" | ||
<?php echo wp_interactivity_data_wp_context( $child_ctx ); ?> | ||
> | ||
<div data-testid="prop" data-wp-text="context.prop"></div> | ||
<div data-testid="nested.prop" data-wp-text="context.nested.prop"></div> | ||
<div data-testid="newProp" data-wp-text="context.newProp"></div> | ||
<div data-testid="nested.newProp" data-wp-text="context.nested.newProp"></div> | ||
<div data-testid="inherited.prop" data-wp-text="context.inherited.prop"></div> | ||
<div data-testid="inherited.newProp" data-wp-text="context.inherited.newProp"></div> | ||
|
||
<button | ||
data-testid="tryToModifyServerContext" | ||
<?php echo wp_interactivity_data_wp_context( array( 'result' => 'modify' ) ); ?> | ||
data-wp-on--click="actions.attemptModification" | ||
data-wp-text="context.result"> | ||
> | ||
modify | ||
</button> | ||
</div> | ||
</div> |
9 changes: 9 additions & 0 deletions
9
packages/e2e-tests/plugins/interactive-blocks/get-server-context/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,9 @@ | ||
<?php return array( | ||
'dependencies' => array( | ||
'@wordpress/interactivity', | ||
array( | ||
'id' => '@wordpress/interactivity-router', | ||
'import' => 'dynamic', | ||
), | ||
), | ||
); |
46 changes: 46 additions & 0 deletions
46
packages/e2e-tests/plugins/interactive-blocks/get-server-context/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,46 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { store, getContext, getServerContext } from '@wordpress/interactivity'; | ||
|
||
store( 'test/get-server-context', { | ||
actions: { | ||
*navigate( e ) { | ||
e.preventDefault(); | ||
const { actions } = yield import( | ||
'@wordpress/interactivity-router' | ||
); | ||
yield actions.navigate( e.target.href ); | ||
}, | ||
attemptModification() { | ||
try { | ||
getServerContext().prop = 'updated from client'; | ||
getContext().result = 'unexpectedly modified ❌'; | ||
} catch ( e ) { | ||
getContext().result = 'not modified ✅'; | ||
} | ||
}, | ||
}, | ||
callbacks: { | ||
updateServerContextParent() { | ||
const ctx = getContext(); | ||
const { prop, newProp, nested, inherited } = getServerContext(); | ||
ctx.prop = prop; | ||
ctx.newProp = newProp; | ||
ctx.nested.prop = nested.prop; | ||
ctx.nested.newProp = nested.newProp; | ||
ctx.inherited.prop = inherited.prop; | ||
ctx.inherited.newProp = inherited.newProp; | ||
}, | ||
updateServerContextChild() { | ||
const ctx = getContext(); | ||
const { prop, newProp, nested, inherited } = getServerContext(); | ||
ctx.prop = prop; | ||
ctx.newProp = newProp; | ||
ctx.nested.prop = nested.prop; | ||
ctx.nested.newProp = nested.newProp; | ||
ctx.inherited.prop = inherited.prop; | ||
ctx.inherited.newProp = inherited.newProp; | ||
}, | ||
}, | ||
} ); |
15 changes: 15 additions & 0 deletions
15
packages/e2e-tests/plugins/interactive-blocks/get-server-state/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/get-server-state", | ||
"title": "E2E Interactivity tests - getServerState", | ||
"category": "text", | ||
"icon": "heart", | ||
"description": "", | ||
"supports": { | ||
"interactivity": true | ||
}, | ||
"textdomain": "e2e-interactivity", | ||
"viewScriptModule": "file:./view.js", | ||
"render": "file:./render.php" | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/e2e-tests/plugins/interactive-blocks/get-server-state/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,50 @@ | ||
<?php | ||
/** | ||
* HTML for testing the getServerState() function. | ||
* | ||
* @package gutenberg-test-interactive-blocks | ||
* | ||
* @phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable | ||
*/ | ||
|
||
if ( isset( $attributes['state'] ) ) { | ||
wp_interactivity_state( 'test/get-server-state', $attributes['state'] ); | ||
} | ||
?> | ||
|
||
<div | ||
data-wp-interactive="test/get-server-state" | ||
data-wp-watch="callbacks.updateState" | ||
> | ||
<div data-testid="prop" data-wp-text="state.prop"></div> | ||
<div data-testid="nested.prop" data-wp-text="state.nested.prop"></div> | ||
<div data-testid="newProp" data-wp-text="state.newProp"></div> | ||
<div data-testid="nested.newProp" data-wp-text="state.nested.newProp"></div> | ||
|
||
<button | ||
data-testid="tryToModifyServerState" | ||
<?php echo wp_interactivity_data_wp_context( array( 'result' => 'modify' ) ); ?> | ||
data-wp-on--click="actions.attemptModification" | ||
data-wp-text="context.result"> | ||
> | ||
modify | ||
</button> | ||
|
||
|
||
<nav> | ||
<?php | ||
if ( isset( $attributes['links'] ) ) { | ||
foreach ( $attributes['links'] as $key => $link ) { | ||
$i = $key += 1; | ||
echo <<<HTML | ||
<a | ||
data-testid="link $i" | ||
data-wp-on--click="actions.navigate" | ||
href="$link" | ||
>link $i</a> | ||
HTML; | ||
} | ||
} | ||
?> | ||
</nav> | ||
</div> |
9 changes: 9 additions & 0 deletions
9
packages/e2e-tests/plugins/interactive-blocks/get-server-state/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,9 @@ | ||
<?php return array( | ||
'dependencies' => array( | ||
'@wordpress/interactivity', | ||
array( | ||
'id' => '@wordpress/interactivity-router', | ||
'import' => 'dynamic', | ||
), | ||
), | ||
); |
33 changes: 33 additions & 0 deletions
33
packages/e2e-tests/plugins/interactive-blocks/get-server-state/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,33 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { store, getServerState, getContext } from '@wordpress/interactivity'; | ||
|
||
const { state } = store( 'test/get-server-state', { | ||
actions: { | ||
*navigate( e ) { | ||
e.preventDefault(); | ||
const { actions } = yield import( | ||
'@wordpress/interactivity-router' | ||
); | ||
yield actions.navigate( e.target.href ); | ||
}, | ||
attemptModification() { | ||
try { | ||
getServerState().prop = 'updated from client'; | ||
getContext().result = 'unexpectedly modified ❌'; | ||
} catch ( e ) { | ||
getContext().result = 'not modified ✅'; | ||
} | ||
}, | ||
}, | ||
callbacks: { | ||
updateState() { | ||
const { prop, newProp, nested } = getServerState(); | ||
state.prop = prop; | ||
state.newProp = newProp; | ||
state.nested.prop = nested.prop; | ||
state.nested.newProp = nested.newProp; | ||
}, | ||
}, | ||
} ); |
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
Oops, something went wrong.