@samitouri / QOS-React / commits / c039c16f21

Fix this in a functional component for ShallowRenderer (#13144)

Toru Kobayashi committed Jul 4, 2018 at 01:47 UTC c039c16f21d7dd5172a3f46d65a4f484543a14ec
2 files changed +16 -1
packages/react-test-renderer/src/ReactShallowRenderer.js
+5 -1
@@ -107,7 +107,11 @@ class ReactShallowRenderer {
107
108 this._mountClassComponent(element, this._context);
109 } else {
110 - this._rendered = element.type(element.props, this._context);
110 + this._rendered = element.type.call(
111 + undefined,
112 + element.props,
113 + this._context,
114 + );
115 }
116 }
117
packages/react-test-renderer/src/__tests__/ReactShallowRenderer-test.js
+11
@@ -1407,4 +1407,15 @@ describe('ReactShallowRenderer', () => {
1407 instance.setState(state => ({count: state.count + 1}));
1408 expect(log).toEqual(['render']);
1409 });
1410 +
1411 + it('should not get this in a functional component', () => {
1412 + const logs = [];
1413 + function Foo() {
1414 + logs.push(this);
1415 + return <div>foo</div>;
1416 + }
1417 + const shallowRenderer = createRenderer();
1418 + shallowRenderer.render(<Foo foo="bar" />);
1419 + expect(logs).toEqual([undefined]);
1420 + });
1421 });