From cfbb004cc75735511c4051e264f6b811de208de9 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 28 Sep 2017 15:11:48 +0100 Subject: [PATCH] Add a failing test that prints "null" in the warning instead of the stack --- .../__tests__/ReactStatelessComponent-test.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/renderers/__tests__/ReactStatelessComponent-test.js b/src/renderers/__tests__/ReactStatelessComponent-test.js index c8d67c7bbfc2c..577a6c1ee13d6 100644 --- a/src/renderers/__tests__/ReactStatelessComponent-test.js +++ b/src/renderers/__tests__/ReactStatelessComponent-test.js @@ -295,6 +295,43 @@ describe('ReactStatelessComponent', () => { console.error.calls.reset(); }); + // This guards against a regression caused by clearing the current debug fiber. + // https://github.com/facebook/react/issues/10831 + it('should warn when giving a function ref with context', () => { + spyOn(console, 'error'); + + function Child() { + return null; + } + Child.contextTypes = { + foo: PropTypes.string, + }; + + class Parent extends React.Component { + static childContextTypes = { + foo: PropTypes.string, + }; + getChildContext() { + return { + foo: 'bar', + }; + } + render() { + return ; + } + } + + ReactTestUtils.renderIntoDocument(); + expectDev(console.error.calls.count()).toBe(1); + expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe( + 'Warning: Stateless function components cannot be given refs. ' + + 'Attempts to access this ref will fail.\n\nCheck the render method ' + + 'of `Parent`.\n' + + ' in Child (at **)\n' + + ' in Parent (at **)', + ); + }); + it('should provide a null ref', () => { function Child() { return
;