Last active
March 14, 2018 19:57
-
-
Save redonkulus/f60372c4b1091f5a8440364e69688cc6 to your computer and use it in GitHub Desktop.
Simple Enzyme shallow componentDidUpdate test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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