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

[FIX] Allow shallow renderer to work with new API #14329

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion packages/react-test-renderer/src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ class ReactShallowRenderer {

this._rendering = true;
this._element = element;
this._context = getMaskedContext(element.type.contextTypes, context);

if (element.type.contextType) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should only be enabled for class components.

this._context = context;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is the legacy context object. It has no relation to the new context API you're implementing.

} else {
this._context = getMaskedContext(element.type.contextTypes, context);
}

if (this._instance) {
this._updateClassComponent(element, this._context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,22 @@ describe('ReactShallowRenderer', () => {
expect(result).toEqual(<div />);
});

it('can shallowly render components with contextType', () => {
const SimpleContext = React.createContext('hello world');

class SimpleComponent extends React.Component {
static contextType = SimpleContext;

render() {
return <div>{this.context}</div>;
}
}

const shallowRenderer = createRenderer();
const result = shallowRenderer.render(<SimpleComponent />, 'Foo bar');
expect(result).toEqual(<div>Foo bar</div>);
});

it('passes expected params to legacy component lifecycle methods', () => {
const componentDidUpdateParams = [];
const componentWillReceivePropsParams = [];
Expand Down