import { mount, shallow } from 'enzyme';
import sinon from 'sinon';
import React from 'react';
import { Modal } from 'web-foundation';


describe('<Modal />', function() {
    it('should render properly', function() {
        const wrapper = mount(<Modal />);

        expect(wrapper.type()).to.equal(Modal);
    });

    it('should open when told to', function() {
        const wrapper = shallow(<Modal />);

        expect(wrapper.find('.modal')).to.have.length(0);

        wrapper.setProps({
            isOpen: true
        });

        expect(wrapper.find('.modal')).to.have.length(1);
    });

    it('should use children', function() {
        const wrapper = mount(
            <Modal isOpen>
                <p>modal</p>
            </Modal>
        );

        expect(wrapper.contains(<p>modal</p>)).to.be.true;
    });

    it('should unmount without errors', function() {
        const wrapper = mount(<Modal isOpen />);

        wrapper.instance().componentWillUnmount();
    });

    it('should close when clicked outside', function() {
        const spy = sinon.spy(),
            wrapper = mount(
                <Modal
                    isOpen
                    closeFunc={ spy } />
            );

        wrapper.find('.modal-dialog').simulate('click');
        expect(spy.callCount).to.equal(0);
        wrapper.find('.modal').simulate('click');
        expect(spy.callCount).to.equal(1);
    });

    it('should call closeFunc when closing', function() {
        const spy = sinon.spy(),
            wrapper = mount(<Modal
                isOpen
                closeFunc={ spy } />);

        wrapper.instance().closeModal();
        expect(spy.callCount).to.equal(1);
    });

    it('should ignore closeFunc when closed', function() {
        const spy = sinon.spy(),
            wrapper = mount(<Modal
                closeFunc={ spy } />);

        wrapper.instance().closeModal();
        expect(spy.callCount).to.equal(0);
    });

    it('should ignore invalid closeFunc props', function() {
        const wrapper = mount(<Modal
            isOpen
            closeFunc={ 1 } />);

        wrapper.instance().closeModal();
    });
});
