-
-
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
mount() wrapper is populated before componentDidMount() #1708
Comments
I think that's the correct behavior - If you add |
I hear what you're saying, but because Whether or not it ends up being correct behavior, I can't use Enzyme selectors / inspection methods to assert the mounted DOM here. Not sure if that's a React 15 issue that will be solved by bumping to React 16 and corresponding Enzyme or not... |
@ericsoco so the real issue here is that you're using DOM methods to directly manipulate the DOM, which is an antipattern in react. Instead, use state, and render as jsx the new div. This passes: it('works', () => {
class MyComponent extends React.Component {
constructor(...args) {
super(...args);
this.state = { newInner: false };
}
componentDidMount() {
this.setState({ newInner: true });
}
render() {
const { newInner } = this.state;
return (
<div
ref={this.containerRef}
className="outer"
>
<div className="inner" />
{newInner && <div className="new-inner" />}
</div>
);
}
}
const wrapper = mount(<MyComponent />);
expect(wrapper.find('.outer')).to.have.lengthOf(1);
expect(wrapper.find('.inner')).to.have.lengthOf(1);
expect(wrapper.find('.new-inner')).to.have.lengthOf(1);
}); In other words, enzyme is meant to test the React tree - and explicitly not to test the actual DOM. If you're doing any direct DOM manipulation, you're stepping outside of the box React wants you to stay inside - see https://github.com/airbnb/enzyme/blob/master/docs/common-issues.md#testing-third-party-libraries. |
Throwing an error of : |
Describe the bug
When
mount()
ing a component with acomponentDidMount()
method that appends DOMElements to a container element created inrender()
, the resulting wrapper does not contain those DOMElements.To Reproduce
I've confirmed that calling
wrapper.update()
does runrender()
a second time, but still does not update the wrapper. This makes me wonder if this is the same bug as #1153.Expected behavior
Since
mount()
synchronously callscomponentDidMount()
, I expect the resulting wrapper to reflect the state of the component aftercomponentDidMount()
. Failing that,wrapper.update()
should force a wrapper update.Desktop (please complete the following information):
enzyme@3.3.0
enzyme-adapter-react-15.4@1.0.5
The text was updated successfully, but these errors were encountered: