Skip to content

Commit

Permalink
Fix React.createFactory() crash (#11484)
Browse files Browse the repository at this point in the history
* Add a failing test for createFactory in production

* Fix createFactory() in production

* Add more prod-only tests

* Fix prettier

* Run prettier 1.8.1
  • Loading branch information
gaearon authored Nov 8, 2017
1 parent 94f44ae commit c932885
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
46 changes: 46 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMProduction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,52 @@ describe('ReactDOMProduction', () => {
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)', () => {
class Component extends React.Component {
render() {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/ReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export function createElement(type, config, children) {
* See https://reactjs.org/docs/react-api.html#createfactory
*/
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
// easily accessed on elements. E.g. `<Foo />.type === Foo`.
// This should not be named `constructor` since this may not be the function
Expand Down

0 comments on commit c932885

Please sign in to comment.