All files / nexshop-web-contents/spec/action new-playlist-spec.js

100% Statements 93/93
100% Branches 0/0
100% Functions 46/46
100% Lines 93/93
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 1961x 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       1x 1x 1x     1x 1x 1x       1x 1x 1x     1x 1x       1x 1x 1x     1x 1x       1x 1x 1x       1x       1x 3x 3x     1x 3x     1x 1x 3x         3x     1x 3x     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 {contentsActions} from 'nexshop-web-store';
import {playlistService as PlaylistService} from 'nexshop-web-store';
import {
    CREATE_PLAYLIST, createPlaylist,
    DELETE_CONTENT_ITEM, deleteContentItem,
    DROP_DRAG_CONTENTS_ITEM, dropDragContentsItem,
    START_DRAG_CONTENTS_ITEM, startDragContentsItem,
    UPDATE_CONTENT_ITEM_TRANSITION, updateContentItemTransition,
    UPDATE_CONTENT_ITEM_DURATION, updateContentItemDuration,
    CHANGE_CONTENT_ITEMS_ORDER_START, changeContentItemsOrderStart,
    CHANGE_CONTENT_ITEMS_ORDER_PROCESS, changeContentItemsOrderProcess,
    CHANGE_CONTENT_ITEMS_ORDER_END, changeContentItemsOrderEnd,
    DISCARD_PLAYLIST, discardPlaylist, savePlaylistAsync,
} from "../../src/action/playlist";
 
describe('New Playlist Spec', () => {
    describe('CREATE_PLAYLIST', () => {
        it('has type', () => {
            expect(createPlaylist().type).to.equal(CREATE_PLAYLIST);
        });
 
        it('has name', () => {
            expect(createPlaylist('some title').name).to.equals('some title');
        });
 
        it('has resolution info', () => {
            const mockData = {resolution: "i am resolution"};
 
            expect(createPlaylist('name', mockData).resolution).to.equal(mockData);
        });
    });
 
    describe('START_DRAG_CONTENTS_ITEM', () => {
        it('has type', () => {
            expect(startDragContentsItem('').type).to.equal(START_DRAG_CONTENTS_ITEM);
        });
 
        it('has contentsItem object', () => {
            expect(startDragContentsItem('some contentsItem').contentsItem).to.equal('some contentsItem');
        });
    });
 
    describe('DROP_DRAG_CONTENTS_ITEM', () => {
        it('has type', () => {
            expect(dropDragContentsItem().type).to.equal(DROP_DRAG_CONTENTS_ITEM);
        });
    });
 
    describe('DELETE_CONTENT_ITEM', () => {
        it('has type', () => {
            expect(deleteContentItem(NaN).type).to.equal(DELETE_CONTENT_ITEM);
        });
 
        it('has index', () => {
            expect(deleteContentItem(13).index).to.equal(13);
        });
    });
 
    describe('CHANGE_CONTENT_ITEMS_ORDER_START', () => {
        it('has type', () => {
            expect(changeContentItemsOrderStart(NaN).type).to.equal(CHANGE_CONTENT_ITEMS_ORDER_START);
        });
 
        it('has index', () => {
            expect(changeContentItemsOrderStart(14).index).to.equal(14);
        });
    });
 
    describe('CHANGE_CONTENT_ITEMS_ORDER_PROCESS', () => {
        it('has type', () => {
            expect(changeContentItemsOrderProcess(NaN).type).to.equal(CHANGE_CONTENT_ITEMS_ORDER_PROCESS);
        });
 
        it('has index', () => {
            expect(changeContentItemsOrderProcess(15).index).to.equal(15);
        });
    });
 
    describe('CHANGE_CONTENT_ITEMS_ORDER_END', () => {
        it('has type', () => {
            expect(changeContentItemsOrderEnd().type).to.equal(CHANGE_CONTENT_ITEMS_ORDER_END);
        });
    });
 
    describe('UPDATE_CONTENT_ITEM_TRANSITION', () => {
        it('has type', () => {
            expect(updateContentItemTransition().type).to.equal(UPDATE_CONTENT_ITEM_TRANSITION);
        });
 
        it('has index', () => {
            expect(updateContentItemTransition('1123123', 'No Effect').uuid).to.equal('1123123');
            expect(updateContentItemTransition('1123123', 'No Effect').transitionEffect).to.equal('No Effect');
        });
    });
 
    describe('UPDATE_CONTENT_ITEM_DURATION', () => {
        it('has type', () => {
            expect(updateContentItemDuration().type).to.equal(UPDATE_CONTENT_ITEM_DURATION);
        });
 
        it('has index', () => {
            expect(updateContentItemDuration('1123123', 30).uuid).to.equal('1123123');
            expect(updateContentItemDuration('1123123', 30).durationSeconds).to.equal(30);
        });
    });
 
    describe('CHANGE_CONTENT_ITEMS_ORDER_START', () => {
        it('has type', () => {
            expect(changeContentItemsOrderStart().type).to.equal(CHANGE_CONTENT_ITEMS_ORDER_START);
        });
 
        it('has index', () => {
            expect(changeContentItemsOrderStart(1).index).to.equal(1);
        });
    });
 
    describe('CHANGE_CONTENT_ITEMS_ORDER_PROCESS', () => {
        it('has type', () => {
            expect(changeContentItemsOrderProcess().type).to.equal(CHANGE_CONTENT_ITEMS_ORDER_PROCESS);
        });
 
        it('has index', () => {
            expect(changeContentItemsOrderProcess(1).index).to.equal(1);
        });
    });
 
    describe('CHANGE_CONTENT_ITEMS_ORDER_END', () => {
        it('has type', () => {
            expect(changeContentItemsOrderEnd().type).to.equal(CHANGE_CONTENT_ITEMS_ORDER_END);
        });
    });
 
    describe('Async test', () => {
        let spyDispatch;
        let action;
 
        beforeEach(() => {
            sinon.stub(global, "alert");
            spyDispatch = sinon.spy();
        });
 
        afterEach(() => {
            global.alert.restore();
        });
 
        describe('savePlaylistAsync', () => {
            beforeEach(() => {
                sinon.stub(PlaylistService, "savePlaylist").returns(Promise.resolve({
                    id: 'random-id',
                    name: 'playlist'
                }));
 
                action = savePlaylistAsync([{name: 'playlist'}]);
            });
 
            afterEach(() => {
                PlaylistService.savePlaylist.restore();
            });
 
            it('calls savePlaylist from Playlist Service', async () => {
                await action(spyDispatch);
 
                expect(PlaylistService.savePlaylist.calledWith([{name: 'playlist'}])).to.be.true;
            });
 
            it('dispatches addContentsItem with playlist returned from playlist service', async () => {
                await action(spyDispatch);
                expect(spyDispatch.calledWith({
                    type: contentsActions.ADD_CONTENTS_ITEM,
                    data: {id: 'random-id', name: 'playlist'},
                    index: 0
                })).to.be.true;
            });
 
            it('show alert popup when savePlaylist is rejected', async () => {
                PlaylistService.savePlaylist.restore();
                sinon.stub(PlaylistService, 'savePlaylist').returns(Promise.reject({errorMessage: 'error!!'}));
 
                action = savePlaylistAsync([{name: 'playlist'}]);
                await action(spyDispatch);
 
                expect(spyDispatch.called).to.be.false;
                expect(global.alert.calledWith('error!!')).to.be.true;
            });
        });
    });
 
    describe('DISCARD_PLAYLIST', () => {
        it('has type', () => {
            expect(discardPlaylist().type).to.equal(DISCARD_PLAYLIST);
        });
    });
});