/* eslint-env browser */
import 'babel-polyfill';
import React, { Component, PureComponent } from 'react';
import createReactClass from 'create-react-class';
import { mount } from 'enzyme';
import TestRenderer from 'react-test-renderer';
import { mapProps } from 'recompose';
import { polyfill } from 'react-lifecycles-compat';
import { AppContainer } from '../src/index.dev';
import RHL from '../src/reactHotLoader';
import { closeGeneration, configureGeneration, increment as incrementGeneration } from '../src/global/generation';
import { configureComponent } from '../src/utils.dev';
import configuration from '../src/configuration';
describe(`AppContainer (dev)`, () => {
beforeEach(() => {
RHL.reset();
configureGeneration(1, 1);
});
describe('with class root', () => {
it('renders children', () => {
const spy = jest.fn();
class App extends Component {
render() {
spy();
return
hey
;
}
}
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(wrapper.find('App').length).toBe(1);
expect(wrapper.contains(hey
)).toBe(true);
expect(spy).toHaveBeenCalledTimes(1);
});
it('force updates the tree on receiving new children', () => {
const spy = jest.fn();
const spy2 = jest.fn();
class App extends Component {
shouldComponentUpdate() {
return false;
}
render() {
spy();
return hey
;
}
}
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(spy).toHaveBeenCalledTimes(1);
{
class App extends Component {
shouldComponentUpdate() {
return false;
}
render() {
spy();
spy2();
return ho
;
}
}
RHL.register(App, 'App', 'test.js');
wrapper.setProps({ children: });
}
expect(spy).toHaveBeenCalledTimes(3);
expect(spy2).toHaveBeenCalledTimes(2);
expect(wrapper.update().contains(ho
)).toBe(true);
});
it('force updates the tree on receiving cached children', () => {
const spy = jest.fn();
class App extends Component {
shouldComponentUpdate() {
return false;
}
render() {
spy();
return hey
;
}
}
RHL.register(App, 'App', 'test.js');
const element = ;
const wrapper = mount({element});
expect(spy).toHaveBeenCalledTimes(1);
{
class App extends Component {
shouldComponentUpdate() {
return false;
}
render() {
spy();
return ho
;
}
}
RHL.register(App, 'App', 'test.js');
wrapper.setProps({ children: element });
}
expect(spy).toHaveBeenCalledTimes(3);
expect(wrapper.update().contains(ho
)).toBe(true);
});
it('renders latest children on receiving cached never-rendered children', () => {
const spy = jest.fn();
class App extends Component {
shouldComponentUpdate() {
return false;
}
render() {
spy();
return hey
;
}
}
RHL.register(App, 'App', 'test.js');
const element = ;
let wrapper;
{
class App extends Component {
shouldComponentUpdate() {
return false;
}
render() {
spy();
return ho
;
}
}
RHL.register(App, 'App', 'test.js');
wrapper = mount({element});
}
expect(spy).toHaveBeenCalledTimes(1);
expect(wrapper.contains(ho
)).toBe(true);
});
it('hot-reloads children without losing state', () => {
class App extends Component {
constructor(props) {
super(props);
this.state = { value: 'old' };
}
shouldComponentUpdate() {
return false;
}
render() {
return old render + {this.state.value} state
;
}
}
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(wrapper.text()).toBe('old render + old state');
{
class App extends Component {
constructor(props) {
super(props);
this.state = { value: 'new' };
}
shouldComponentUpdate() {
return false;
}
render() {
return new render + {this.state.value} state
;
}
}
RHL.register(App, 'App', 'test.js');
wrapper.setProps({ children: });
}
expect(wrapper.text()).toBe('new render + old state');
});
it('Could handle render as a prop', () => {
const spy = jest.fn();
/* eslint-disable */
class SubApp extends Component {
componentWillUnmount() {
spy('SubApp1');
}
render = function() {
return works;
};
}
class App extends Component {
componentWillUnmount() {
spy('App1');
}
__reactstandin__regenerateByEval(key, code) {
this[key] = eval(code);
}
render() {
return (
before
);
}
}
/* eslint-enable */
const Indirect = ({ App }) => (
);
configureComponent(Indirect, { allowSFC: false });
const wrapper = mount();
expect(wrapper.text()).toBe('works before');
{
/* eslint-disable */
class SubApp extends Component {
componentWillUnmount() {
spy('SubApp2');
}
render = function() {
return works;
};
}
SubApp.displayName = 'SubApp';
class App extends Component {
componentWillUnmount() {
spy('App2');
}
__reactstandin__regenerateByEval(key, code) {
this[key] = eval(code);
}
render = function() {
return (
after
);
};
}
App.displayName = 'App';
/* eslint-enable */
incrementGeneration();
wrapper.setProps({ App });
}
expect(wrapper.text()).toBe('works after');
expect(spy).not.toHaveBeenCalled();
});
it('Could handle render as a prop with Pure Render', () => {
const { pureRender } = configuration;
configuration.pureRender = true;
const spy = jest.fn();
/* eslint-disable */
class SubApp extends Component {
componentWillUnmount() {
spy('SubApp1');
}
render = function() {
return works;
};
}
class App extends Component {
componentWillUnmount() {
spy('App1');
}
__reactstandin__regenerateByEval(key, code) {
this[key] = eval(code);
}
render() {
return (
before
);
}
}
/* eslint-enable */
const Indirect = ({ App }) => (
);
configureComponent(Indirect, { allowSFC: false });
const wrapper = mount();
expect(wrapper.text()).toBe('works before');
expect(.type.prototype.render).toBe(App.prototype.render);
const originalRender = App.prototype.render;
let newRender;
{
/* eslint-disable */
class SubApp extends Component {
componentWillUnmount() {
spy('SubApp2');
}
render = function() {
return works;
};
}
SubApp.displayName = 'SubApp';
class App extends Component {
componentWillUnmount() {
spy('App2');
}
__reactstandin__regenerateByEval(key, code) {
this[key] = eval(code);
}
render() {
return (
after
);
}
}
App.displayName = 'App';
/* eslint-enable */
incrementGeneration();
wrapper.setProps({ App });
newRender = App.prototype.render;
}
expect(wrapper.text()).toBe('works after');
expect(spy).not.toHaveBeenCalled();
// render on App is changed by merge process. Compare with stored value
expect(.type.prototype.render).not.toBe(originalRender);
expect(.type.prototype.render).not.toBe(newRender);
closeGeneration();
expect(.type.prototype.render).toBe(newRender);
configuration.pureRender = pureRender;
});
it('replaces children class methods', () => {
const spy = jest.fn();
class App extends Component {
constructor(props) {
super(props);
this.state = { value: 'old' };
}
shouldComponentUpdate() {
return false;
}
handleClick() {
spy('foo');
}
render() {
return old render + {this.state.value} state;
}
}
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
wrapper.find('span').simulate('click');
expect(spy).toHaveBeenCalledWith('foo');
expect(wrapper.text()).toBe('old render + old state');
spy.mockReset();
{
class App extends Component {
constructor(props) {
super(props);
this.state = { value: 'new' };
}
shouldComponentUpdate() {
return false;
}
handleClick() {
spy('bar');
}
render() {
return new render + {this.state.value} state;
}
}
RHL.register(App, 'App', 'test.js');
wrapper.setProps({ children: });
}
wrapper.find('span').simulate('click');
expect(spy).toHaveBeenCalledWith('bar');
expect(wrapper.text()).toBe('new render + old state');
});
it('replaces children class property arrow functions', () => {
const spy = jest.fn();
class App extends Component {
constructor(props) {
super(props);
this.state = { value: 'old' };
}
shouldComponentUpdate() {
return false;
}
handleClick = () => {
spy('foo');
};
render() {
return old render + {this.state.value} state;
}
}
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
wrapper.find('span').simulate('click');
expect(spy).toHaveBeenCalledWith('foo');
expect(wrapper.text()).toBe('old render + old state');
spy.mockReset();
window.spy = spy;
{
class App extends Component {
constructor(props) {
super(props);
this.state = { value: 'new' };
}
shouldComponentUpdate() {
return false;
}
handleClick = () => {
window.spy('bar');
};
/* eslint-disable */
__reactstandin__regenerateByEval(key, code) {
this[key] = eval(code);
}
/* eslint-enable */
render() {
return new render + {this.state.value} state;
}
}
RHL.register(App, 'App', 'test.js');
wrapper.setProps({ children: });
}
wrapper.find('span').simulate('click');
expect(spy).toHaveBeenCalledWith('bar');
expect(wrapper.text()).toBe('new render + old state');
});
it('replaces children class arrow functions in constructor', () => {
const spy = jest.fn();
class App extends Component {
constructor(props) {
super(props);
this.handleClick = () => {
spy('foo');
};
this.state = { value: 'old' };
}
shouldComponentUpdate() {
return false;
}
render() {
return old render + {this.state.value} state;
}
}
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
wrapper.find('span').simulate('click');
expect(spy).toHaveBeenCalledWith('foo');
expect(wrapper.text()).toBe('old render + old state');
spy.mockReset();
window.spy = spy;
{
class App extends Component {
constructor(props) {
super(props);
this.state = { value: 'new' };
}
shouldComponentUpdate() {
return false;
}
handleClick = () => {
window.spy('bar');
};
/* eslint-disable */
__reactstandin__regenerateByEval(key, code) {
this[key] = eval(code);
}
/* eslint-enable */
render() {
return new render + {this.state.value} state;
}
}
RHL.register(App, 'App', 'test.js');
wrapper.setProps({ children: });
}
wrapper.find('span').simulate('click');
expect(spy).toHaveBeenCalledWith('bar');
expect(wrapper.text()).toBe('new render + old state');
});
it('replaces children class property arrow functions without block statement bodies', () => {
const spy = jest.fn();
class App extends Component {
constructor(props) {
super(props);
this.state = { value: 'old' };
}
shouldComponentUpdate() {
return false;
}
handleClick = () => spy('foo');
render() {
return old render + {this.state.value} state;
}
}
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
wrapper.find('span').simulate('click');
expect(spy).toHaveBeenCalledWith('foo');
expect(wrapper.text()).toBe('old render + old state');
spy.mockReset();
window.spy = spy;
{
class App extends Component {
constructor(props) {
super(props);
this.state = { value: 'new' };
}
shouldComponentUpdate() {
return false;
}
handleClick = () => window.spy('bar');
/* eslint-disable */
__reactstandin__regenerateByEval(key, code) {
this[key] = eval(code);
}
/* eslint-enable */
render() {
return new render + {this.state.value} state;
}
}
RHL.register(App, 'App', 'test.js');
wrapper.setProps({ children: });
}
wrapper.find('span').simulate('click');
expect(spy).toHaveBeenCalledWith('bar');
expect(wrapper.text()).toBe('new render + old state');
});
it('replaces PureComponent', () => {
const spy = jest.fn();
class Pure extends PureComponent {
componentWillUnmount() {
spy();
}
render() {
return I am old;
}
}
RHL.register(Pure, 'Pure', 'test.js');
class RenderFn extends PureComponent {
render() {
const { _children, v } = this.props;
return _children()(v);
}
}
const innerRenderFn = v => ;
const renderFn = () => innerRenderFn;
class App extends PureComponent {
render() {
return (
);
}
}
const wrapper = mount(
,
);
expect(wrapper.text()).toBe('I am old');
{
class Pure extends PureComponent {
componentWillUnmount() {
spy();
}
render() {
return I am new;
}
}
RHL.register(Pure, 'Pure', 'test.js');
wrapper.setProps({ children: });
}
expect(wrapper.text()).toBe('I am new');
expect(spy).not.toHaveBeenCalled();
});
it('replaces React.memo', done => {
if (!React.memo) {
expect(1).toBe(1);
done();
return;
}
const Pure = React.memo(() => I am old);
RHL.register(Pure, 'Pure', 'test.js');
const App = () => (
);
const wrapper = TestRenderer.create(
,
);
expect(wrapper.root.findByProps({ children: 'I am old' })).toBeDefined();
{
const Pure = React.memo(() => I am new);
RHL.register(Pure, 'Pure', 'test.js');
wrapper.update(
,
);
}
setTimeout(() => {
expect(wrapper.root.findByProps({ children: 'I am new' })).toBeDefined();
done();
}, 16);
});
it('handles forwardRef', () => {
if (!React.forwardRef) {
expect(1).toBe(1);
return;
}
const spy = jest.fn();
class MountSpy extends PureComponent {
static displayName = 'MountSpy';
componentWillUnmount() {
spy();
}
render() {
return I am old;
}
}
const FRW = React.forwardRef(() => (
));
RHL.register(FRW, 'FRW', 'test.js');
const App = () => (
children
);
const wrapper = TestRenderer.create(
,
);
expect(wrapper.root.findByProps({ children: 'I am old' })).toBeDefined();
{
class MountSpy extends PureComponent {
static displayName = 'MountSpy';
componentWillUnmount() {
spy();
}
render() {
return I am new;
}
}
const FRW = React.forwardRef(() => (
));
RHL.register(FRW, 'FRW', 'test.js');
wrapper.update(
,
);
}
expect(spy).not.toHaveBeenCalled();
expect(wrapper.root.findByProps({ children: 'I am new' })).toBeDefined();
});
it.skip('handles react-lazy', async () => {
const spy = jest.fn();
class MountSpy extends PureComponent {
static displayName = 'MountSpy';
componentWillUnmount() {
spy();
}
render() {
return I am old;
}
}
const Tmp = MountSpy;
const Lazy = React.lazy(() => Promise.resolve({ default: Tmp }));
RHL.register(MountSpy, 'Pure', 'test.js');
RHL.register(Lazy, 'Lazy', 'test.js');
const ref = jest.fn();
class App extends Component {
render() {
return ;
}
}
const wrapper = TestRenderer.create(
,
);
await Promise.resolve(1);
expect(spy).not.toHaveBeenCalled();
expect(ref).toHaveBeenCalledTimes(1);
expect(wrapper.root.findByProps({ children: 'I am old' })).toBeDefined();
{
class MountSpy extends PureComponent {
static displayName = 'MountSpy';
componentWillUnmount() {
spy();
}
render() {
return I am new;
}
}
const Lazy = React.lazy(() => Promise.resolve({ default: Tmp }));
RHL.register(MountSpy, 'Pure', 'test.js');
RHL.register(Lazy, 'Lazy', 'test.js');
wrapper.update(
,
);
await Promise.resolve(1);
}
expect(spy).not.toHaveBeenCalled();
expect(wrapper.root.findByProps({ children: 'I am new' })).toBeDefined();
});
it('replaces children with class property arrow functions with different numbers of arguments', () => {
const spy = jest.fn();
class App extends Component {
constructor(props) {
super(props);
this.state = { value: 'old' };
}
shouldComponentUpdate() {
return false;
}
handleClick = () => spy('foo');
render() {
return old render + {this.state.value} state;
}
}
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
wrapper.find('span').simulate('click');
expect(spy).toHaveBeenCalledWith('foo');
expect(wrapper.text()).toBe('old render + old state');
spy.mockReset();
window.spy = spy;
{
class App extends Component {
constructor(props) {
super(props);
this.state = { value: 'new' };
}
shouldComponentUpdate() {
return false;
}
handleClick = ({ target }) => window.spy(target.value);
/* eslint-disable */
__reactstandin__regenerateByEval(key, code) {
this[key] = eval(code);
}
/* eslint-enable */
render() {
return new render + {this.state.value} state;
}
}
RHL.register(App, 'App', 'test.js');
wrapper.setProps({ children: });
}
wrapper.find('span').simulate('click', { target: { value: 'bar' } });
expect(spy).toHaveBeenCalledWith('bar');
expect(wrapper.text()).toBe('new render + old state');
});
it('passes a default context value', () => {
if (React.version.startsWith('16')) {
const spy = jest.fn();
const contextValue = 'value';
const { Consumer } = React.createContext('value');
class App extends Component {
render() {
return (
{value => {
spy();
return {value}
;
}}
);
}
}
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(wrapper.contains({contextValue}
)).toBe(true);
expect(spy).toHaveBeenCalledTimes(1);
} else {
expect(true).toBe(true);
}
});
it('passes provider defined context value', () => {
if (React.version.startsWith('16')) {
const spy = jest.fn();
const contextValue = 'value';
const { Provider, Consumer } = React.createContext();
class App extends Component {
render() {
return (
{value => {
spy();
return {value}
;
}}
);
}
}
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(wrapper.contains({contextValue}
)).toBe(true);
expect(spy).toHaveBeenCalledTimes(1);
} else {
expect(true).toBe(true);
}
});
it('passes provider defined falsy context value', () => {
if (React.version.startsWith('16')) {
const spy = jest.fn();
const contextValue = 0;
const { Provider, Consumer } = React.createContext();
class App extends Component {
render() {
return (
{value => {
spy(value);
return {value}
;
}}
);
}
}
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(wrapper.contains({contextValue}
)).toBe(true);
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(contextValue);
} else {
expect(true).toBe(true);
}
});
it('passes provider defined falsy context value on receiving new children', () => {
if (React.version.startsWith('16')) {
const spy = jest.fn();
const spy2 = jest.fn();
const contextValue = false;
const { Provider, Consumer } = React.createContext();
class App extends Component {
render() {
return (
{value => {
spy(value);
return {value}
;
}}
);
}
}
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(wrapper.contains({contextValue}
)).toBe(true);
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(contextValue);
{
class App extends Component {
shouldComponentUpdate() {
return false;
}
render() {
return (
{value => {
spy2(value);
return {value}
;
}}
);
}
}
RHL.register(App, 'App', 'test.js');
wrapper.setProps({ children: });
}
const firstCallArg = spy2.mock.calls[0][0];
expect(spy2).toHaveBeenCalledTimes(2);
expect(firstCallArg).toBeDefined();
expect(firstCallArg).toBe(contextValue);
} else {
expect(true).toBe(true);
}
});
});
describe('with createClass root', () => {
it('renders children', () => {
const spy = jest.fn();
const App = createReactClass({
render() {
spy();
return hey
;
},
});
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(wrapper.find('App').length).toBe(1);
expect(wrapper.contains(hey
)).toBe(true);
expect(spy).toHaveBeenCalledTimes(1);
});
it('force updates the tree on receiving new children', () => {
const spy = jest.fn();
const App = createReactClass({
shouldComponentUpdate() {
return false;
},
render() {
spy();
return hey
;
},
});
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(spy).toHaveBeenCalledTimes(1);
{
const App = createReactClass({
shouldComponentUpdate() {
return false;
},
render() {
spy();
return ho
;
},
});
RHL.register(App, 'App', 'test.js');
wrapper.setProps({ children: });
}
expect(spy).toHaveBeenCalledTimes(1 + 2);
expect(wrapper.update().contains(ho
)).toBe(true);
});
it('force updates the tree on receiving cached children', () => {
const spy = jest.fn();
const App = createReactClass({
shouldComponentUpdate() {
return false;
},
render() {
spy();
return hey
;
},
});
RHL.register(App, 'App', 'test.js');
const element = ;
const wrapper = mount({element});
expect(spy).toHaveBeenCalledTimes(1);
{
const App = createReactClass({
shouldComponentUpdate() {
return false;
},
render() {
spy();
return ho
;
},
});
RHL.register(App, 'App', 'test.js');
wrapper.setProps({ children: element });
}
expect(spy).toHaveBeenCalledTimes(1 + 2);
expect(wrapper.update().contains(ho
)).toBe(true);
});
it('renders latest children on receiving cached never-rendered children', () => {
const spy = jest.fn();
const App = createReactClass({
shouldComponentUpdate() {
return false;
},
render() {
spy();
return hey
;
},
});
RHL.register(App, 'App', 'test.js');
const element = ;
let wrapper;
{
const App = createReactClass({
shouldComponentUpdate() {
return false;
},
render() {
spy();
return ho
;
},
});
RHL.register(App, 'App', 'test.js');
wrapper = mount({element});
}
expect(spy).toHaveBeenCalledTimes(1);
expect(wrapper.contains(ho
)).toBe(true);
});
it('hot-reloads children without losing state', () => {
const App = createReactClass({
getInitialState() {
return { value: 'old' };
},
shouldComponentUpdate() {
return false;
},
render() {
return old render + {this.state.value} state
;
},
});
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(wrapper.text()).toBe('old render + old state');
{
const App = createReactClass({
getInitialState() {
return { value: 'new' };
},
shouldComponentUpdate() {
return false;
},
render() {
return new render + {this.state.value} state
;
},
});
RHL.register(App, 'App', 'test.js');
wrapper.setProps({ children: });
}
expect(wrapper.text()).toBe('new render + old state');
});
});
describe('with createFactory root', () => {
it('renders children', () => {
const spy = jest.fn();
const App = createReactClass({
render() {
spy();
return hey
;
},
});
RHL.register(App, 'App', 'test.js');
const AppF = React.createFactory(App);
const wrapper = mount({AppF()});
expect(wrapper.find('App').length).toBe(1);
expect(wrapper.contains(hey
)).toBe(true);
expect(spy).toHaveBeenCalledTimes(1);
});
it('force updates the tree on receiving new children', () => {
const spy = jest.fn();
const App = createReactClass({
shouldComponentUpdate() {
return false;
},
render() {
spy();
return hey
;
},
});
RHL.register(App, 'App', 'test.js');
const AppF = React.createFactory(App);
const wrapper = mount({AppF()});
expect(spy).toHaveBeenCalledTimes(1);
{
const App = createReactClass({
shouldComponentUpdate() {
return false;
},
render() {
spy();
return ho
;
},
});
RHL.register(App, 'App', 'test.js');
const AppF = React.createFactory(App);
wrapper.setProps({ children: AppF() });
}
expect(spy).toHaveBeenCalledTimes(1 + 2);
expect(wrapper.update().contains(ho
)).toBe(true);
});
it('force updates the tree on receiving cached children', () => {
const spy = jest.fn();
const App = createReactClass({
shouldComponentUpdate() {
return false;
},
render() {
spy();
return hey
;
},
});
RHL.register(App, 'App', 'test.js');
const AppF = React.createFactory(App);
const element = AppF();
const wrapper = mount({element});
expect(spy).toHaveBeenCalledTimes(1);
{
const App = createReactClass({
shouldComponentUpdate() {
return false;
},
render() {
spy();
return ho
;
},
});
RHL.register(App, 'App', 'test.js');
wrapper.setProps({ children: element });
}
expect(spy).toHaveBeenCalledTimes(1 + 2);
expect(wrapper.update().contains(ho
)).toBe(true);
});
it('renders latest children on receiving cached never-rendered children', () => {
const spy = jest.fn();
const App = createReactClass({
shouldComponentUpdate() {
return false;
},
render() {
spy();
return hey
;
},
});
RHL.register(App, 'App', 'test.js');
const AppF = React.createFactory(App);
const element = AppF();
let wrapper;
{
const App = createReactClass({
shouldComponentUpdate() {
return false;
},
render() {
spy();
return ho
;
},
});
RHL.register(App, 'App', 'test.js');
wrapper = mount({element});
}
expect(spy).toHaveBeenCalledTimes(1);
expect(wrapper.contains(ho
)).toBe(true);
});
it('hot-reloads children without losing state', () => {
const App = createReactClass({
getInitialState() {
return { value: 'old' };
},
shouldComponentUpdate() {
return false;
},
render() {
return old render + {this.state.value} state
;
},
});
RHL.register(App, 'App', 'test.js');
const AppF = React.createFactory(App);
const wrapper = mount({AppF()});
expect(wrapper.text()).toBe('old render + old state');
{
const App = createReactClass({
getInitialState() {
return { value: 'new' };
},
shouldComponentUpdate() {
return false;
},
render() {
return new render + {this.state.value} state
;
},
});
RHL.register(App, 'App', 'test.js');
const AppF = React.createFactory(App);
wrapper.setProps({ children: AppF() });
}
expect(wrapper.text()).toBe('new render + old state');
});
});
describe('with SFC root', () => {
it('renders children', () => {
const spy = jest.fn();
const App = () => {
spy();
return hey
;
};
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(wrapper.find('App').length).toBe(1);
expect(wrapper.contains(hey
)).toBe(true);
expect(spy).toHaveBeenCalledTimes(1);
});
it('renders falsy children', () => {
const spy = jest.fn();
const App = () => {
spy();
return null;
};
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(wrapper.find('App').length).toBe(1);
expect(spy).toHaveBeenCalledTimes(1);
});
it('force updates the tree on receiving new children', () => {
const spy = jest.fn();
const App = () => {
spy();
return hey
;
};
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(spy).toHaveBeenCalledTimes(1);
{
const App = () => {
spy();
return ho
;
};
RHL.register(App, 'App', 'test.js');
wrapper.setProps({ children: });
}
expect(spy).toHaveBeenCalledTimes(configuration.pureRender && !React.lazy ? 4 : 3);
expect(wrapper.contains(ho
)).toBe(true);
});
it('force updates the tree on receiving cached children', () => {
const firstSpy = jest.fn();
const Dummy = () => {
firstSpy();
return first
;
};
const App = () => ;
RHL.register(Dummy, 'Dummy', 'test.js');
const wrapper = mount(
,
);
expect(firstSpy).toHaveBeenCalledTimes(1);
const secondSpy = jest.fn();
{
const Dummy = () => {
secondSpy();
return second
;
};
RHL.register(Dummy, 'Dummy', 'test.js');
wrapper.setProps({ children: });
}
expect(firstSpy).toHaveBeenCalledTimes(1);
expect(secondSpy).toHaveBeenCalledTimes(configuration.pureRender && !React.lazy ? 3 : 2);
expect(wrapper.contains(second
)).toBe(true);
});
it('renders latest children on receiving cached never-rendered children', () => {
const spy = jest.fn();
const App = () => {
spy();
return hey
;
};
RHL.register(App, 'App', 'test.js');
const element = ;
let wrapper;
{
const App = () => {
spy();
return ho
;
};
RHL.register(App, 'App', 'test.js');
wrapper = mount({element});
}
expect(spy).toHaveBeenCalledTimes(1);
expect(wrapper.contains(ho
)).toBe(true);
});
it('hot-reloads children without losing state', () => {
class App extends Component {
constructor(props) {
super(props);
this.state = { value: 'old' };
}
shouldComponentUpdate() {
return false;
}
render() {
return old render + {this.state.value} state
;
}
}
RHL.register(App, 'App', 'test.js');
const Root = () => ;
RHL.register(Root, 'Root', 'test.js');
const wrapper = mount(
,
);
expect(wrapper.text()).toBe('old render + old state');
{
class App extends Component {
constructor(props) {
super(props);
this.state = { value: 'new' };
}
shouldComponentUpdate() {
return false;
}
render() {
return new render + {this.state.value} state
;
}
}
RHL.register(App, 'App', 'test.js');
const Root = () => ;
RHL.register(Root, 'Root', 'test.js');
wrapper.setProps({ children: });
}
expect(wrapper.text()).toBe('new render + old state');
});
});
it('hot-reloads nested children without losing state', () => {
const onUnmount = jest.fn();
let child = 1;
function getChild() {
if (child === 1) {
return class Child extends Component {
componentWillUnmount() {
onUnmount();
}
render() {
return old child
;
}
};
}
return class Child extends Component {
componentWillUnmount() {
// onUnmount();
}
render() {
return new child
;
}
};
}
class Layout extends Component {
render() {
return (
TEST
{this.props.children}
);
}
}
class App extends Component {
render() {
const Child = getChild();
return (
);
}
}
const Root = () => ;
RHL.register(Root, 'Root', 'test.js');
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(onUnmount).toHaveBeenCalledTimes(0);
child = 2;
// emulate HRM
incrementGeneration();
wrapper.setProps({ children: });
expect(onUnmount).toHaveBeenCalledTimes(0);
expect(wrapper.text()).toBe('TESTnew childnew child');
});
it('hot-reloads nested SFC children without losing state', () => {
const onUnmount = jest.fn();
class MountSpy extends Component {
componentWillUnmount() {
onUnmount();
}
render() {
return spy
;
}
}
let child = 1;
const childs = [
function Child() {
return (
a
);
},
function Child() {
return (
b
);
},
];
function getChild() {
return child === 1 ? childs[0] : childs[1];
}
class Layout extends Component {
render() {
return (
TEST
{this.props.children}
);
}
}
class App extends Component {
render() {
const Child = getChild();
return (
);
}
}
const Root = () => ;
RHL.register(Root, 'Root', 'test.js');
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(onUnmount).toHaveBeenCalledTimes(0);
expect(wrapper.text()).toBe('TESTa spya spy');
child = 2;
// emulate HRM
incrementGeneration();
wrapper.setProps({ children: });
expect(onUnmount).toHaveBeenCalledTimes(0);
expect(wrapper.text()).toBe('TESTb spyb spy');
});
it('unmounts nested in mixed condition', () => {
const onUnmount = jest.fn();
class MountSpy extends Component {
componentWillUnmount() {
onUnmount();
}
render() {
return spy
;
}
}
let child = 1;
const childA = () => (
a
);
class ChildB extends Component {
render() {
return (
b
);
}
}
function getChild() {
return child === 1 ? childA : ChildB;
}
class Layout extends Component {
render() {
return (
TEST
{this.props.children}
);
}
}
class App extends Component {
render() {
const Child = getChild();
return (
);
}
}
const Root = () => ;
RHL.register(Root, 'Root', 'test.js');
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
expect(onUnmount).toHaveBeenCalledTimes(0);
expect(wrapper.text()).toBe('TESTa spya spy');
child = 2;
// emulate HRM
incrementGeneration();
wrapper.setProps({ children: });
expect(onUnmount).toHaveBeenCalledTimes(2);
expect(wrapper.text()).toBe('TESTb spyb spy');
});
it('renders children with chunked re-register', () => {
const spy = jest.fn();
class App extends Component {
componentWillUnmount() {
spy();
}
render() {
return I AM CHILD
;
}
}
RHL.reset();
RHL.register(App, 'App1', 'test.js');
RHL.register(App, 'App2', 'test.js');
const wrapper = mount(
,
);
expect(spy).not.toHaveBeenCalled();
wrapper.setProps({ children: });
expect(spy).not.toHaveBeenCalled();
RHL.register(App, 'App3', 'test.js');
wrapper.setProps({ children: });
expect(spy).not.toHaveBeenCalled();
{
class App extends Component {
componentWillUnmount() {
spy();
}
render() {
return I AM NEW CHILD
;
}
}
RHL.register(App, 'App3', 'test.js');
wrapper.setProps({ children: });
expect(spy).not.toHaveBeenCalled();
RHL.register(App, 'App4', 'test.js');
wrapper.setProps({ children: });
expect(spy).not.toHaveBeenCalled();
}
});
describe('test similarity', () => {
describe('unmounts nested in mixed condition', () => {
/* eslint-disable */
let onUnmount = jest.fn();
class MountSpy extends Component {
componentWillUnmount() {
onUnmount();
}
render() {
return spy
;
}
}
let ChildBase, ChildB, ChildC, ChildD, ChildE, ChildF;
const generateChilds = () => {
ChildBase = class ChildA extends Component {
render() {
return (
b
);
}
};
{
ChildB = class ChildA extends Component {
render() {
return (
NOT A BIG CHANGE!
);
}
};
}
{
ChildC = class ChildA extends Component {
componentWillUnmount() {
// some data
}
render() {
return (
b
);
}
};
}
{
ChildD = class ChildA extends Component {
componentWillUnmount() {
// some data
}
componentWillMount() {
// some data
}
render() {
return (
BIG CHANGE!
);
}
};
}
{
ChildE = class ChildA extends Component {
render() {
return (
SUPER MEGA UBER PUPER BIG CHANGE!
);
}
};
}
{
ChildF = function ChildA() {
return (
b
);
};
}
};
/* eslint-enable */
const expectUnmounts = [false, false, true, true, true];
generateChilds();
[ChildB, ChildC, ChildD, ChildE, ChildF].forEach((Replace, index) => {
const expectUnmount = expectUnmounts[index];
it(`${expectUnmount ? 'expect unmount' : 'keep'} for ${index}`, () => {
generateChilds(); // renegerate base
onUnmount = jest.fn();
let Child = ChildBase;
class App extends Component {
render() {
return (
);
}
}
const wrapper = mount(
,
);
expect(onUnmount).toHaveBeenCalledTimes(0);
// Replace.name=Child.name;
Child = Replace;
incrementGeneration();
wrapper.setProps({ children: });
expect(onUnmount).toHaveBeenCalledTimes(expectUnmount ? 1 : 0);
});
});
});
});
describe('with HOC-wrapped root', () => {
it('renders children', () => {
const spy = jest.fn();
class App extends React.Component {
render() {
spy();
return hey
;
}
}
RHL.register(App, 'App', 'test.js');
const Enhanced = mapProps(props => ({ n: props.n * 5 }))(App);
RHL.register(Enhanced, 'Enhanced', 'test.js');
const wrapper = mount(
,
);
expect(wrapper.find('App').length).toBe(1);
expect(wrapper.contains(hey
)).toBe(true);
expect(wrapper.find('App').prop('n')).toBe(15);
expect(spy).toHaveBeenCalledTimes(1);
});
it('force updates the tree on receiving new children', () => {
const spy = jest.fn();
class App extends React.Component {
render() {
spy();
return hey
;
}
}
RHL.register(App, 'App', 'test.js');
const Enhanced = mapProps(props => ({ n: props.n * 5 }))(App);
RHL.register(Enhanced, 'Enhanced', 'test.js');
const wrapper = mount(
,
);
expect(spy).toHaveBeenCalledTimes(1);
{
class App extends React.Component {
render() {
spy();
return ho
;
}
}
RHL.register(App, 'App', 'test.js');
const Enhanced = mapProps(props => ({ n: props.n * 5 }))(App);
RHL.register(Enhanced, 'Enhanced', 'test.js');
wrapper.setProps({ children: });
}
if (React.memo && !configuration.pureRender) {
expect(spy).toHaveBeenCalledTimes(1 + 2);
}
expect(wrapper.contains(ho
)).toBe(true);
});
it('force updates the tree on receiving cached children', () => {
const firstSpy = jest.fn();
const Dummy = () => {
firstSpy();
return first
;
};
const App = () => ;
RHL.register(Dummy, 'Dummy', 'test.js');
const Enhanced = mapProps(props => ({ n: props.n * 5 }))(App);
RHL.register(Enhanced, 'Enhanced', 'test.js');
const wrapper = mount(
,
);
expect(firstSpy).toHaveBeenCalledTimes(1);
const secondSpy = jest.fn();
{
const Dummy = () => {
secondSpy();
return second
;
};
RHL.register(Dummy, 'Dummy', 'test.js');
const Enhanced = mapProps(props => ({ n: props.n * 5 }))(App);
RHL.register(Enhanced, 'Enhanced', 'test.js');
wrapper.setProps({ children: });
}
expect(firstSpy).toHaveBeenCalledTimes(1);
expect(secondSpy).toHaveBeenCalledTimes(configuration.pureRender && !React.lazy ? 3 : 2);
expect(wrapper.contains(second
)).toBe(true);
});
it('support indeterminateComponent', () => {
[false, true].forEach(withContext => {
const spy = jest.fn();
const AnotherComponent = () => old
;
class App extends React.Component {
static getDerivedStateFromProps({ n }) {
return { n };
}
render() {
return (
);
}
}
// Can't always run polyfill, as running polyfill in React 16 will make
// this test case pass even without the fix.
if (React.version.startsWith('15')) {
polyfill(App);
}
let CurrentApp = App;
RHL.register(AnotherComponent, 'AnotherComponent', 'test.js');
RHL.register(App, 'App', 'test.js');
// return rendered component from a stateless
const IndeterminateComponent = (props, context) => {
const instance = new CurrentApp(props, context);
IndeterminateComponent.getDerivedStateFromProps = CurrentApp.getDerivedStateFromProps;
return instance;
};
if (withContext) {
IndeterminateComponent.contextTypes = {};
}
const RootApp = props => ;
const wrapper = mount(
,
);
expect(wrapper.text()).toBe('hey 42 old');
{
class App2 extends React.Component {
static getDerivedStateFromProps({ n }) {
return { n };
}
render() {
spy();
return ho {this.state.n + 1}
;
}
}
if (React.version.startsWith('15')) {
polyfill(App2);
}
RHL.register(App2, 'App', 'test.js');
CurrentApp = App2;
const AnotherComponent = () => new
;
RHL.register(AnotherComponent, 'AnotherComponent', 'test.js');
wrapper.setProps({ update: true, children: });
// How it works. IndeterminateComponent calculates return value once.
expect(wrapper.text()).toBe('hey 44 new');
expect(spy).toHaveBeenCalledTimes(0); // never gets called
// How it should work
// expect(wrapper.text()).toBe('ho 45 new');
// expect(spy).toHaveBeenCalledTimes(2);
}
});
});
it('renders latest children on receiving cached never-rendered children', () => {
const spy = jest.fn();
class App extends React.Component {
render() {
spy();
return hey
;
}
}
RHL.register(App, 'App', 'test.js');
const Enhanced = mapProps(props => ({ n: props.n * 5 }))(App);
RHL.register(Enhanced, 'Enhanced', 'test.js');
const element = ;
let wrapper;
{
class App extends React.Component {
render() {
spy();
return ho
;
}
}
RHL.register(App, 'App', 'test.js');
const Enhanced = mapProps(props => ({ n: props.n * 5 }))(App);
RHL.register(Enhanced, 'Enhanced', 'test.js');
wrapper = mount({element});
}
expect(spy).toHaveBeenCalledTimes(1);
expect(wrapper.contains(ho
)).toBe(true);
});
it('hot-reloads children with overriden render', () => {
class App extends Component {
constructor() {
super();
this.secret = 1;
this.superSecret = 42;
}
componentWillMount() {
const oldRender = this.render.bind(this);
this.render = () => {
this.superSecret = this.secret + 1;
return PATCHED + {oldRender()}
;
};
}
render() {
return {this.superSecret}
;
}
}
RHL.register(App, 'App', 'test.js');
const MiddleApp = () => (
);
const wrapper = mount(
,
);
expect(wrapper.text()).toBe('PATCHED + 2');
{
class App2 extends Component {
constructor() {
super();
this.secret = 2;
}
render() {
return {this.superSecret * 2} v2
;
}
}
RHL.register(App2, 'App', 'test.js');
wrapper.setProps({ children: });
}
if (!configuration.pureRender) {
expect(wrapper.update().text()).toBe('PATCHED + 6 v2');
}
});
it('hot-reloads children inside simple Fragments', () => {
if (React.version.startsWith('16')) {
const unmount = jest.fn();
class InnerComponent extends Component {
componentWillUnmount() {
unmount();
}
render() {
return internal;
}
}
InnerComponent.displayName = 'InnerComponent';
const App = () => (
);
RHL.register(App, 'App', 'test.js');
const wrapper = mount(
,
);
{
class InnerComponent extends Component {
componentWillUnmount() {
unmount();
}
render() {
return internal;
}
}
InnerComponent.displayName = 'InnerComponent';
const App = () => (
);
RHL.register(App, 'App', 'test.js');
wrapper.setProps({ children: });
}
expect(unmount).toHaveBeenCalledTimes(0);
expect(wrapper.update().text()).toBe('another text internal');
} else {
// React 15 is always ok
expect(true).toBe(true);
}
});
it('hot-reloads children inside Fragments', () => {
if (React.version.startsWith('16')) {
const unmount = jest.fn();
class InnerComponent extends Component {
componentWillUnmount() {
unmount();
}
render() {
return OldInnerComponent
;
}
}
InnerComponent.displayName = 'InnerComponent';
const InnerItem = () => (
{false && hole
}
-1-
{false && hole
}
-3-
);
RHL.register(InnerItem, 'InnerItem', 'test.js');
const Item = () => (
1
3
F
);
//
const App = () => (
);
const wrapper = mount(
,
);
expect(wrapper.update().text()).toBe('1-1-OldInnerComponent-3-OldInnerComponent3F');
{
class InnerComponent extends Component {
componentWillUnmount() {
unmount();
}
render() {
return NewInnerComponent
;
}
}
InnerComponent.displayName = 'InnerComponent';
const InnerItem = () => (
{false && hole
}
-2-
{false && hole
}
-3-
*
);
RHL.register(InnerItem, 'InnerItem', 'test.js');
wrapper.setProps({ children: });
}
expect(unmount).toHaveBeenCalledTimes(0);
expect(wrapper.update().text()).toBe(
'1-2-NewInnerComponent-3-NewInnerComponent*3F', // it should not be so!
);
} else {
// React 15 is always ok
expect(true).toBe(true);
}
});
it('hot-reloads children without losing state', () => {
class App extends Component {
constructor(props) {
super(props);
this.state = { value: 'old' };
}
shouldComponentUpdate() {
return false;
}
render() {
return (
old render + {this.state.value} state + {this.props.n}
);
}
}
RHL.register(App, 'App', 'test.js');
const Enhanced = mapProps(props => ({ n: props.n * 5 }))(App);
RHL.register(Enhanced, 'Enhanced', 'test.js');
const wrapper = mount(
,
);
expect(wrapper.text()).toBe('old render + old state + 15');
{
class App extends Component {
constructor(props) {
super(props);
this.state = { value: 'new' };
}
shouldComponentUpdate() {
return false;
}
render() {
return (
new render + {this.state.value} state + {this.props.n}
);
}
}
RHL.register(App, 'App', 'test.js');
const Enhanced = mapProps(props => ({ n: props.n * 5 }))(App);
RHL.register(Enhanced, 'Enhanced', 'test.js');
wrapper.setProps({ children: });
}
expect(wrapper.text()).toBe('new render + old state + 20');
});
it('should work with react-lifecycle-compact', () => {
class Component extends React.Component {
static getDerivedStateFromProps() {
return {};
}
}
/* eslint-disable no-underscore-dangle */
polyfill(Component);
expect(Component.prototype.componentWillReceiveProps.__suppressDeprecationWarning).toBe(true);
const Proxy = .type;
expect(Proxy.prototype.componentWillReceiveProps.__suppressDeprecationWarning).toBe(true);
/* eslint-enable */
});
});
});