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

Components: Deprecate withContext HOC and remove its usage #8189

Merged
merged 10 commits into from
Aug 14, 2018

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented Jul 25, 2018

Description

It's part of our efforts to stablize API.

How has this been tested?

npm test

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@gziolo gziolo added [Type] Breaking Change For PRs that introduce a change that will break existing functionality [Package] Components /packages/components labels Jul 25, 2018
@gziolo gziolo self-assigned this Jul 25, 2018
@gziolo gziolo requested a review from aduth July 25, 2018 10:27
@@ -445,6 +445,8 @@ export function renderElement( element, context = {} ) {
},
context
);
default:
throw new Error( tagName );
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 couldn' make it throw so I figured out we don't use the source code in all cases. It turned out it is even worse, we don't use in a majority of cases. I opened #8188 to fix it before I can continue debugging.

@gziolo gziolo added the [Status] In Progress Tracking issues with work in progress label Jul 25, 2018
@gziolo gziolo force-pushed the update/deprecate-with-context branch from 76b5cdc to 12793b1 Compare August 6, 2018 10:10
</Consumer>
</Provider>
{ '|' }
<Consumer>
Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

it's no longer an issue.

@gziolo gziolo requested a review from youknowriad August 6, 2018 10:18
@gziolo
Copy link
Member Author

gziolo commented Aug 6, 2018

Almost there, I made all the tests pass, which is very promising. When comparing the current implementation with react-dom I came up with a conclusion that it will fail when there are multiple providers used. I added a unit test which indeed fails. It might be a more complex refactor though. Any ideas how to fix it with the minimal amount of work? 😄

@gziolo
Copy link
Member Author

gziolo commented Aug 9, 2018

I took advantage of the fact we use recursion and add another param which transports values from the providers created with new Context API: 88e833c

It needs some polishing but it solves all the issues we had.

@gziolo gziolo removed the [Status] In Progress Tracking issues with work in progress label Aug 9, 2018
@gziolo gziolo requested a review from a team August 9, 2018 14:41
@gziolo gziolo added this to the 3.6 milestone Aug 9, 2018
BlockContentProvider.childContextTypes = {
BlockContent: () => {},
return (
<Provider value={ { BlockContent } }>
Copy link
Member

Choose a reason for hiding this comment

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

We pass a new value each render. I suppose this isn't a huge issue since it's used only in the serialization step anyways, but why does the context need to be an object anyways? Why not just the BlockContent value directly?

*
* @return {Component} Enhanced component with injected context as props.
*/
export const withBlockContentContext = ( mapContextToProps ) => createHigherOrderComponent( ( OriginalComponent ) => {
Copy link
Member

Choose a reason for hiding this comment

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

To my previous point, I don't see why we'd be passing anything other than the content component. We shouldn't need to map anything.

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 did it for consistency with other context related HOCs. However, you made a good point. I will refactor as suggested.

@gziolo gziolo force-pushed the update/deprecate-with-context branch from 8ef02fb to fc4997d Compare August 9, 2018 15:09

/**
* Internal dependencies
*/
import { serialize } from '../api';

const { Consumer, Provider } = createContext( {
Copy link
Member

Choose a reason for hiding this comment

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

I think this still needs to be updated to not set the initial context as an object, but rather the noop directly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, missed this one :(

/**
* An internal block component used in block content serialization to inject
* nested block content within the `save` implementation of the ancestor
* component in which it is nested. The component provides a pre-bound
* `BlockContent` component via context, which is used by the developer-facing
* `InnerBlocks.Content` component to render block content.
*
* @return {WPElement} Element with BlockContent injected via context.
Copy link
Member

Choose a reason for hiding this comment

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

Minor: We might want to establish conventions around tag order. How I've treated it, I always have @return last, preceded by @param, preceded by anything else.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, makes sense. In general, we could make JSDoc validation more strict since we started using it more consistently.

@gziolo gziolo force-pushed the update/deprecate-with-context branch from fc4997d to b6b7da7 Compare August 13, 2018 07:13
@gziolo
Copy link
Member Author

gziolo commented Aug 13, 2018

I pushed two more commits:

  • fix for the issues raised with the recent review iteration (b6b7da7)
  • one more tests which ensures we can properly serialize nested providers (e32503e)

@@ -39,6 +39,7 @@ describe( 'withContext', () => {
);

expect( wrapper.root.findByType( 'div' ).children[ 0 ] ).toBe( 'ok' );
expect( console ).toHaveWarned();
Copy link
Member

Choose a reason for hiding this comment

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

Minor: Question came up from @danielbachhuber about testing with deprecated warnings, and potential for conflicts with its internal memoization. Made me think over the weekend that the fact we test against console logging is a bit of an abstraction piercing; while it seems obvious deprecated would log to console by its description, we can't know for certain, and seems we should test that deprecated was merely called, regardless of its internal implementation.

I'd like to think this would be as simple as? ...

const deprecated = jest.mock( '@wordpress/deprecated' );

// ...

expect( deprecated ).toHaveBeenCalled();

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 think it should need:

import deprecated from '@wordpress/deprecated';

jest.mock( '@wordpress/deprecated', () => jest.fn() );

expect( deprecated ).toHaveBeenCalled();

I can give it a try.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Refactored in cf41540.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Breaking Change For PRs that introduce a change that will break existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants