All files / nexshop-web-contents/spec/component contents-router-spec.js

100% Statements 72/72
100% Branches 0/0
100% Functions 18/18
100% Lines 72/72
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 1361x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x           1x 11x 11x 11x   11x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x         1x     1x 1x 1x   1x 1x       1x               1x 2x 2x 2x 2x   2x 2x 2x   2x   2x                 2x     1x 2x     1x 1x 1x   1x     1x 1x 1x   1x          
import React from 'react';
import {expect} from 'chai';
import {shallow} from 'enzyme';
import {Redirect, Route} from 'react-router-dom';
import {createMemoryHistory} from 'history';
import sinon from 'sinon';
import * as Redux from 'redux';
import ContentsRouter from '../../src/component/contents-router';
import ContentsMain from '../../src/component/contents-main';
import Playlist from '../../src/component/playlist/playlist';
import PlaylistPreview from '../../src/component/preview/playlist/playlist-preview';
import ContentsPreview from "../../src/component/preview/contents/contents-preview";
import ScenePreview from "../../src/component/preview/scene/scene-preview";
import {closeFloatingView} from "../../src/action/floating-view";
import Scene from "../../src/component/scene/scene";
 
describe('Contents Router Spec', () => {
    let wrapper;
    let mockActions;
    let history;
    let match;
 
    beforeEach(() => {
        history = createMemoryHistory('/');
        mockActions = {closeFloatingView: sinon.spy()};
        match = { url: '/contents' };
 
        wrapper = shallow(<ContentsRouter.WrappedComponent history={history} actions={mockActions} match={match}/>);
    });
 
    it('has path as "/contents/main" and ContentsMain component', () => {
        expect(wrapper.find(Route).at(0).prop('path')).to.equal('/contents/main');
        expect(wrapper.find(Route).at(0).prop('component')).to.equal(ContentsMain);
    });
 
    it('has path as "/contents/create-playlist" and Playlist component', () => {
        expect(wrapper.find(Route).at(1).prop('path')).to.equal('/contents/create-playlist');
        expect(wrapper.find(Route).at(1).prop('component')).to.equal(Playlist);
    });
 
    it('has path as "/contents/playlist-detail" and Playlist component', () => {
        expect(wrapper.find(Route).at(2).prop('path')).to.equal('/contents/playlist-detail');
        expect(wrapper.find(Route).at(2).prop('component')).to.equal(Playlist);
    });
 
    it('has path as "/contents/playlist-preview" and PlaylistPreview component', () => {
        expect(wrapper.find(Route).at(3).prop('path')).to.equal('/contents/playlist-preview');
        expect(wrapper.find(Route).at(3).prop('component')).to.equal(PlaylistPreview);
    });
 
    it('has path as "/contents/contents-preview" and ContentsPreview component', () => {
        expect(wrapper.find(Route).at(4).prop('path')).to.equal('/contents/contents-preview');
        expect(wrapper.find(Route).at(4).prop('component')).to.equal(ContentsPreview);
    });
 
    it('has path as "/contents/scene" and Scene component', () => {
        expect(wrapper.find(Route).at(5).prop('path')).to.equal('/contents/scene');
        expect(wrapper.find(Route).at(5).prop('component')).to.equal(Scene);
    });
 
    it('has path as "/contents/scene-preview" and Scene component', () => {
        expect(wrapper.find(Route).at(6).prop('path')).to.equal('/contents/scene-preview');
        expect(wrapper.find(Route).at(6).prop('component')).to.equal(ScenePreview);
    });
 
    it('redirect to contents main when url with trailing slash', () => {
         wrapper.setProps({
             match: {
                 url: '/contents/'
             }
         });
        expect(wrapper.find(Redirect).props().to).to.equal('/contents/main');
    });
 
    describe('change location', () => {
        it('close floating popup', () => {
            wrapper.instance().componentDidMount();
 
            history.push('/changedUrl');
            expect(mockActions.closeFloatingView.called).to.be.true;
        });
    });
 
    describe('Mapping props', () => {
        let spyCloseFloatingView;
        let spyFetchContentsAsync;
        let contentRouter;
        let spyConnect;
        let spyWithRouter;
        let mapDispatchToProps;
 
        beforeEach(() => {
            let proxyquire = require('proxyquire');
            spyConnect = sinon.stub();
            spyWithRouter = sinon.spy();
            spyConnect.returns(() => {
            });
            spyFetchContentsAsync = sinon.stub();
            spyCloseFloatingView = sinon.stub();
            mockActions = {closeFloatingView: spyCloseFloatingView, fetchContentsAsync: spyFetchContentsAsync};
 
            sinon.stub(Redux, 'bindActionCreators').returns(mockActions);
 
            contentRouter = proxyquire('../../src/component/contents-router', {
                'react-redux': {
                    'connect': spyConnect
                },
                'react-router-dom': {
                    'withRouter': spyWithRouter
                }
            });
 
            mapDispatchToProps = spyConnect.lastCall.args[1];
        });
 
        afterEach(() => {
            Redux.bindActionCreators.restore();
        });
 
        it('binds actionCreator with closeFloatingView', () => {
            let spyDispatch = sinon.spy();
            mapDispatchToProps(spyDispatch);
 
            expect(Redux.bindActionCreators.calledWith({closeFloatingView}, spyDispatch)).to.be.true;
        });
 
        it('returns props including bound actions', () => {
            let spyDispatch = sinon.spy();
            let actualProps = mapDispatchToProps(spyDispatch);
 
            expect(actualProps).to.deep.equal({
                actions: mockActions
            });
        });
    });
});