Skip to content

Commit 39b18f3

Browse files
koba04gaearon
authored andcommitted
Fix this in a functional component for ShallowRenderer (#13144)
1 parent 2e8ad8e commit 39b18f3

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/ReactShallowRenderer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ class ReactShallowRenderer {
107107

108108
this._mountClassComponent(element, this._context);
109109
} 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+
);
111115
}
112116
}
113117

src/__tests__/ReactShallowRenderer-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,4 +1407,15 @@ describe('ReactShallowRenderer', () => {
14071407
instance.setState(state => ({count: state.count + 1}));
14081408
expect(log).toEqual(['render']);
14091409
});
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+
});
14101421
});

0 commit comments

Comments
 (0)