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: Fix context inheritance from namespaces different than the current one #64677

Merged
merged 3 commits into from
Aug 21, 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
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' );
} );
} );
Loading