mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
* Enable Yarn workspaces for packages/* * Move src/isomorphic/* into packages/react/src/* * Create index.js stubs for all packages in packages/* This makes the test pass again, but breaks the build because npm/ folders aren't used yet. I'm not sure if we'll keep this structure--I'll just keep working and fix the build after it settles down. * Put FB entry point for react-dom into packages/* * Move src/renderers/testing/* into packages/react-test-renderer/src/* Note that this is currently broken because Jest ignores node_modules, and so Yarn linking makes Jest skip React source when transforming. * Remove src/node_modules It is now unnecessary. Some tests fail though. * Add a hacky workaround for Jest/Workspaces issue Jest sees node_modules and thinks it's third party code. This is a hacky way to teach Jest to still transform anything in node_modules/react* if it resolves outside of node_modules (such as to our packages/*) folder. I'm not very happy with this and we should revisit. * Add a fake react-native package * Move src/renderers/art/* into packages/react-art/src/* * Move src/renderers/noop/* into packages/react-noop-renderer/src/* * Move src/renderers/dom/* into packages/react-dom/src/* * Move src/renderers/shared/fiber/* into packages/react-reconciler/src/* * Move DOM/reconciler tests I previously forgot to move * Move src/renderers/native-*/* into packages/react-native-*/src/* * Move shared code into packages/shared It's not super clear how to organize this properly yet. * Add back files that somehow got lost * Fix the build * Prettier * Add missing license headers * Fix an issue that caused mocks to get included into build * Update other references to src/ * Re-run Prettier * Fix lint * Fix weird Flow violation I didn't change this file but Flow started complaining. Caleb said this annotation was unnecessarily using $Abstract though so I removed it. * Update sizes * Fix stats script * Fix packaging fixtures Use file: instead of NODE_PATH since NODE_PATH. NODE_PATH trick only worked because we had no react/react-dom in root node_modules, but now we do. file: dependency only works as I expect in Yarn, so I moved the packaging fixtures to use Yarn and committed lockfiles. Verified that the page shows up. * Fix art fixture * Fix reconciler fixture * Fix SSR fixture * Rename native packages
584 lines
17 KiB
JavaScript
584 lines
17 KiB
JavaScript
/**
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @emails react-core
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var React;
|
|
var ReactDOM;
|
|
var ReactDOMServer;
|
|
var ReactTestUtils;
|
|
|
|
describe('ReactComponent', () => {
|
|
function normalizeCodeLocInfo(str) {
|
|
return str && str.replace(/\(at .+?:\d+\)/g, '(at **)');
|
|
}
|
|
|
|
beforeEach(() => {
|
|
React = require('react');
|
|
ReactDOM = require('react-dom');
|
|
ReactDOMServer = require('react-dom/server');
|
|
ReactTestUtils = require('react-dom/test-utils');
|
|
});
|
|
|
|
it('should throw on invalid render targets', () => {
|
|
var container = document.createElement('div');
|
|
// jQuery objects are basically arrays; people often pass them in by mistake
|
|
expect(function() {
|
|
ReactDOM.render(<div />, [container]);
|
|
}).toThrowError(/Target container is not a DOM element./);
|
|
|
|
expect(function() {
|
|
ReactDOM.render(<div />, null);
|
|
}).toThrowError(/Target container is not a DOM element./);
|
|
});
|
|
|
|
it('should throw when supplying a ref outside of render method', () => {
|
|
var instance = <div ref="badDiv" />;
|
|
expect(function() {
|
|
instance = ReactTestUtils.renderIntoDocument(instance);
|
|
}).toThrow();
|
|
});
|
|
|
|
it('should warn when children are mutated during render', () => {
|
|
spyOn(console, 'error');
|
|
function Wrapper(props) {
|
|
props.children[1] = <p key={1} />; // Mutation is illegal
|
|
return <div>{props.children}</div>;
|
|
}
|
|
expect(() => {
|
|
ReactTestUtils.renderIntoDocument(
|
|
<Wrapper>
|
|
<span key={0} />
|
|
<span key={1} />
|
|
<span key={2} />
|
|
</Wrapper>,
|
|
);
|
|
}).toThrowError(/Cannot assign to read only property.*/);
|
|
});
|
|
|
|
it('should warn when children are mutated during update', () => {
|
|
spyOn(console, 'error');
|
|
|
|
class Wrapper extends React.Component {
|
|
componentDidMount() {
|
|
this.props.children[1] = <p key={1} />; // Mutation is illegal
|
|
this.forceUpdate();
|
|
}
|
|
|
|
render() {
|
|
return <div>{this.props.children}</div>;
|
|
}
|
|
}
|
|
|
|
expect(() => {
|
|
ReactTestUtils.renderIntoDocument(
|
|
<Wrapper>
|
|
<span key={0} />
|
|
<span key={1} />
|
|
<span key={2} />
|
|
</Wrapper>,
|
|
);
|
|
}).toThrowError(/Cannot assign to read only property.*/);
|
|
});
|
|
|
|
it('should support refs on owned components', () => {
|
|
var innerObj = {};
|
|
var outerObj = {};
|
|
|
|
class Wrapper extends React.Component {
|
|
getObject = () => {
|
|
return this.props.object;
|
|
};
|
|
|
|
render() {
|
|
return <div>{this.props.children}</div>;
|
|
}
|
|
}
|
|
|
|
class Component extends React.Component {
|
|
render() {
|
|
var inner = <Wrapper object={innerObj} ref="inner" />;
|
|
var outer = <Wrapper object={outerObj} ref="outer">{inner}</Wrapper>;
|
|
return outer;
|
|
}
|
|
|
|
componentDidMount() {
|
|
expect(this.refs.inner.getObject()).toEqual(innerObj);
|
|
expect(this.refs.outer.getObject()).toEqual(outerObj);
|
|
}
|
|
}
|
|
|
|
ReactTestUtils.renderIntoDocument(<Component />);
|
|
});
|
|
|
|
it('should not have refs on unmounted components', () => {
|
|
class Parent extends React.Component {
|
|
render() {
|
|
return <Child><div ref="test" /></Child>;
|
|
}
|
|
|
|
componentDidMount() {
|
|
expect(this.refs && this.refs.test).toEqual(undefined);
|
|
}
|
|
}
|
|
|
|
class Child extends React.Component {
|
|
render() {
|
|
return <div />;
|
|
}
|
|
}
|
|
|
|
ReactTestUtils.renderIntoDocument(<Parent child={<span />} />);
|
|
});
|
|
|
|
it('should support new-style refs', () => {
|
|
var innerObj = {};
|
|
var outerObj = {};
|
|
|
|
class Wrapper extends React.Component {
|
|
getObject = () => {
|
|
return this.props.object;
|
|
};
|
|
|
|
render() {
|
|
return <div>{this.props.children}</div>;
|
|
}
|
|
}
|
|
|
|
var mounted = false;
|
|
|
|
class Component extends React.Component {
|
|
render() {
|
|
var inner = (
|
|
<Wrapper object={innerObj} ref={c => (this.innerRef = c)} />
|
|
);
|
|
var outer = (
|
|
<Wrapper object={outerObj} ref={c => (this.outerRef = c)}>
|
|
{inner}
|
|
</Wrapper>
|
|
);
|
|
return outer;
|
|
}
|
|
|
|
componentDidMount() {
|
|
expect(this.innerRef.getObject()).toEqual(innerObj);
|
|
expect(this.outerRef.getObject()).toEqual(outerObj);
|
|
mounted = true;
|
|
}
|
|
}
|
|
|
|
ReactTestUtils.renderIntoDocument(<Component />);
|
|
expect(mounted).toBe(true);
|
|
});
|
|
|
|
it('should support new-style refs with mixed-up owners', () => {
|
|
class Wrapper extends React.Component {
|
|
getTitle = () => {
|
|
return this.props.title;
|
|
};
|
|
|
|
render() {
|
|
return this.props.getContent();
|
|
}
|
|
}
|
|
|
|
var mounted = false;
|
|
|
|
class Component extends React.Component {
|
|
getInner = () => {
|
|
// (With old-style refs, it's impossible to get a ref to this div
|
|
// because Wrapper is the current owner when this function is called.)
|
|
return <div className="inner" ref={c => (this.innerRef = c)} />;
|
|
};
|
|
|
|
render() {
|
|
return (
|
|
<Wrapper
|
|
title="wrapper"
|
|
ref={c => (this.wrapperRef = c)}
|
|
getContent={this.getInner}
|
|
/>
|
|
);
|
|
}
|
|
|
|
componentDidMount() {
|
|
// Check .props.title to make sure we got the right elements back
|
|
expect(this.wrapperRef.getTitle()).toBe('wrapper');
|
|
expect(ReactDOM.findDOMNode(this.innerRef).className).toBe('inner');
|
|
mounted = true;
|
|
}
|
|
}
|
|
|
|
ReactTestUtils.renderIntoDocument(<Component />);
|
|
expect(mounted).toBe(true);
|
|
});
|
|
|
|
it('should call refs at the correct time', () => {
|
|
var log = [];
|
|
|
|
class Inner extends React.Component {
|
|
render() {
|
|
log.push(`inner ${this.props.id} render`);
|
|
return <div />;
|
|
}
|
|
|
|
componentDidMount() {
|
|
log.push(`inner ${this.props.id} componentDidMount`);
|
|
}
|
|
|
|
componentDidUpdate() {
|
|
log.push(`inner ${this.props.id} componentDidUpdate`);
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
log.push(`inner ${this.props.id} componentWillUnmount`);
|
|
}
|
|
}
|
|
|
|
class Outer extends React.Component {
|
|
render() {
|
|
return (
|
|
<div>
|
|
<Inner
|
|
id={1}
|
|
ref={c => {
|
|
log.push(`ref 1 got ${c ? `instance ${c.props.id}` : 'null'}`);
|
|
}}
|
|
/>
|
|
<Inner
|
|
id={2}
|
|
ref={c => {
|
|
log.push(`ref 2 got ${c ? `instance ${c.props.id}` : 'null'}`);
|
|
}}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
componentDidMount() {
|
|
log.push('outer componentDidMount');
|
|
}
|
|
|
|
componentDidUpdate() {
|
|
log.push('outer componentDidUpdate');
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
log.push('outer componentWillUnmount');
|
|
}
|
|
}
|
|
|
|
// mount, update, unmount
|
|
var el = document.createElement('div');
|
|
log.push('start mount');
|
|
ReactDOM.render(<Outer />, el);
|
|
log.push('start update');
|
|
ReactDOM.render(<Outer />, el);
|
|
log.push('start unmount');
|
|
ReactDOM.unmountComponentAtNode(el);
|
|
|
|
/* eslint-disable indent */
|
|
expect(log).toEqual([
|
|
'start mount',
|
|
'inner 1 render',
|
|
'inner 2 render',
|
|
'inner 1 componentDidMount',
|
|
'ref 1 got instance 1',
|
|
'inner 2 componentDidMount',
|
|
'ref 2 got instance 2',
|
|
'outer componentDidMount',
|
|
'start update',
|
|
// Previous (equivalent) refs get cleared
|
|
// Fiber renders first, resets refs later
|
|
'inner 1 render',
|
|
'inner 2 render',
|
|
'ref 1 got null',
|
|
'ref 2 got null',
|
|
'inner 1 componentDidUpdate',
|
|
'ref 1 got instance 1',
|
|
'inner 2 componentDidUpdate',
|
|
'ref 2 got instance 2',
|
|
'outer componentDidUpdate',
|
|
'start unmount',
|
|
'outer componentWillUnmount',
|
|
'ref 1 got null',
|
|
'inner 1 componentWillUnmount',
|
|
'ref 2 got null',
|
|
'inner 2 componentWillUnmount',
|
|
]);
|
|
/* eslint-enable indent */
|
|
});
|
|
|
|
it('fires the callback after a component is rendered', () => {
|
|
var callback = jest.fn();
|
|
var container = document.createElement('div');
|
|
ReactDOM.render(<div />, container, callback);
|
|
expect(callback.mock.calls.length).toBe(1);
|
|
ReactDOM.render(<div className="foo" />, container, callback);
|
|
expect(callback.mock.calls.length).toBe(2);
|
|
ReactDOM.render(<span />, container, callback);
|
|
expect(callback.mock.calls.length).toBe(3);
|
|
});
|
|
|
|
it('throws usefully when rendering badly-typed elements', () => {
|
|
spyOn(console, 'error');
|
|
|
|
var X = undefined;
|
|
expect(() => ReactTestUtils.renderIntoDocument(<X />)).toThrowError(
|
|
'Element type is invalid: expected a string (for built-in components) ' +
|
|
'or a class/function (for composite components) but got: undefined. ' +
|
|
"You likely forgot to export your component from the file it's " +
|
|
'defined in.',
|
|
);
|
|
|
|
var Y = null;
|
|
expect(() => ReactTestUtils.renderIntoDocument(<Y />)).toThrowError(
|
|
'Element type is invalid: expected a string (for built-in components) ' +
|
|
'or a class/function (for composite components) but got: null.',
|
|
);
|
|
|
|
// One warning for each element creation
|
|
expectDev(console.error.calls.count()).toBe(2);
|
|
});
|
|
|
|
it('includes owner name in the error about badly-typed elements', () => {
|
|
spyOn(console, 'error');
|
|
|
|
var X = undefined;
|
|
|
|
function Indirection(props) {
|
|
return <div>{props.children}</div>;
|
|
}
|
|
|
|
function Bar() {
|
|
return <Indirection><X /></Indirection>;
|
|
}
|
|
|
|
function Foo() {
|
|
return <Bar />;
|
|
}
|
|
|
|
expect(() => ReactTestUtils.renderIntoDocument(<Foo />)).toThrowError(
|
|
'Element type is invalid: expected a string (for built-in components) ' +
|
|
'or a class/function (for composite components) but got: undefined. ' +
|
|
"You likely forgot to export your component from the file it's " +
|
|
'defined in.\n\nCheck the render method of `Bar`.',
|
|
);
|
|
|
|
// One warning for each element creation
|
|
expectDev(console.error.calls.count()).toBe(1);
|
|
});
|
|
|
|
it('throws if a plain object is used as a child', () => {
|
|
var children = {
|
|
x: <span />,
|
|
y: <span />,
|
|
z: <span />,
|
|
};
|
|
var element = <div>{[children]}</div>;
|
|
var container = document.createElement('div');
|
|
var ex;
|
|
try {
|
|
ReactDOM.render(element, container);
|
|
} catch (e) {
|
|
ex = e;
|
|
}
|
|
expect(ex).toBeDefined();
|
|
expect(normalizeCodeLocInfo(ex.message)).toBe(
|
|
'Objects are not valid as a React child (found: object with keys ' +
|
|
'{x, y, z}). If you meant to render a collection of children, use ' +
|
|
'an array instead.' +
|
|
'\n in div (at **)',
|
|
);
|
|
});
|
|
|
|
it('throws if a plain object even if it is in an owner', () => {
|
|
class Foo extends React.Component {
|
|
render() {
|
|
var children = {
|
|
a: <span />,
|
|
b: <span />,
|
|
c: <span />,
|
|
};
|
|
return <div>{[children]}</div>;
|
|
}
|
|
}
|
|
var container = document.createElement('div');
|
|
var ex;
|
|
try {
|
|
ReactDOM.render(<Foo />, container);
|
|
} catch (e) {
|
|
ex = e;
|
|
}
|
|
expect(ex).toBeDefined();
|
|
expect(normalizeCodeLocInfo(ex.message)).toBe(
|
|
'Objects are not valid as a React child (found: object with keys ' +
|
|
'{a, b, c}). If you meant to render a collection of children, use ' +
|
|
'an array instead.\n' +
|
|
' in div (at **)\n' +
|
|
' in Foo (at **)',
|
|
);
|
|
});
|
|
|
|
it('throws if a plain object is used as a child when using SSR', async () => {
|
|
var children = {
|
|
x: <span />,
|
|
y: <span />,
|
|
z: <span />,
|
|
};
|
|
var element = <div>{[children]}</div>;
|
|
var ex;
|
|
try {
|
|
ReactDOMServer.renderToString(element);
|
|
} catch (e) {
|
|
ex = e;
|
|
}
|
|
expect(ex).toBeDefined();
|
|
expect(normalizeCodeLocInfo(ex.message)).toBe(
|
|
'Objects are not valid as a React child (found: object with keys ' +
|
|
'{x, y, z}). If you meant to render a collection of children, use ' +
|
|
'an array instead.' +
|
|
'\n in div (at **)',
|
|
);
|
|
});
|
|
|
|
it('throws if a plain object even if it is in an owner when using SSR', async () => {
|
|
class Foo extends React.Component {
|
|
render() {
|
|
var children = {
|
|
a: <span />,
|
|
b: <span />,
|
|
c: <span />,
|
|
};
|
|
return <div>{[children]}</div>;
|
|
}
|
|
}
|
|
var container = document.createElement('div');
|
|
var ex;
|
|
try {
|
|
ReactDOMServer.renderToString(<Foo />, container);
|
|
} catch (e) {
|
|
ex = e;
|
|
}
|
|
expect(ex).toBeDefined();
|
|
expect(normalizeCodeLocInfo(ex.message)).toBe(
|
|
'Objects are not valid as a React child (found: object with keys ' +
|
|
'{a, b, c}). If you meant to render a collection of children, use ' +
|
|
'an array instead.\n' +
|
|
' in div (at **)\n' +
|
|
' in Foo (at **)',
|
|
);
|
|
});
|
|
|
|
describe('with new features', () => {
|
|
it('warns on function as a return value from a function', () => {
|
|
function Foo() {
|
|
return Foo;
|
|
}
|
|
spyOn(console, 'error');
|
|
var container = document.createElement('div');
|
|
ReactDOM.render(<Foo />, container);
|
|
expectDev(console.error.calls.count()).toBe(1);
|
|
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
|
|
'Warning: Functions are not valid as a React child. This may happen if ' +
|
|
'you return a Component instead of <Component /> from render. ' +
|
|
'Or maybe you meant to call this function rather than return it.\n' +
|
|
' in Foo (at **)',
|
|
);
|
|
});
|
|
|
|
it('warns on function as a return value from a class', () => {
|
|
class Foo extends React.Component {
|
|
render() {
|
|
return Foo;
|
|
}
|
|
}
|
|
spyOn(console, 'error');
|
|
var container = document.createElement('div');
|
|
ReactDOM.render(<Foo />, container);
|
|
expectDev(console.error.calls.count()).toBe(1);
|
|
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
|
|
'Warning: Functions are not valid as a React child. This may happen if ' +
|
|
'you return a Component instead of <Component /> from render. ' +
|
|
'Or maybe you meant to call this function rather than return it.\n' +
|
|
' in Foo (at **)',
|
|
);
|
|
});
|
|
|
|
it('warns on function as a child to host component', () => {
|
|
function Foo() {
|
|
return <div><span>{Foo}</span></div>;
|
|
}
|
|
spyOn(console, 'error');
|
|
var container = document.createElement('div');
|
|
ReactDOM.render(<Foo />, container);
|
|
expectDev(console.error.calls.count()).toBe(1);
|
|
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
|
|
'Warning: Functions are not valid as a React child. This may happen if ' +
|
|
'you return a Component instead of <Component /> from render. ' +
|
|
'Or maybe you meant to call this function rather than return it.\n' +
|
|
' in span (at **)\n' +
|
|
' in div (at **)\n' +
|
|
' in Foo (at **)',
|
|
);
|
|
});
|
|
|
|
it('does not warn for function-as-a-child that gets resolved', () => {
|
|
function Bar(props) {
|
|
return props.children();
|
|
}
|
|
function Foo() {
|
|
return <Bar>{() => 'Hello'}</Bar>;
|
|
}
|
|
spyOn(console, 'error');
|
|
var container = document.createElement('div');
|
|
ReactDOM.render(<Foo />, container);
|
|
expect(container.innerHTML).toBe('Hello');
|
|
expectDev(console.error.calls.count()).toBe(0);
|
|
});
|
|
|
|
it('deduplicates function type warnings based on component type', () => {
|
|
spyOn(console, 'error');
|
|
class Foo extends React.PureComponent {
|
|
constructor() {
|
|
super();
|
|
this.state = {type: 'mushrooms'};
|
|
}
|
|
render() {
|
|
return (
|
|
<div>
|
|
{Foo}{Foo}
|
|
<span>{Foo}{Foo}</span>
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
var container = document.createElement('div');
|
|
var component = ReactDOM.render(<Foo />, container);
|
|
component.setState({type: 'portobello mushrooms'});
|
|
expectDev(console.error.calls.count()).toBe(2);
|
|
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
|
|
'Warning: Functions are not valid as a React child. This may happen if ' +
|
|
'you return a Component instead of <Component /> from render. ' +
|
|
'Or maybe you meant to call this function rather than return it.\n' +
|
|
' in div (at **)\n' +
|
|
' in Foo (at **)',
|
|
);
|
|
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toBe(
|
|
'Warning: Functions are not valid as a React child. This may happen if ' +
|
|
'you return a Component instead of <Component /> from render. ' +
|
|
'Or maybe you meant to call this function rather than return it.\n' +
|
|
' in span (at **)\n' +
|
|
' in div (at **)\n' +
|
|
' in Foo (at **)',
|
|
);
|
|
});
|
|
});
|
|
});
|