All files / nexshop-web-contents/spec/enhancers popup-enhancer-spec.js

100% Statements 39/39
100% Branches 0/0
100% Functions 11/11
100% Lines 38/38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 751x 1x 1x 1x 1x   1x 1x 1x         1x 7x 7x   7x 7x     1x 7x 7x     1x 1x     1x 1x     1x 1x   1x     1x 1x   1x     1x 1x 1x   1x     1x 1x 1x         1x     1x 1x 1x             1x    
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;
    });
});