Skip to content

Instantly share code, notes, and snippets.

@redonkulus
Last active March 14, 2018 19:57
Show Gist options
  • Save redonkulus/f60372c4b1091f5a8440364e69688cc6 to your computer and use it in GitHub Desktop.
Save redonkulus/f60372c4b1091f5a8440364e69688cc6 to your computer and use it in GitHub Desktop.
Simple Enzyme shallow componentDidUpdate test
import React, { Component } from 'react';
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({
adapter: new Adapter(),
disableLifecycleMethods: false
});
var didItGetCalled = false;
class Test extends Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
this.state = {
isFoo: false
};
}
handleClick(e) {
debugger;
this.setState({
isFoo: !this.state.isFoo
});
}
componentDidUpdate() {
console.log('did update');
didItGetCalled = true;
}
render() {
return (
<div>
Foo? = {this.state.isFoo}
<button onClick={this.handleClick}>click me</button>
</div>
);
}
}
describe('#test', () => {
it('should call componentDidUpdate', () => {
const wrapper = Enzyme.shallow(
React.createElement(Test)
);
wrapper.find('button').simulate('click');
assert(wrapper.state('isFoo'), 'should be true to ensure click is happening');
assert(didItGetCalled, 'componentDidUpdate should have been called');
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment