-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3.4.3 breaks tests with "ShallowWrapper::setState() can only be called on root" #1765
Comments
@monken Could you create a minimal test case? I can't try your code. The following test is passed with v3.4.3, which is an expected behavior. it('should call `componentDidUpdate` when component’s `setState` is called2', () => {
class Foo extends React.Component {
constructor(props) {
super(props);
this.state = {
foo: 'init',
};
}
componentDidMount() {
this.setState({ foo: 'update' });
}
render() {
return <div>{this.state.foo}</div>;
}
}
const spy = sinon.spy(Foo.prototype, 'componentDidMount');
const wrapper = shallow(<Foo />, { disableLifecycleMethods: true });
expect(spy).to.have.property('callCount', 0);
wrapper.instance().componentDidMount();
wrapper.update();
expect(spy).to.have.property('callCount', 1);
}); |
@monken can you share the code for
|
import React, { Fragment, Component } from 'react';
import { shallow } from 'enzyme';
import sinon from 'sinon';
class Table extends Component {
render() {
return (<table />);
}
}
class MyComponent extends Component {
state = {
showTable: false,
}
componentDidMount() {
this.setState({ showTable: true });
}
render() {
const { showTable } = this.state;
return (<Fragment>{showTable ? <Table /> : null}</Fragment>);
}
}
describe('<MyContainer />', () => {
it('should call `componentDidUpdate` when component’s `setState` is called', () => {
const wrapper = shallow(<MyComponent />, { disableLifecycleMethods: true });
expect(wrapper.find(Table).length).toEqual(0);
wrapper.instance().componentDidMount();
wrapper.update();
expect(wrapper.find(Table).length).toEqual(1);
});
}); Output:
|
This will be fixed by #1768 |
When is this going to npm? |
@koba04 thanks for the quick turn-around on this! |
Describe the bug
Not sure why, but the 3.4.3 release broke my tests. I receive Error: ShallowWrapper::setState() can only be called on the root errors all over the place. Seems to be related to #1756.
The relevant test is attached. I was able to figure out that componentDidMount is called twice with the 3.4.3 release, which was not the case before.
To Reproduce
The second call to
componentDidMount
has the following stack trace:The text was updated successfully, but these errors were encountered: