Skip to content

Commit

Permalink
Fix this in a functional component for ShallowRenderer (#13144)
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 authored and gaearon committed Jul 3, 2018
1 parent 2e8ad8e commit 39b18f3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ class ReactShallowRenderer {

this._mountClassComponent(element, this._context);
} else {
this._rendered = element.type(element.props, this._context);
this._rendered = element.type.call(
undefined,
element.props,
this._context,
);
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/ReactShallowRenderer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1407,4 +1407,15 @@ describe('ReactShallowRenderer', () => {
instance.setState(state => ({count: state.count + 1}));
expect(log).toEqual(['render']);
});

it('should not get this in a functional component', () => {
const logs = [];
function Foo() {
logs.push(this);
return <div>foo</div>;
}
const shallowRenderer = createRenderer();
shallowRenderer.render(<Foo foo="bar" />);
expect(logs).toEqual([undefined]);
});
});

0 comments on commit 39b18f3

Please sign in to comment.