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

96.15% Statements 125/130
100% Branches 0/0
91.43% Functions 32/35
96.12% Lines 124/129
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 2641x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x       1x   1x   1x                                                                   1x         1x 19x 19x 19x 19x 19x 19x   19x   19x               19x 19x 19x   19x   19x 19x     1x 19x 19x 19x 19x     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 1x   1x 1x       1x 1x 1x 1x   1x     1x 1x 1x   1x       1x     1x 2x 2x     1x 1x     1x 1x 1x       1x 1x   1x 1x 1x     1x 1x 1x 1x 1x   1x 1x 1x 1x 1x       1x   1x 2x     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 {shallow} from 'enzyme';
import configureStore from 'redux-mock-store';
import * as Redux from "redux";
import sinon from "sinon";
import {actions as AlertActions, DiscardRefresh} from "nexshop-web-alert";
import {ContentsMini} from "nexshop-web-contents-mini-view";
import {playlistActions, contentItemModel} from "nexshop-web-store";
import Playlist from "../../../src/component/playlist/playlist";
import PlayListTitleView from "../../../src/component/playlist/playlist-title-view";
import PlaylistEditor from "../../../src/component/playlist/playlist-editor/playlist-editor";
 
describe('Playlist Spec', () => {
    const CONTENT_ITEMS = Object.freeze([
        contentItemModel.createContentItem({thumbnailUrls: {thumbnail: 'some nail'}}, 5),
        contentItemModel.createContentItem('contentItem2', 30)]
    );
    const CURRENT_FOLDER = {id: 'folderId'};
 
    const {discardPlaylist} = playlistActions;
 
    const DEFAULT_DATA = Object.freeze({
        playlist: {
            name: 'playlist title',
            contentItems: CONTENT_ITEMS,
            resolution: {value: "QHD", text: "2560x1440", orientation: "vertical"},
        },
        update_playlist: {
            name: 'playlist title',
            contentItems: CONTENT_ITEMS,
            resolution: {value: "QHD", text: "2560x1440", orientation: "vertical"},
            id: "some id",
            type: "video",
            durationSeconds: 30,
            transitionEffect: "none",
            fitMode: "contain"
        },
        contents: {
            breadcrumb: [
                CURRENT_FOLDER
            ]
        }
    });
 
    let wrapper;
    let instance;
    let mockStore;
    let spyBlockNavigation;
    let spyUnblockNavigation;
    let spySavePlaylistAsync;
    let spyMoveToPreview;
    let spyCreatePlaylistPreview;
    let spyOpenDialog;
    let stubUpdatePlaylistAsync;
 
    let mockHistory = {
        push: () => {
        }
    };
 
    beforeEach(() => {
        spySavePlaylistAsync = sinon.spy();
        spyBlockNavigation = sinon.spy();
        spyUnblockNavigation = sinon.spy();
        spyMoveToPreview = sinon.spy();
        spyCreatePlaylistPreview = sinon.spy();
        spyOpenDialog = sinon.spy();
 
        stubUpdatePlaylistAsync = sinon.stub().returns(Promise.resolve({occupied: true}));
 
        sinon.stub(Redux, "bindActionCreators").returns({
            savePlaylistAsync: spySavePlaylistAsync,
            blockNavigation: spyBlockNavigation,
            unblockNavigation: spyUnblockNavigation,
            createPlaylistPreview: spyCreatePlaylistPreview,
            openDialog: spyOpenDialog,
            updatePlaylistAsync: stubUpdatePlaylistAsync,
        });
        sinon.stub(mockHistory, 'push').returns(sinon.spy());
        sinon.stub(global, 'setTimeout');
        sinon.stub(document, 'getElementsByClassName').returns([{getClientRects: () => [{width: 16, height: 9}]}]);
 
        mockStore = configureStore()(DEFAULT_DATA);
 
        wrapper = shallow(<Playlist moveToPreview={spyMoveToPreview} store={mockStore} history={mockHistory}/>).dive();
        instance = wrapper.instance();
    });
 
    afterEach(() => {
        Redux.bindActionCreators.restore();
        mockHistory.push.restore();
        global.setTimeout.restore();
        document.getElementsByClassName.restore();
    });
 
    describe('Mapping props and actions', () => {
        it('has props.name', () => {
            expect(wrapper.instance().props.name).to.equal('playlist title');
        });
 
        it('has props.contentItems', () => {
            expect(wrapper.instance().props.contentItems).to.deep.equal(DEFAULT_DATA.playlist.contentItems);
        });
 
        it('has props.breadcrumb', () => {
            expect(wrapper.instance().props.breadcrumb[0]).to.deep.equal(CURRENT_FOLDER);
        });
 
        it('binds actionCreator with blockNavigation and unblockNavigation', () => {
            let actions = Redux.bindActionCreators.lastCall.args[0];
            const actionsKeys = Object.keys(actions);
            expect(actionsKeys).to.deep.equal(['savePlaylistAsync', 'updatePlaylistAsync',
                'openDialog', 'createPlaylistPreview', 'blockNavigation', 'unblockNavigation']);
 
            expect(actions.blockNavigation).to.equal(AlertActions.blockNavigation);
            expect(actions.unblockNavigation).to.equal(AlertActions.unblockNavigation);
        });
 
        it('has actions', () => {
            expect(wrapper.instance().props.actions).to.deep.equal({
                blockNavigation: spyBlockNavigation,
                createPlaylistPreview: spyCreatePlaylistPreview,
                unblockNavigation: spyUnblockNavigation,
                savePlaylistAsync: spySavePlaylistAsync,
                openDialog: spyOpenDialog,
                updatePlaylistAsync: stubUpdatePlaylistAsync,
            });
        });
    });
 
    describe('rendering', () => {
        it('has title and editor component with "playlist-title", "playlist-editor" and  "contents-mini"', () => {
            expect(wrapper.find(PlayListTitleView).length).to.equals(1);
            expect(wrapper.find(PlaylistEditor).length).to.equals(1);
            expect(wrapper.find(ContentsMini).length).to.equals(1);
            expect(wrapper.find(DiscardRefresh).length).to.equals(1);
        });
 
        it('passes title and onPreviewClick to PlayListTitleView as props', () => {
            expect(wrapper.find(PlayListTitleView).prop('name')).to.equal('playlist title');
            expect(wrapper.find(PlayListTitleView).prop('onPreviewClick')).to.equal(wrapper.instance().moveToPreview);
        });
    });
 
    it('calls blockNavigation action when componentDidMount is called', () => {
        wrapper.instance().componentDidMount();
 
        expect(spyBlockNavigation.calledWith('Your work has not been saved.', discardPlaylist())).to.be.true;
    });
 
    it('calls unblockNavigation action when componentWillUnmount is called', () => {
        wrapper.instance().componentWillUnmount();
 
        expect(spyUnblockNavigation.called).to.be.true;
    });
 
    it('calls unblockNavigation, createPreview action when moveToPreview is called', () => {
        wrapper.instance().moveToPreview();
 
        expect(spyUnblockNavigation.called).to.be.true;
        expect(spyCreatePlaylistPreview.calledWith(DEFAULT_DATA.playlist.name, DEFAULT_DATA.playlist.contentItems, 0,
            {width: 16, height: 9}, {value: "QHD", text: "2560x1440", orientation: "vertical"})).to.be.true;
    });
 
    describe('calls history.push with "/contents/main"', () => {
        it('when title is undefined', () => {
            wrapper.setProps({title: '', resolution: {}});
            wrapper.instance().componentWillMount();
 
            expect(mockHistory.push.calledWith('/contents/main'));
        });
 
        it('when resolution is undefined', () => {
            wrapper.setProps({title: 'some title', resolution: undefined});
            wrapper.instance().componentWillMount();
 
            expect(mockHistory.push.calledWith('/contents/main'));
        });
    });
 
    describe('onPreviewClick', () => {
        let setTimeoutArgument;
 
        beforeEach(() => {
            wrapper.instance().moveToPreview();
            setTimeoutArgument = global.setTimeout.lastCall.args[0];
        });
 
        it('calls unblockNavigation', () => {
            expect(spyUnblockNavigation.called).to.be.true;
        });
 
        it('sets history.push', () => {
            setTimeoutArgument();
            expect(mockHistory.push.calledWith('/contents/playlist-preview')).to.be.true;
        });
    });
 
    describe('onSavePlayList', () => {
        describe('playlist save', () => {
 
            it('call savePlaylistAsync', () => {
                instance.onSavePlaylist();
                expect(spySavePlaylistAsync.called).to.be.true;
            });
 
            it('call savePlayListAsync with args', () => {
                instance.onSavePlaylist();
                let args = spySavePlaylistAsync.getCall(0).args[0];
                let playlist = DEFAULT_DATA.playlist;
                const [width, height] = playlist.resolution.text.split('x');
 
                expect(args.name).to.be.equal(playlist.name);
                expect(args.folderId).to.be.equal(CURRENT_FOLDER.id);
                expect(args.metaData.display.resolution.width).to.be.equal(width);
                expect(args.metaData.display.resolution.height).to.be.equal(height);
                expect(args.thumbnailUrls.thumbnail).to.be.equal(playlist.contentItems[0].contents.thumbnailUrls.thumbnail);
            });
        });
 
        describe('playlist update', () => {
 
            beforeEach(() => {
                wrapper.setProps({id: "some id"});
            });
 
            it('call updatePlaylistAsync when contents id is exist', () => {
                it('without dialog', () => {
                    instance.onSavePlaylist();
 
                    expect(stubUpdatePlaylistAsync.called).to.be.true;
                });
 
                it('show update contents item dialog but the playlist is used in others', () => {
                    stubUpdatePlaylistAsync = sinon.stub().returns(Promise.resolve({occupied: true}));
 
                    instance.onSavePlaylist();
 
                    expect(spyOpenDialog.calledWith('update-contents-item')).to.true;
                });
            });
 
            it('call updatePlaylistAsync with args when contents id is exist', () => {
                instance.onSavePlaylist();
                let args = stubUpdatePlaylistAsync.getCall(0).args[0];
                let playlist = DEFAULT_DATA.update_playlist;
                const [width, height] = playlist.resolution.text.split('x');
 
                expect(args.name).to.be.equal(playlist.name);
                expect(args.metaData.display.resolution.width).to.be.equal(width);
                expect(args.metaData.display.resolution.height).to.be.equal(height);
                expect(args.thumbnailUrls.thumbnail).to.be.equal(playlist.contentItems[0].contents.thumbnailUrls.thumbnail);
                expect(args.metaData.contentItems[0].id).to.be.equal(playlist.contentItems[0].id);
            });
        });
    });
 
    it('getRelatedContents', () => {
        const expected = [{id: '3'}, {id: '2'}, {id: '1'}];
        expect(instance.getRelatedContents([{contents: {id: '3'}},
            {contents: {id: '2', relatedContents: [{id: '3'}, {id: '1'}]}}])).to.deep.equal(expected);
    });
});