Skip to content

Commit

Permalink
Interactivity API: Fix context inheritance from namespaces different …
Browse files Browse the repository at this point in the history
…than the current one (WordPress#64677)

* Add failing test

* Fix contextStack generation to include other namespaces

* Update changelog

Co-authored-by: DAreRodz <[email protected]>
Co-authored-by: luisherranz <[email protected]>
  • Loading branch information
3 people authored and bph committed Aug 31, 2024
1 parent 4b6be65 commit 20a059e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,24 @@
></span>
</div>
</div>


<div
data-testid="inheritance from other namespaces"
data-wp-interactive="directive-context/parent"
data-wp-context='{ "prop": "fromParentNs" }'
>
<div
data-wp-interactive="directive-context/child"
data-wp-context='{ "prop": "fromChildNs" }'
>
<span
data-testid="parent"
data-wp-text="directive-context/parent::context.prop"
></span>
<span
data-testid="child"
data-wp-text="directive-context/child::context.prop"
></span>
</div>
</div>
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

- Fix context inheritance from namespaces different than the current one ([#64677](https://github.com/WordPress/gutenberg/pull/64677)).

## 6.5.0 (2024-08-07)

### Enhancements
Expand Down
14 changes: 7 additions & 7 deletions packages/interactivity/src/directives.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,11 @@ export default () => {
const inheritedValue = useContext( inheritedContext );

const ns = defaultEntry!.namespace;
const currentValue = useRef( {
[ ns ]: proxifyState( ns, {} ),
} );
const currentValue = useRef( proxifyState( ns, {} ) );

// No change should be made if `defaultEntry` does not exist.
const contextStack = useMemo( () => {
const result = { ...inheritedValue };
if ( defaultEntry ) {
const { namespace, value } = defaultEntry;
// Check that the value is a JSON object. Send a console warning if not.
Expand All @@ -288,15 +287,16 @@ export default () => {
);
}
updateContext(
currentValue.current[ namespace ],
currentValue.current,
deepClone( value ) as object
);
currentValue.current[ namespace ] = proxifyContext(
currentValue.current[ namespace ],
currentValue.current = proxifyContext(
currentValue.current,
inheritedValue[ namespace ]
);
result[ namespace ] = currentValue.current;
}
return currentValue.current;
return result;
}, [ defaultEntry, inheritedValue ] );

return createElement( Provider, { value: contextStack }, children );
Expand Down
15 changes: 15 additions & 0 deletions test/e2e/specs/interactivity/directive-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,19 @@ test.describe( 'data-wp-context', () => {
expect( childContextAfter.obj2.prop6 ).toBe( 'child' );
expect( childContextAfter.obj2.overwritten ).toBeUndefined();
} );

test( 'properties from other namespaces defined in a parent context are inherited', async ( {
page,
} ) => {
const childProp = page
.getByTestId( 'inheritance from other namespaces' )
.getByTestId( 'child' );

const parentProp = page
.getByTestId( 'inheritance from other namespaces' )
.getByTestId( 'parent' );

await expect( childProp ).toHaveText( 'fromChildNs' );
await expect( parentProp ).toHaveText( 'fromParentNs' );
} );
} );

0 comments on commit 20a059e

Please sign in to comment.