-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interactivity API: Prevent empty namespace or different namespaces fr…
…om killing the runtime (#61409) * Prevent empty namespace or different namespaces from killing the runtime * Update changelog * Move changelog * Update with isdebug, rephrase error * Prevent warning duplication * Remove not needed check * Move warn to same resolve function * Add some tests * Remove not needed warn * Fix typos Co-authored-by: cbravobernal <cbravobernal@git.wordpress.org> Co-authored-by: sirreal <jonsurrell@git.wordpress.org> Co-authored-by: luisherranz <luisherranz@git.wordpress.org> Co-authored-by: DAreRodz <darerodz@git.wordpress.org> Co-authored-by: michalczaplinski <czapla@git.wordpress.org>
- Loading branch information
1 parent
298fa5c
commit c70ea83
Showing
11 changed files
with
144 additions
and
20 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/namespace/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-namespace/directive-bind", | ||
"title": "E2E Interactivity tests - directive bind", | ||
"category": "text", | ||
"icon": "heart", | ||
"description": "", | ||
"supports": { | ||
"interactivity": true | ||
}, | ||
"textdomain": "e2e-interactivity", | ||
"viewScriptModule": "file:./view.js", | ||
"render": "file:./render.php" | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/e2e-tests/plugins/interactive-blocks/namespace/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,23 @@ | ||
<?php | ||
/** | ||
* HTML for testing the directive `data-wp-bind`. | ||
* | ||
* @package gutenberg-test-interactive-blocks | ||
*/ | ||
?> | ||
|
||
<div data-wp-interactive=""> | ||
<a data-wp-bind--href="state.url" data-testid="empty namespace"></a> | ||
</div> | ||
|
||
<div data-wp-interactive="namespace"> | ||
<a data-wp-bind--href="state.url" data-testid="correct namespace"></a> | ||
</div> | ||
|
||
<div data-wp-interactive="{}"> | ||
<a data-wp-bind--href="state.url" data-testid="object namespace"></a> | ||
</div> | ||
|
||
<div data-wp-interactive> | ||
<a data-wp-bind--href="other::state.url" data-testid="other namespace"></a> | ||
</div> |
1 change: 1 addition & 0 deletions
1
packages/e2e-tests/plugins/interactive-blocks/namespace/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' ) ); |
16 changes: 16 additions & 0 deletions
16
packages/e2e-tests/plugins/interactive-blocks/namespace/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,16 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { store } from '@wordpress/interactivity'; | ||
|
||
store( 'namespace', { | ||
state: { | ||
url: '/some-url', | ||
}, | ||
} ); | ||
|
||
store( 'other', { | ||
state: { | ||
url: '/other-store-url', | ||
}, | ||
} ); |
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,21 @@ | ||
const logged = new Set(); | ||
|
||
export const warn = ( message ) => { | ||
// @ts-expect-error | ||
if ( typeof SCRIPT_DEBUG !== 'undefined' && SCRIPT_DEBUG === true ) { | ||
if ( logged.has( message ) ) { | ||
return; | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.warn( message ); | ||
|
||
// Adding a stack trace to the warning message to help with debugging. | ||
try { | ||
throw Error( message ); | ||
} catch ( e ) { | ||
// Do nothing. | ||
} | ||
logged.add( message ); | ||
} | ||
}; |
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,49 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { test, expect } from './fixtures'; | ||
|
||
test.describe( 'Namespaces', () => { | ||
test.beforeAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.activatePlugins(); | ||
await utils.addPostWithBlock( 'test-namespace/directive-bind' ); | ||
} ); | ||
|
||
test.beforeEach( async ( { interactivityUtils: utils, page } ) => { | ||
await page.goto( utils.getLink( 'test-namespace/directive-bind' ) ); | ||
} ); | ||
|
||
test.afterAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.deactivatePlugins(); | ||
await utils.deleteAllPosts(); | ||
} ); | ||
|
||
test( 'Empty string as namespace should not work', async ( { page } ) => { | ||
const el = page.getByTestId( 'empty namespace' ); | ||
await expect( el ).not.toHaveAttribute( 'href', '/some-url' ); | ||
} ); | ||
|
||
test( 'A string as namespace should work', async ( { page } ) => { | ||
const el = page.getByTestId( 'correct namespace' ); | ||
await expect( el ).toHaveAttribute( 'href', '/some-url' ); | ||
} ); | ||
|
||
test( 'An empty object as namespace should work', async ( { page } ) => { | ||
const el = page.getByTestId( 'object namespace' ); | ||
await expect( el ).not.toHaveAttribute( 'href', '/some-url' ); | ||
} ); | ||
|
||
test( 'A wrong namespace should not break the runtime', async ( { | ||
page, | ||
} ) => { | ||
const el = page.getByTestId( 'object namespace' ); | ||
await expect( el ).not.toHaveAttribute( 'href', '/some-url' ); | ||
const correct = page.getByTestId( 'correct namespace' ); | ||
await expect( correct ).toHaveAttribute( 'href', '/some-url' ); | ||
} ); | ||
|
||
test( 'A different store namespace should work', async ( { page } ) => { | ||
const el = page.getByTestId( 'other namespace' ); | ||
await expect( el ).toHaveAttribute( 'href', '/other-store-url' ); | ||
} ); | ||
} ); |