-
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: Restore scope on thrown exception in async callback (#…
…59708) Ensure scope is properly restored when an exception is thrown in a generator callback. For example, the following block should have the correct context in the catch block: try { yield Promise.reject( '💥' ); } catch ( err ) { getContext().error = err; } --- Co-authored-by: sirreal <[email protected]> Co-authored-by: DAreRodz <[email protected]> Co-authored-by: gziolo <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: anton-vlasenko <[email protected]> # Conflicts: # packages/interactivity/CHANGELOG.md
- Loading branch information
Showing
8 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/e2e-tests/plugins/interactive-blocks/generator-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/generator-scope", | ||
"title": "E2E Interactivity tests - generator scope", | ||
"category": "text", | ||
"icon": "heart", | ||
"description": "", | ||
"supports": { | ||
"interactivity": true | ||
}, | ||
"textdomain": "e2e-interactivity", | ||
"viewScriptModule": "file:./view.js", | ||
"render": "file:./render.php" | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/e2e-tests/plugins/interactive-blocks/generator-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,16 @@ | ||
<?php | ||
/** | ||
* HTML for testing scope restoration with generators. | ||
* | ||
* @package gutenberg-test-interactive-blocks | ||
*/ | ||
?> | ||
|
||
<div | ||
data-wp-interactive="test/generator-scope" | ||
<?php echo wp_interactivity_data_wp_context( array( 'result' => '' ) ); ?> | ||
> | ||
<input readonly data-wp-bind--value="context.result" data-testid="result" /> | ||
<button type="button" data-wp-on--click="callbacks.resolve" data-testid="resolve">Async resolve</button> | ||
<button type="button" data-wp-on--click="callbacks.reject" data-testid="reject">Async reject</button> | ||
</div> |
1 change: 1 addition & 0 deletions
1
packages/e2e-tests/plugins/interactive-blocks/generator-scope/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' ) ); |
23 changes: 23 additions & 0 deletions
23
packages/e2e-tests/plugins/interactive-blocks/generator-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,23 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { store, getContext } from '@wordpress/interactivity'; | ||
|
||
store( 'test/generator-scope', { | ||
callbacks: { | ||
*resolve() { | ||
try { | ||
getContext().result = yield Promise.resolve( 'ok' ); | ||
} catch ( err ) { | ||
getContext().result = err.toString(); | ||
} | ||
}, | ||
*reject() { | ||
try { | ||
getContext().result = yield Promise.reject( new Error( '😘' ) ); | ||
} catch ( err ) { | ||
getContext().result = err.toString(); | ||
} | ||
}, | ||
}, | ||
} ); |
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,31 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { test, expect } from './fixtures'; | ||
|
||
test.describe( 'async actions', () => { | ||
test.beforeAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.activatePlugins(); | ||
await utils.addPostWithBlock( 'test/generator-scope' ); | ||
} ); | ||
test.beforeEach( async ( { interactivityUtils: utils, page } ) => { | ||
await page.goto( utils.getLink( 'test/generator-scope' ) ); | ||
} ); | ||
test.afterAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.deactivatePlugins(); | ||
await utils.deleteAllPosts(); | ||
} ); | ||
|
||
test( 'Promise generator callbacks should restore scope on resolve and reject', async ( { | ||
page, | ||
} ) => { | ||
const resultInput = page.getByTestId( 'result' ); | ||
await expect( resultInput ).toHaveValue( '' ); | ||
|
||
await page.getByTestId( 'resolve' ).click(); | ||
await expect( resultInput ).toHaveValue( 'ok' ); | ||
|
||
await page.getByTestId( 'reject' ).click(); | ||
await expect( resultInput ).toHaveValue( 'Error: 😘' ); | ||
} ); | ||
} ); |