Last active
February 27, 2018 12:16
-
-
Save atmd83/6ef2dc0b2c0c06c6444f7b1ccdc315e4 to your computer and use it in GitHub Desktop.
medium post 2
This file contains hidden or 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 from 'react'; | |
| import PubSub from 'pubsub-js'; | |
| import { configure, shallow } from 'enzyme'; | |
| import Adapter from 'enzyme-adapter-react-16'; | |
| configure({ adapter: new Adapter() }); | |
| import Thing from './thing'; | |
| describe('thing', () => { | |
| let component; | |
| beforeEach(() => { | |
| spyOn(PubSub, 'publish'); | |
| }); | |
| describe('when the lifecycle events trigger', () => { | |
| it('will publish the correct message', () => { | |
| // we are ignoring componentWillMount() for now | |
| component = shallow(<Thing />); | |
| // rendering the component triggers componentDidMount() | |
| expect(PubSub.publish).toHaveBeenCalledWith('thing.did.mount'); | |
| // updating the props triggers: | |
| // componentWillReceiveProps() | |
| // shouldComponentUpdate() | |
| // componentWillUpdate() | |
| component.setProps({}); | |
| expect(PubSub.publish).toHaveBeenCalledWith('thing.will.receive.props'); | |
| expect(PubSub.publish).toHaveBeenCalledWith('thing.should.update'); | |
| expect(PubSub.publish).toHaveBeenCalledWith('thing.will.update'); | |
| // when the component unmounts it triggers componentWillUnmount() | |
| component.unmount(); | |
| expect(PubSub.publish).toHaveBeenCalledWith('thing.will.unmount'); | |
| }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment