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

Support getChildContext() in shallow render #1971

Merged
merged 4 commits into from
Jan 22, 2019

Conversation

minznerjosh
Copy link
Contributor

Fixes #664

This PR adds support for getChildContext() when shallow-rendering. I've decided to only implement parent-based context, not owner-based context (as I'm not exactly sure what that behavior is, tbh) which means I've excluded react@0.13 from this implementation. (Hope that's okay!)

In react@<=15, the react shallow renderer actually calls getChildContext() for us! This means, as is the case with shouldComponentUpdate(), we just spy on that method's return value.

In react@>=16, getChildContext() is not called (understandable—it's a deprecated API), so we have to call it ourselves.

P.S. Sorry the diff is a little ugly. I ended up messing with the nesting of some conditionals to avoid duplicated checks.

@minznerjosh minznerjosh force-pushed the josh__shallow-get-child-context branch 4 times, most recently from a301c92 to 56d78bf Compare January 8, 2019 19:33
@minznerjosh
Copy link
Contributor Author

P.S. I'm pretty sure the build will succeed once is #1968 is in.

@minznerjosh minznerjosh force-pushed the josh__shallow-get-child-context branch from 56d78bf to 5a4f92b Compare January 8, 2019 23:39
@ljharb ljharb force-pushed the josh__shallow-get-child-context branch from 5a4f92b to f0cd7e2 Compare January 14, 2019 19:36
@ljharb
Copy link
Member

ljharb commented Jan 14, 2019

@minznerjosh I've rebased this and split up the commits a bit more atomically; it'd be great if you could add some tests to each commit/change :-)

@minznerjosh
Copy link
Contributor Author

@ljharb you’ve got it! I’ll take care of it when I get back from vacation on Wednesday. Thanks for taking a look at this!

@minznerjosh minznerjosh force-pushed the josh__shallow-get-child-context branch 4 times, most recently from f426448 to ddf3550 Compare January 16, 2019 17:20
@minznerjosh
Copy link
Contributor Author

@ljharb I added tests to every commit except [enzyme-adapter-react-*] [new] add `legacyContextMode`, and `getChildContext` lifecycle method (which is just a configuration change.) This is ready for another round of review!

