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

100% Statements 110/110
100% Branches 0/0
100% Functions 42/42
100% Lines 110/110
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 2501x 1x 1x 1x 1x 1x               1x 1x                     1x             1x   1x 1x 1x   1x       1x 1x 1x   1x       1x 1x 1x   1x       1x 1x 5x     1x 5x     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     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     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      
import React from 'react';
import {expect} from 'chai';
import sinon from 'sinon';
import {playlist} from "../../src/reducer/playlist";
import * as ContentModel from "../../src/model-factory/content-item";
import {
    CHANGE_CONTENT_ITEMS_ORDER_END, CHANGE_CONTENT_ITEMS_ORDER_PROCESS,
    CHANGE_CONTENT_ITEMS_ORDER_START, CREATE_PLAYLIST,
    DELETE_CONTENT_ITEM, DISCARD_PLAYLIST,
    DROP_DRAG_CONTENTS_ITEM, START_DRAG_CONTENTS_ITEM,
    UPDATE_CONTENT_ITEM_DURATION, UPDATE_CONTENT_ITEM_TRANSITION,
} from "../../src/action/playlist";
 
describe('New Playlist Reducer Spec', () => {
    const INITIAL_STATE = {
        name: '',
        resolution: {},
        contentItems: [],
        willBeReordered: {},
        blankIndex: -1,
        dragContentsItem: {},
        selectedPreviewItemIndex: 0,
        selectedPreviewPreviousItemIndex: -1,
    };
 
    const SECOND_CONTENT_IS_TO_BE_REORDERED = {
        contentItems: [{name: "media1", uuid: "1"}, {}, {name: "media3", uuid: "3"}],
        blankIndex: 1,
        willBeReordered: {name: 'media2', uuid: "2"},
        selectedPreviewPreviousItemIndex: 2,
    };
 
    let firstItem = SECOND_CONTENT_IS_TO_BE_REORDERED.contentItems[0];
 
    describe('CREATE_PLAYLIST Action', () => {
        it('change state "name" with action.name and resolution with action.resolution', () => {
            let nextState = playlist({}, {type: CREATE_PLAYLIST, name: 'playlist title', resolution: 'resolution'});
 
            expect(nextState).to.deep.equal({name: 'playlist title', resolution: 'resolution'});
        });
    });
 
    describe('Default Action', () => {
        it('returns initialState', () => {
            let nextState = playlist({}, {type: '', name: 'playlist title', resolution: 'resolution'});
 
            expect(nextState).to.deep.equal({});
        });
    });
 
    describe('START_DRAG_CONTENTS_ITEM Action', () => {
        it('store drag contentsItem', () => {
            let nextState = playlist({}, {type: START_DRAG_CONTENTS_ITEM, contentsItem: 'some contentsItem'});
 
            expect(nextState).to.deep.equals({dragContentsItem: "some contentsItem"});
        });
    });
 
    describe('DROP_DRAG_CONTENTS_ITEM Action', () => {
        beforeEach(() => {
            sinon.spy(ContentModel, 'createContentItem');
        });
 
        afterEach(() => {
            ContentModel.createContentItem.restore();
        });
 
        it('calls createContentItem to create content from contentsItem', () => {
            playlist({contentItems: ['media1'], dragContentsItem: {name: 'media2'}}, {type: DROP_DRAG_CONTENTS_ITEM});
 
            expect(ContentModel.createContentItem.calledWith({name: 'media2'})).to.be.true;
        });
 
        it('append drag contentsItem to contentItems', () => {
            let nextState = playlist({contentItems: ['media1'], dragContentsItem: {name: 'media2'}}, {type: DROP_DRAG_CONTENTS_ITEM});
 
            expect(nextState.contentItems.length).to.equal(2);
        });
 
        it('drop contentsItem has default duration', () => {
            let nextState = playlist({contentItems: [], dragContentsItem: {name: 'media2'}}, {type: DROP_DRAG_CONTENTS_ITEM});
 
            expect(nextState.contentItems[0].durationSeconds).to.equal(30);
        });
 
        it('replaces dragContentsItem with empty object', () => {
            let nextState = playlist({contentItems: [], dragContentsItem: {name: 'media2'}}, {type: DROP_DRAG_CONTENTS_ITEM});
 
            expect(nextState.dragContentsItem).to.deep.equal({});
        });
 
        it('does not change state when dragContentsItem is empty object', () => {
            let nextState = playlist({contentItems: [], dragContentsItem: {}}, {type: DROP_DRAG_CONTENTS_ITEM});
 
            expect(nextState).to.deep.equal({contentItems: [], dragContentsItem: {}});
        });
    });
 
    describe('DELETE_CONTENT_ITEM Action', () => {
        it('delete contentsItem from contentItems', () => {
            let nextState = playlist({contentItems: [{name: 'media1'}, {name: 'media2'}]}, {
                type: DELETE_CONTENT_ITEM,
                index: 0
            });
 
            expect(nextState.contentItems.length).to.equal(1);
            expect(nextState.contentItems[0]).to.deep.equal({name: 'media2'});
        });
    });
 
    describe('CHANGE_CONTENT_ITEMS_ORDER_START Action', () => {
        let state;
        let action;
        let nextState;
 
        beforeEach(() => {
            state = {contentItems: [{name: "media1"}, {name: "media2"}]};
            action = {type: CHANGE_CONTENT_ITEMS_ORDER_START, index: 1};
 
            nextState = playlist(state, action);
        });
 
        it('sets willBeReordered', () => {
            expect(nextState.willBeReordered).to.deep.equal({name: "media2"});
        });
 
        it('sets blankIndex with action index', () => {
            expect(nextState.blankIndex).to.equal(1);
        });
    });
 
    describe('CHANGE_CONTENT_ITEMS_ORDER_PROCESS Action', () => {
        let action;
        let nextState;
 
        it('returns previous State when blankIndex and index is equal', () => {
            action = {type: CHANGE_CONTENT_ITEMS_ORDER_PROCESS, index: 1};
 
            nextState = playlist(SECOND_CONTENT_IS_TO_BE_REORDERED, action);
 
            expect(nextState).to.deep.equal({
                contentItems: [{name: "media1", uuid: "1"}, {}, {name: "media3", uuid: "3"}],
                blankIndex: 1,
                willBeReordered: {name: 'media2', uuid: "2"},
                selectedPreviewPreviousItemIndex: 2,
            });
        });
 
        it('sets blankIndex with action index', () => {
            action = {type: CHANGE_CONTENT_ITEMS_ORDER_PROCESS, index: 2};
 
            nextState = playlist(SECOND_CONTENT_IS_TO_BE_REORDERED, action);
 
            expect(nextState.blankIndex).to.equal(2);
        });
 
        it('changes element at index with willBeReordered item', () => {
            action = {type: CHANGE_CONTENT_ITEMS_ORDER_PROCESS, index: 2};
            nextState = playlist(SECOND_CONTENT_IS_TO_BE_REORDERED, action);
 
            expect(nextState.contentItems[2]).to.deep.equal({name: 'media2', uuid: "2"});
        });
 
        it('replace blank element with next element when index is greater than blankIndex', () => {
            action = {type: CHANGE_CONTENT_ITEMS_ORDER_PROCESS, index: 2};
            nextState = playlist(SECOND_CONTENT_IS_TO_BE_REORDERED, action);
 
            expect(nextState.contentItems[1]).to.deep.equal({name: 'media3', uuid: "3"});
        });
 
        it('replace blank element with previous element when index is less than blankIndex', () => {
            action = {type: CHANGE_CONTENT_ITEMS_ORDER_PROCESS, index: 0};
            nextState = playlist(SECOND_CONTENT_IS_TO_BE_REORDERED, action);
 
            expect(nextState.contentItems[1]).to.deep.equal({name: 'media1', uuid: "1"});
        });
 
        it('does not change state when willBeReordered is empty object', () => {
            const notToBeChanged = Object.assign({}, SECOND_CONTENT_IS_TO_BE_REORDERED, {willBeReordered: {}});
 
            expect(playlist(notToBeChanged, {
                type: CHANGE_CONTENT_ITEMS_ORDER_PROCESS,
                index: 0
            })).to.deep.equal(notToBeChanged);
        });
    });
 
    describe('CHANGE_CONTENT_ITEMS_ORDER_END Action', () => {
        const action = {type: CHANGE_CONTENT_ITEMS_ORDER_END};
        let nextState;
 
        beforeEach(() => {
            nextState = playlist(SECOND_CONTENT_IS_TO_BE_REORDERED, action);
        });
 
        it('initializes blankIndex', () => {
            expect(nextState.blankIndex).to.equal(-1);
        });
 
        it('initializes willBeReordered', () => {
            expect(nextState.willBeReordered).to.deep.equal({});
        });
 
        it('does not change state when willBeReordered is empty object', () => {
            const previousState = Object.assign({}, SECOND_CONTENT_IS_TO_BE_REORDERED, {willBeReordered: {}});
            const nextState = playlist(previousState, action);
 
            expect(nextState).to.deep.equal(previousState);
        });
    });
 
    describe('UPDATE_CONTENT_ITEM_TRANSITION Action', () => {
        const action = {type: UPDATE_CONTENT_ITEM_TRANSITION, uuid: firstItem.uuid, transitionEffect: "SLIDE"};
        let nextState;
 
        beforeEach(() => {
            nextState = playlist(SECOND_CONTENT_IS_TO_BE_REORDERED, action);
        });
 
        it('update item from contentItems', () => {
            expect(nextState.contentItems[0].transitionEffect).to.equal("SLIDE");
        });
    });
 
    describe('UPDATE_CONTENT_ITEM_DURATION Action', () => {
        const action = {type: UPDATE_CONTENT_ITEM_DURATION, uuid: "1", durationSeconds: 30};
        let nextState;
 
        beforeEach(() => {
            nextState = playlist(SECOND_CONTENT_IS_TO_BE_REORDERED, action);
        });
 
        it('update item from contentItems', () => {
            expect(nextState.contentItems[0].durationSeconds).to.equal(30);
        });
    });
 
    describe('DISCARD_PLAYLIST Action', () => {
        const action = {type: DISCARD_PLAYLIST};
        let nextState;
 
        beforeEach(() => {
            nextState = playlist(undefined, action);
        });
 
        it('nextState is equal to INITIAL_STATE', () => {
            expect(nextState).to.deep.equal(INITIAL_STATE);
        });
    });
});