Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactivity API: Only add proxies to plain objects inside the store #59039

Merged
merged 6 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/e2e-tests/plugins/interactive-blocks/store/block.json
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/store",
"title": "E2E Interactivity tests - store definition",
"category": "text",
"icon": "heart",
"description": "",
"supports": {
"interactivity": true
},
"textdomain": "e2e-interactivity",
"viewScript": "store-view",
"render": "file:./render.php"
}
17 changes: 17 additions & 0 deletions packages/e2e-tests/plugins/interactive-blocks/store/render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* HTML for testing the directive `data-wp-bind`.
*
* @package gutenberg-test-interactive-blocks
*/

wp_enqueue_script_module( 'store-view' );
?>

<div data-wp-interactive="test/store">
<div
data-testid="non-plain object"
data-wp-text="state.isNotProxified"
data-wp-init="callbacks.init"
></div>
</div>
20 changes: 20 additions & 0 deletions packages/e2e-tests/plugins/interactive-blocks/store/view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import { store, getElement } from '@wordpress/interactivity';


const { state } = store( 'test/store', {
state: {
get isNotProxified() {
const { ref } = getElement();
return state.elementRef === ref;
}
},
callbacks: {
init() {
const { ref } = getElement();
state.elementRef = ref; // HTMLElement
}
}
} )
4 changes: 4 additions & 0 deletions packages/interactivity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fixes

- Only add proxies to plain objects inside the store. ([#59039](https://github.com/WordPress/gutenberg/pull/59039))

## 5.0.0 (2024-02-09)

### New Features
Expand Down
4 changes: 2 additions & 2 deletions packages/interactivity/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
resetNamespace,
} from './hooks';

const isObject = ( item: unknown ): boolean =>
!! item && typeof item === 'object' && ! Array.isArray( item );
const isObject = ( item: unknown ): item is Record< string, unknown > =>
item && typeof item === 'object' && item.constructor === Object;

const deepMerge = ( target: any, source: any ) => {
if ( isObject( target ) && isObject( source ) ) {
Expand Down
25 changes: 25 additions & 0 deletions test/e2e/specs/interactivity/store.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Internal dependencies
*/
import { test, expect } from './fixtures';

test.describe( 'data-wp-bind', () => {
test.beforeAll( async ( { interactivityUtils: utils } ) => {
await utils.activatePlugins();
await utils.addPostWithBlock( 'test/store' );
} );

test.beforeEach( async ( { interactivityUtils: utils, page } ) => {
await page.goto( utils.getLink( 'test/store' ) );
} );

test.afterAll( async ( { interactivityUtils: utils } ) => {
await utils.deactivatePlugins();
await utils.deleteAllPosts();
} );

test( 'non-plain objects are not proxified', async ( { page } ) => {
const el = page.getByTestId( 'non-plain object' );
await expect( el ).toHaveText( 'true' );
} );
} );
Loading