packages/enzyme/src/ShallowWrapper.js Show resolved Hide resolved
}
const childContext = instance.getChildContext();
Object.keys(childContext).forEach((key) => {
if (!(key in Component.childContextTypes)) {
Copy link
Member

Choose a reason for hiding this comment

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

are we using in here because React does? it seems like this should be an own property check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I tried to match react as much as possible and they are using in.

Copy link
Member

Choose a reason for hiding this comment

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

packages/enzyme/src/ShallowWrapper.js Show resolved Hide resolved
packages/enzyme/src/ShallowWrapper.js Show resolved Hide resolved
packages/enzyme/src/ShallowWrapper.js Outdated Show resolved Hide resolved
packages/enzyme/src/ShallowWrapper.js Show resolved Hide resolved
packages/enzyme/src/ShallowWrapper.js Outdated Show resolved Hide resolved
@ljharb ljharb force-pushed the josh__shallow-get-child-context branch from ddf3550 to cc59eab Compare January 17, 2019 08:18
@minznerjosh minznerjosh force-pushed the josh__shallow-get-child-context branch from cc59eab to 47b860f Compare January 17, 2019 16:07
@minznerjosh
Copy link
Contributor Author

@ljharb

  • Fixed the formatting of that ternary
  • Added tests that simulate an old adapter
  • Made it so that the context option overrides a parent's context (and added a test)

This is ready for another round of review!

packages/enzyme/src/ShallowWrapper.js Outdated Show resolved Hide resolved
packages/enzyme/src/ShallowWrapper.js Outdated Show resolved Hide resolved
packages/enzyme/src/ShallowWrapper.js Outdated Show resolved Hide resolved
@minznerjosh minznerjosh force-pushed the josh__shallow-get-child-context branch from 47b860f to f12f0dc Compare January 17, 2019 23:58
@minznerjosh
Copy link
Contributor Author

@ljharb

  • Should we implement checkPropTypes in all the adapters, even though it's only used in the >=16 adapters?
  • Removed multi-line strings
  • Added comment to explain warning if childContextTypes is not defined, with link to react src
  • Added comment to explain checking of propTypes implementation, with link to react src
  • Adjusted implementation of dive() so that options.context (passed to dive()) overrides anything else.

This is ready for another round!

@ljharb
Copy link
Member

ljharb commented Jan 19, 2019

@minznerjosh is there any chance the functionality is actually accessible inside one of the react packages, without copy/paste?

@minznerjosh
Copy link
Contributor Author

@ljharb Unfortunately, I don't think so. 🙁Both pieces of logic live in the react-reconciler package here and here, but neither are public exports, and the public exports that use those functions (like updateContainer) do too much to be useful for us.

@minznerjosh

This comment has been minimized.

@minznerjosh
Copy link
Contributor Author

@ljharb Can we live with the duplication of react logic here? If so, anything else blocking?

@ljharb
Copy link
Member

ljharb commented Jan 19, 2019

It's not ideal, but we can live with it :-) I'm reviewing and making some tweaks locally now.

@minznerjosh
Copy link
Contributor Author

Thank you!! Lemme know if there’s anything else I can do to help.

@ljharb ljharb force-pushed the josh__shallow-get-child-context branch 2 times, most recently from 9fba55d to 02d6337 Compare January 21, 2019 22:44
@ljharb ljharb merged commit 02d6337 into enzymejs:master Jan 22, 2019
context interaction automation moved this from In progress to Done / Closed Jan 22, 2019
@ljharb
Copy link
Member

ljharb commented Jan 22, 2019

Thanks for fixing this long-standing bug!

@minznerjosh minznerjosh deleted the josh__shallow-get-child-context branch January 22, 2019 17:45
@ljharb ljharb added this to Context in React 16 Jan 23, 2019
ljharb added a commit that referenced this pull request Jan 23, 2019
 - [new] add `legacyContextMode`, and `getChildContext` lifecycle method (#1971)
 - [deps] update `enzyme-adapter-utils`, `object.values`, `react-is`, `enzyme-adapter-react-helper`
 - [dev deps] update `eslint`, `eslint-plugin-react`, `rimraf`, `eslint-plugin-import`, `eslint-plugin-jsx-a11y`
ljharb added a commit that referenced this pull request Jan 27, 2019
 - [new] add `checkPropTypes` (#1971)
 - [new] add `legacyContextMode`, and `getChildContext` lifecycle method (#1971)
 - [fix] Filter legacy context on childContextTypes when necessary (#1968)
 - [deps] update `enzyme-adapter-utils`, `react-is`
 - [dev deps] update `eslint-plugin-import`, `eslint`, `eslint-plugin-react`, `rimraf`, `eslint-plugin-react`, `eslint-plugin-jsx-a11y`
ljharb added a commit that referenced this pull request Jan 27, 2019
 - [new] add `checkPropTypes` (#1971)
 - [new] add `legacyContextMode`, and `getChildContext` lifecycle method (#1971)
 - [fix] Filter legacy context on childContextTypes when necessary (#1968)
 - [deps] update `enzyme-adapter-utils`, `react-is`, `object.values`
 - [dev deps] update `eslint-plugin-import`, `eslint`, `eslint-plugin-react`, `rimraf`, `eslint-plugin-react`, `eslint-plugin-jsx-a11y`
ljharb added a commit that referenced this pull request Jan 27, 2019
 - [new] add `checkPropTypes` (#1971)
 - [new] add `legacyContextMode`, and `getChildContext` lifecycle method (#1971)
 - [fix] Filter legacy context on childContextTypes when necessary (#1968)
 - [deps] update `enzyme-adapter-utils`, `react-is`, `object.values`
 - [dev deps] update `eslint-plugin-import`, `eslint`, `eslint-plugin-react`, `rimraf`, `eslint-plugin-react`, `eslint-plugin-jsx-a11y`
ljharb added a commit that referenced this pull request Jan 27, 2019
 - [new] add `checkPropTypes` (#1971)
 - [new] add `legacyContextMode`, and `getChildContext` lifecycle method (#1971)
 - [fix] Filter legacy context on childContextTypes when necessary (#1968)
 - [deps] update `enzyme-adapter-utils`, `react-is`, `object.values`
 - [dev deps] update `eslint-plugin-import`, `eslint`, `eslint-plugin-react`, `rimraf`, `eslint-plugin-react`, `eslint-plugin-jsx-a11y`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
React 16
  
Context
context interaction
  
Done / Closed
Development

Successfully merging this pull request may close these issues.

None yet

2 participants