-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interactivity: Export
withScope()
and allow to use it with asynchro…
…nous operations. (#58013) * Initial commit * Commit to change branches * Support generators in with scope * Update changelog
- Loading branch information
1 parent
c35e95a
commit f4c34a4
Showing
9 changed files
with
121 additions
and
6 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/e2e-tests/plugins/interactive-blocks/with-scope/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/with-scope", | ||
"title": "E2E Interactivity tests - with scope", | ||
"category": "text", | ||
"icon": "heart", | ||
"description": "", | ||
"supports": { | ||
"interactivity": true | ||
}, | ||
"textdomain": "e2e-interactivity", | ||
"viewScript": "with-scope-view", | ||
"render": "file:./render.php" | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/e2e-tests/plugins/interactive-blocks/with-scope/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,14 @@ | ||
<?php | ||
/** | ||
* HTML for testing the directive `data-wp-on`. | ||
* | ||
* @package gutenberg-test-interactive-blocks | ||
*/ | ||
|
||
gutenberg_enqueue_module( 'with-scope-view' ); | ||
?> | ||
|
||
<div data-wp-interactive='{ "namespace": "with-scope" }' data-wp-context='{"asyncCounter": 0, "syncCounter": 0}' data-wp-init--a='callbacks.asyncInit' data-wp-init--b='callbacks.syncInit'> | ||
<p data-wp-text="context.asyncCounter" data-testid="asyncCounter">0</p> | ||
<p data-wp-text="context.syncCounter" data-testid="syncCounter">0</p> | ||
</div> |
20 changes: 20 additions & 0 deletions
20
packages/e2e-tests/plugins/interactive-blocks/with-scope/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, withScope } from '@wordpress/interactivity'; | ||
|
||
store( 'with-scope', { | ||
callbacks: { | ||
asyncInit: () => { | ||
setTimeout( withScope(function*() { | ||
yield new Promise(resolve => setTimeout(resolve, 1)); | ||
const context = getContext() | ||
context.asyncCounter += 1; | ||
}, 1 )); | ||
}, | ||
syncInit: () => { | ||
const context = getContext() | ||
context.syncCounter += 1; | ||
} | ||
}, | ||
} ); |
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
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,27 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { test, expect } from './fixtures'; | ||
|
||
test.describe( 'withScope', () => { | ||
test.beforeAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.activatePlugins(); | ||
await utils.addPostWithBlock( 'test/with-scope' ); | ||
} ); | ||
test.beforeEach( async ( { interactivityUtils: utils, page } ) => { | ||
await page.goto( utils.getLink( 'test/with-scope' ) ); | ||
} ); | ||
test.afterAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.deactivatePlugins(); | ||
await utils.deleteAllPosts(); | ||
} ); | ||
|
||
test( 'directives using withScope should work with async and sync functions', async ( { | ||
page, | ||
} ) => { | ||
const asyncCounter = page.getByTestId( 'asyncCounter' ); | ||
await expect( asyncCounter ).toHaveText( '1' ); | ||
const syncCounter = page.getByTestId( 'syncCounter' ); | ||
await expect( syncCounter ).toHaveText( '1' ); | ||
} ); | ||
} ); |