import React from 'react';
import {expect} from 'chai';
import sinon from "sinon";
import {shallow} from "enzyme";
import popupEnhancer from "../../src/enhancers/popup-enhancer";
describe('Popup Enhancer Spec', () => {
const DummyComponent = (props) => (<div>dummy component</div>);
const PopupEnhancer = popupEnhancer(DummyComponent, 'add-menu');
let wrapper;
let spyClose;
beforeEach(() => {
spyClose = sinon.spy();
wrapper = shallow(<PopupEnhancer close={spyClose} someProp="should have this"/>);
sinon.spy(window, 'addEventListener');
sinon.spy(window, 'removeEventListener');
});
afterEach(() => {
window.addEventListener.restore();
window.removeEventListener.restore();
});
it('passes props to child component', () => {
expect(wrapper.find(DummyComponent).prop('someProp')).to.equal('should have this');
});
it('has child component', () => {
expect(wrapper.find(DummyComponent).html()).to.include('dummy component');
});
it('calls addEventListener with props.onChange when component is mounted', () => {
wrapper.instance().componentDidMount();
expect(window.addEventListener.calledWith('click')).to.be.true;
});
it('calls removeEventListener with props.onChange when component is unmounted', () => {
wrapper.instance().componentWillUnmount();
expect(window.removeEventListener.calledWith('click')).to.be.true;
});
it('e.stopPropagation when handleClick is called', () => {
let spyStopPropagation = sinon.spy();
wrapper.instance().handleClick({stopPropagation: spyStopPropagation, path: []});
expect(spyStopPropagation.called).to.be.true;
});
it('calls closePopup action when handleClick is called with node does not have "add-menu" in path', () => {
let spyStopPropagation = sinon.spy();
wrapper.instance().handleClick({
stopPropagation: spyStopPropagation,
path: []
});
expect(spyClose.called).to.be.true;
});
it('does not call closePopup action when handleClick is called with node has "add-menu" exist in path', () => {
let spyStopPropagation = sinon.spy();
wrapper.instance().handleClick({
stopPropagation: spyStopPropagation,
path: [{
className: 'add-menu'
}]
});
expect(spyClose.called).to.be.false;
});
}); |