Skip to content

Commit 20a059e

Browse files
DAreRodzDAreRodzluisherranz
authored andcommitted
Interactivity API: Fix context inheritance from namespaces different 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]>
1 parent 4b6be65 commit 20a059e

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

packages/e2e-tests/plugins/interactive-blocks/directive-context/render.php

+21
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,24 @@
212212
></span>
213213
</div>
214214
</div>
215+
216+
217+
<div
218+
data-testid="inheritance from other namespaces"
219+
data-wp-interactive="directive-context/parent"
220+
data-wp-context='{ "prop": "fromParentNs" }'
221+
>
222+
<div
223+
data-wp-interactive="directive-context/child"
224+
data-wp-context='{ "prop": "fromChildNs" }'
225+
>
226+
<span
227+
data-testid="parent"
228+
data-wp-text="directive-context/parent::context.prop"
229+
></span>
230+
<span
231+
data-testid="child"
232+
data-wp-text="directive-context/child::context.prop"
233+
></span>
234+
</div>
235+
</div>

packages/interactivity/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Bug Fixes
6+
7+
- Fix context inheritance from namespaces different than the current one ([#64677](https://github.com/WordPress/gutenberg/pull/64677)).
8+
59
## 6.5.0 (2024-08-07)
610

711
### Enhancements

packages/interactivity/src/directives.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,11 @@ export default () => {
273273
const inheritedValue = useContext( inheritedContext );
274274

275275
const ns = defaultEntry!.namespace;
276-
const currentValue = useRef( {
277-
[ ns ]: proxifyState( ns, {} ),
278-
} );
276+
const currentValue = useRef( proxifyState( ns, {} ) );
279277

280278
// No change should be made if `defaultEntry` does not exist.
281279
const contextStack = useMemo( () => {
280+
const result = { ...inheritedValue };
282281
if ( defaultEntry ) {
283282
const { namespace, value } = defaultEntry;
284283
// Check that the value is a JSON object. Send a console warning if not.
@@ -288,15 +287,16 @@ export default () => {
288287
);
289288
}
290289
updateContext(
291-
currentValue.current[ namespace ],
290+
currentValue.current,
292291
deepClone( value ) as object
293292
);
294-
currentValue.current[ namespace ] = proxifyContext(
295-
currentValue.current[ namespace ],
293+
currentValue.current = proxifyContext(
294+
currentValue.current,
296295
inheritedValue[ namespace ]
297296
);
297+
result[ namespace ] = currentValue.current;
298298
}
299-
return currentValue.current;
299+
return result;
300300
}, [ defaultEntry, inheritedValue ] );
301301

302302
return createElement( Provider, { value: contextStack }, children );

test/e2e/specs/interactivity/directive-context.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,19 @@ test.describe( 'data-wp-context', () => {
380380
expect( childContextAfter.obj2.prop6 ).toBe( 'child' );
381381
expect( childContextAfter.obj2.overwritten ).toBeUndefined();
382382
} );
383+
384+
test( 'properties from other namespaces defined in a parent context are inherited', async ( {
385+
page,
386+
} ) => {
387+
const childProp = page
388+
.getByTestId( 'inheritance from other namespaces' )
389+
.getByTestId( 'child' );
390+
391+
const parentProp = page
392+
.getByTestId( 'inheritance from other namespaces' )
393+
.getByTestId( 'parent' );
394+
395+
await expect( childProp ).toHaveText( 'fromChildNs' );
396+
await expect( parentProp ).toHaveText( 'fromParentNs' );
397+
} );
383398
} );

0 commit comments

Comments
 (0)