Skip to content

Instantly share code, notes, and snippets.

@atmd83
Last active February 27, 2018 12:16
Show Gist options
  • Select an option

  • Save atmd83/6ef2dc0b2c0c06c6444f7b1ccdc315e4 to your computer and use it in GitHub Desktop.

Select an option

Save atmd83/6ef2dc0b2c0c06c6444f7b1ccdc315e4 to your computer and use it in GitHub Desktop.
medium post 2
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