Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Element: Fix serializer handling of multiple distinct contexts
  • Loading branch information
aduth committed Mar 26, 2020
commit 6313fad188e25569ed6cb54035e99439fe910299
2 changes: 1 addition & 1 deletion packages/element/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ Serializes a React element to string.
_Parameters_

- _element_ `WPElement`: Element to serialize.
- _context_ `?Object`: Context object.
- _context_ `[Map]`: Context object.
- _legacyContext_ `?Object`: Legacy context object.

_Returns_
Expand Down
11 changes: 8 additions & 3 deletions packages/element/src/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ function getNormalStylePropertyValue( property, value ) {
* Serializes a React element to string.
*
* @param {WPElement} element Element to serialize.
* @param {?Object} context Context object.
* @param {Map=} context Context object.
* @param {?Object} legacyContext Legacy context object.
*
* @return {string} Serialized element.
Expand Down Expand Up @@ -411,11 +411,16 @@ export function renderElement( element, context, legacyContext = {} ) {

switch ( type && type.$$typeof ) {
case Provider.$$typeof:
return renderChildren( props.children, props.value, legacyContext );
context = new Map( context );
context.set( type, props.value );
return renderChildren( props.children, context, legacyContext );

case Consumer.$$typeof:
return renderElement(
props.children( context || type._currentValue ),
props.children(
( context && context.get( type._context.Provider ) ) ||
type._currentValue
),
context,
legacyContext
);
Expand Down
30 changes: 30 additions & 0 deletions packages/element/src/test/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,36 @@ describe( 'renderElement()', () => {
expect( result ).toBe( 'inner provided|outer provided' );
} );

it( 'renders proper value through Context API when nested, distinct providers present', () => {
const {
Consumer: FirstConsumer,
Provider: FirstProvider,
} = createContext();
const {
Consumer: SecondConsumer,
Provider: SecondProvider,
} = createContext();

const result = renderElement(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that e2e tests fail for blocks that have nesting. I'm wondering if there should be another unit tests that replicate how InnerBlocks works, if I follow it properly it would be something like:

<FirstProvider value="First">
 	<FirstConsumer>
 		{ ( first ) => (
 			<SecondProvider value="Second">
 				<SecondConsumer>
 					{ ( second ) => `First: { first }, Second: ${ second }` }
 				</SecondConsumer>
			</SecondProvider>
 		) }
	</FirstConsumer>
</FirstProvider>

Although, I don't see any reason why it would make any difference.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure what's going on with the end-to-end tests, to be honest. I've not been able to reproduce those same failures in my local environment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test case for this in 90dacf0 for good measure. It passes, as expected. Still struggling with the end-to-end tests in Travis.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just realized that we use one shared context for InnerBlocks so the following might be closer to the use case that fails on Travis:

<Provider value="outer">
 	<Consumer>
 		{ ( outter ) => (
 			< Provider value="inner">
 				<Consumer>
 					{ ( inner ) => `Outer: { outer }, Inner: ${ inner }` }
 				</Consumer>
			</Provider>
 		) }
	</Consumer>
</Provider>

I don't think it's much different from a similar existing test though. I can't think of any reason why it fails on Travis :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no :(

<FirstProvider value="First">
<SecondProvider value="Second">
<FirstConsumer>
{ ( first ) => (
<>
First: { first }, Second:{ ' ' }
<SecondConsumer>
{ ( second ) => second }
</SecondConsumer>
</>
) }
</FirstConsumer>
</SecondProvider>
</FirstProvider>
);

expect( result ).toBe( 'First: First, Second: Second' );
} );

it( 'renders RawHTML as its unescaped children', () => {
const result = renderElement( <RawHTML>{ '<img/>' }</RawHTML> );

Expand Down