Fix React.createFactory() crash (#11484)

* Add a failing test for createFactory in production

* Fix createFactory() in production

* Add more prod-only tests

* Fix prettier

* Run prettier 1.8.1
This commit is contained in:
Dan Abramov 2017-11-08 00:02:07 +00:00 committed by GitHub
parent 94f44aeba7
commit c932885e79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View File

@ -93,6 +93,52 @@ describe('ReactDOMProduction', () => {
expect(container.childNodes.length).toBe(0); expect(container.childNodes.length).toBe(0);
}); });
it('should support createFactory', () => {
var span = React.createFactory('span');
class Component extends React.Component {
render() {
return span({children: this.props.children});
}
}
var ComponentFactory = React.createFactory(Component);
var container = document.createElement('div');
ReactDOM.render(
ComponentFactory(null, span(null, 'Hello'), span(null, 'world')),
container,
);
expect(container.firstChild.tagName).toBe('SPAN');
expect(container.firstChild.childNodes[0].tagName).toBe('SPAN');
expect(container.firstChild.childNodes[0].textContent).toBe('Hello');
expect(container.firstChild.childNodes[1].tagName).toBe('SPAN');
expect(container.firstChild.childNodes[1].textContent).toBe('world');
});
it('should support React public API methods', () => {
expect(React.isValidElement(42)).toBe(false);
expect(React.isValidElement(<div />)).toBe(true);
expect(React.cloneElement(<div />, {foo: 42})).toEqual(<div foo={42} />);
const mapped = React.Children.map(<div />, el =>
React.cloneElement(el, {foo: 42}),
);
expect(mapped.length).toBe(1);
expect(mapped[0].type).toBe('div');
expect(mapped[0].props.foo).toBe(42);
const arr = React.Children.toArray(<div />);
expect(arr.length).toBe(1);
expect(arr[0].type).toBe('div');
let called = 0;
React.Children.forEach(<div />, () => called++);
expect(called).toBe(1);
expect(React.Children.count(<div />)).toBe(1);
expect(() => React.Children.only(42)).toThrowError();
});
it('should handle a simple flow (ssr)', () => { it('should handle a simple flow (ssr)', () => {
class Component extends React.Component { class Component extends React.Component {
render() { render() {

View File

@ -266,7 +266,7 @@ export function createElement(type, config, children) {
* See https://reactjs.org/docs/react-api.html#createfactory * See https://reactjs.org/docs/react-api.html#createfactory
*/ */
export function createFactory(type) { export function createFactory(type) {
var factory = ReactElement.createElement.bind(null, type); var factory = createElement.bind(null, type);
// Expose the type on the factory and the prototype so that it can be // Expose the type on the factory and the prototype so that it can be
// easily accessed on elements. E.g. `<Foo />.type === Foo`. // easily accessed on elements. E.g. `<Foo />.type === Foo`.
// This should not be named `constructor` since this may not be the function // This should not be named `constructor` since this may not be the function