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

100% Statements 211/211
100% Branches 1/1
100% Functions 53/53
100% Lines 209/209
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 4861x 1x 1x 1x 1x 1x 1x 1x   1x                                             1x 1x   1x                                               1x                                             1x         1x   1x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x   38x                                                       1x           1x 17x 17x 17x 17x     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       1x 1x       1x 1x     1x 1x     1x 1x   1x   1x     1x 1x   1x     1x 1x     1x 1x 1x   1x     1x 17x       4x         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       2x 2x 2x     1x 1x   1x 2x 1x   1x 1x   1x     1x 1x   1x 2x 1x   1x 1x   1x      
import React from 'react';
import {expect} from 'chai';
import {shallow} from "enzyme";
import sinon from 'sinon';
import ContentsView from "../../../src/component/contents/contents-view";
import ContentsItemView from "../../../src/component/contents/contents-item-view";
import FolderView from "../../../src/component/contents/folder-view";
import ContextMenu from '../../../src/component/popup/context-menu/context-menu';
 
describe('Contents View Spec', () => {
    let wrapper;
    let spyDeleteContentsItem;
    let spyOpenDialog;
    let spyWillBeDeletedContentsItem;
    let spySelectUploadFiles;
    let spyUploadFilesAsync;
    let spyOpenFloatingView;
    let spyOnContentsClick;
    let spyUpdateFolderAsync;
    let spyOnContentItemDoubleClick;
    let spyOnContextMenuClicked;
    let spyCloseContextMenuPopup;
    let spyUpdateFolderData;
    let spyOnFolderItemDoubleClick;
    let spyOnBreadcrumbItemClick;
    let spyOnPreview;
    let spyRename;
    let spyUpdateContentsItemAsync;
    let spyUpdateContentsItemData;
    let spyMoveToClick;
    let spyCopyToClick;
 
    const SELECTED_FOLDER_ID = '3423424';
    const SELECTED_CONTENTS_ITEM_ID = 1;
 
    const CONTENTS = [
        {
            id: 1,
            name: 'first.file',
            lastModifiedTimestamp: '2017-09-07T09:15:51.283Z',
            type: 'image',
            fileSizeBytes: 213674,
            thumbnailUrls: {
                thumbnail: 'first.thumbnail.file',
            },
            status: 'new'
        },
        {
            id: 2,
            name: 'second.file',
            lastModifiedTimestamp: '2017-09-08T16:15:51.283Z',
            type: 'video',
            fileSizeBytes: 12345670,
            thumbnailUrls: {
                thumbnail: 'second.thumbnail.file'
            }
        },
    ];
 
    const FOLDERS = Object.freeze([
        {
            createdTimestamp: "2017-10-24T07:01:36.026Z",
            lastModifiedTimestamp: "2017-10-24T07:01:36.027Z",
            id: "3423424",
            parentId: "334535",
            name: "Summer Festival",
            ancestors: [
                "334535"
            ]
        },
        {
            createdTimestamp: "2017-09-28T02:02:39.959Z",
            lastModifiedTimestamp: "2017-09-28T02:02:39.959Z",
            id: "59cc583fa153d66606dd6e5c",
            parentId: "334535",
            name: "Folder_170928_1",
            ancestors: [
                "334535"
            ]
        }
    ]);
 
    const dummyBreadcrumb = [{name: 'folder1', id: 'folder1'}, {name: 'folder2', id: 'folder2'}, {
        name: 'folder3',
        id: 'folder3'
    }];
 
    const searchOption = {name: '', type: 'all'};
 
    beforeEach(() => {
        spyDeleteContentsItem = sinon.spy();
        spyOpenDialog = sinon.spy();
        spyWillBeDeletedContentsItem = sinon.spy();
        spySelectUploadFiles = sinon.spy();
        spyUploadFilesAsync = sinon.spy();
        spyOpenFloatingView = sinon.spy();
        spyOnContentsClick = sinon.spy();
        spyUpdateFolderAsync = sinon.spy();
        spyOnContentItemDoubleClick = sinon.spy();
        spyOnContextMenuClicked = sinon.spy();
        spyCloseContextMenuPopup = sinon.spy();
        spyUpdateFolderData = sinon.spy();
        spyOnFolderItemDoubleClick = sinon.spy();
        spyOnBreadcrumbItemClick = sinon.spy();
        spyOnPreview = sinon.spy();
        spyRename = sinon.spy();
        spyUpdateContentsItemAsync = sinon.spy();
        spyUpdateContentsItemData = sinon.spy();
        spyMoveToClick = sinon.spy();
        spyCopyToClick = sinon.spy();
 
        wrapper = shallow(<ContentsView contents={CONTENTS}
                                        folders={FOLDERS}
                                        onDeleteContentsItem={spyDeleteContentsItem}
                                        willBeDeletedContentsItem={spyWillBeDeletedContentsItem}
                                        openDialog={spyOpenDialog}
                                        selectUploadFiles={spySelectUploadFiles}
                                        uploadFilesAsync={spyUploadFilesAsync}
                                        openFloatingView={spyOpenFloatingView}
                                        onContentsClick={spyOnContentsClick}
                                        updateFolderAsync={spyUpdateFolderAsync}
                                        onContentItemDoubleClick={spyOnContentItemDoubleClick}
                                        onContextMenuClicked={spyOnContextMenuClicked}
                                        contextMenuVisibility={true}
                                        closePopup={spyCloseContextMenuPopup}
                                        updateFolderData={spyUpdateFolderData}
                                        onFolderItemDoubleClick={spyOnFolderItemDoubleClick}
                                        breadcrumb={dummyBreadcrumb}
                                        onBreadcrumbItemClick={spyOnBreadcrumbItemClick}
                                        onPreview={spyOnPreview}
                                        rename={spyRename}
                                        updateContentsItemAsync={spyUpdateContentsItemAsync}
                                        updateContentsItemData={spyUpdateContentsItemData}
                                        moveToClick={spyMoveToClick}
                                        copyToClick={spyCopyToClick}
                                        searchOption={searchOption}
        />);
    });
 
    describe('Passing Props to ContentsItemView and FolderView', () => {
        let firstContentsItemProps;
        let secondContentsItemProps;
        let firstFolderProps;
        let secondFolderProps;
 
        beforeEach(() => {
            firstContentsItemProps = wrapper.find(ContentsItemView).at(0).props();
            secondContentsItemProps = wrapper.find(ContentsItemView).at(1).props();
            firstFolderProps = wrapper.find(FolderView).at(0).props();
            secondFolderProps = wrapper.find(FolderView).at(1).props();
        });
 
        it('passes contentsItem', () => {
            let {contentsItem} = firstContentsItemProps;
 
            expect(contentsItem).to.deep.equal(CONTENTS[0]);
        });
 
        it('passes folderItem', () => {
            let {folder} = firstFolderProps;
 
            expect(folder).to.deep.equal(FOLDERS[0]);
        });
 
        it('passes onDelete with closure props.openDialog and willBeDeletedContentsItem with index', () => {
            firstContentsItemProps.onDelete();
 
            expect(spyOpenDialog.calledWith('delete-contents-item')).to.be.true;
            expect(spyWillBeDeletedContentsItem.calledWith(0)).to.be.true;
 
            secondContentsItemProps.onDelete();
 
            expect(spyOpenDialog.calledWith('delete-contents-item')).to.be.true;
            expect(spyWillBeDeletedContentsItem.calledWith(1)).to.be.true;
        });
 
        it('passes onPreview with closure props.onSelectedPreviewItemIndex with id', () => {
            firstContentsItemProps.onPreview();
 
            expect(spyOnPreview.calledWith(1)).to.be.true;
 
            secondContentsItemProps.onPreview();
 
            expect(spyOnPreview.calledWith(2)).to.be.true;
        });
 
        it('passes onClick with onContentsClick with type and index', () => {
            firstContentsItemProps.onClick();
 
            expect(spyOnContentsClick.calledWith('image', 0)).to.true;
 
            secondContentsItemProps.onClick();
 
            expect(spyOnContentsClick.calledWith('video', 1)).to.true;
        });
 
        it('passes onDoubleClick with onContentItemDoubleClick with index', () => {
            firstContentsItemProps.onDoubleClick();
 
            expect(spyOnContentItemDoubleClick.calledWith(0)).to.true;
        });
 
        it('passes onClick with onContentsClick with undefined type and index', () => {
            firstFolderProps.onClick();
 
            expect(spyOnContentsClick.calledWith(undefined, 0)).to.true;
        });
 
        it('passes onDoubleClick with onFolderItemDoubleClick with folder item', () => {
            firstFolderProps.onDoubleClick();
 
            expect(spyOnFolderItemDoubleClick.calledWith(FOLDERS[0])).to.true;
        });
 
        describe('passes onContextMenu prop', () => {
            it('with onContextMenu on FolderView', () => {
                const e = {preventDefault: sinon.spy()};
 
                sinon.stub(wrapper.instance(), 'getContentsScrollTop').returns(0);
 
                firstFolderProps.onContextMenu(e);
 
                expect(spyOnContextMenuClicked.calledWith(undefined, 0)).to.true;
            });
 
            it('with onContextMenu on ContentsItemView', () => {
                const e = {preventDefault: sinon.spy()};
 
                sinon.stub(wrapper.instance(), 'getContentsScrollTop').returns(0);
 
                firstContentsItemProps.onContextMenu(e);
 
                expect(spyOnContextMenuClicked.calledWith('image', 0)).to.true;
            });
        });
 
        it('passes onBreadcrumbItemClick with id and index', () => {
            wrapper.find('.contents-breadcrumb-item > span').at(1).simulate('click');
 
            expect(spyOnBreadcrumbItemClick.calledWith('folder2', 1)).to.true;
        });
 
        it('passes selectedContentsItemIds when folder is selected', () => {
            wrapper.setProps({
                selectedContentsItemIds: [SELECTED_FOLDER_ID]
            });
 
            firstFolderProps = wrapper.find(FolderView).at(0).props();
 
            expect(firstFolderProps.selectedContentsItemIds[0]).to.deep.equal(SELECTED_FOLDER_ID);
        });
 
        it('passes selectedContentsItemIds when contentsItem is selected', () => {
            wrapper.setProps({
                selectedContentsItemIds: [SELECTED_CONTENTS_ITEM_ID]
            });
 
            firstContentsItemProps = wrapper.find(ContentsItemView).at(0).props();
 
            expect(firstContentsItemProps.selectedContentsItemIds[0]).to.deep.equal(SELECTED_CONTENTS_ITEM_ID);
        });
 
        it('passes rename on folder\'s Context menu with onRenameClick', () => {
            wrapper.find(ContextMenu).at(0).props().onRenameClick();
 
            expect(spyRename.called).to.true;
        });
 
        it('passes onMoveToClick on contents item\'s Context menu and open move to Dialog with onMoveToClick', () => {
            wrapper.find(ContextMenu).at(0).props().onMoveToClick();
 
            expect(spyMoveToClick.called).to.true;
            expect(spyOpenDialog.calledWith('move-to'));
        });
 
        it('passes onCopyToClick on contents item\'s Context menu and copied contents', () => {
            wrapper.find(ContextMenu).at(0).props().onCopyToClick();
 
            expect(spyCopyToClick.called).to.true;
        });
 
        it('show search bar and contents size when searchKeyword is exist', () => {
            wrapper.setProps({
                searchOption: {name: 'search name', type: 'image'}
            });
 
            expect(wrapper.find('.contents-search-result').exists()).to.true;
            expect(wrapper.find('.contents-search-result').text()).to.equal('Search result 2');
        });
    });
 
    it('has rows as many as contentsItem', () => {
        expect(wrapper.find(ContentsItemView).length).to.equal(2);
    });
 
    it('has rows as many as folders', () => {
        expect(wrapper.find(FolderView).length).to.equal(2);
    });
 
    it('shows drop zone when dropping state is true', () => {
        expect(wrapper.find('.contents-drop-area').length).to.equal(0);
 
        wrapper.setState({dropping: true});
 
        expect(wrapper.find('.contents-drop-area').length).to.equal(1);
    });
 
    it('set drop zone top and height by scroll top', () => {
        wrapper.setState({dropping: true});
 
        expect(wrapper.find('.contents-drop-area').html()).to.contain('style="top:0px;height:calc(100vh - 110px - 50px);"')
    });
 
    it('getContentsScrollTop function return 0 when this.contentWrapper is undefined', () => {
            expect(wrapper.instance().getContentsScrollTop()).to.equal(0);
    });
 
    it('set drop zone top and height by scroll top when contentWrapper is scrolled', () => {
        sinon.stub(wrapper.instance(), 'getContentsScrollTop').returns(70);
        wrapper.setState({dropping: true});
 
        expect(wrapper.find('.contents-drop-area').html()).to.contain('style="top:20px;height:calc(100vh - 110px - 0px);"')
    });
 
    const createDragOverEvent = (kind, names = []) => {
        return {
            preventDefault: sinon.spy(),
            dataTransfer: {
                types: [kind],
                files: names.map((name) => ({name}))
            }
        };
    };
 
    describe('render', () => {
        it('ContextMenu is exist when contextMenuVisibility is true', () => {
            wrapper.setProps({
                contextMenuVisibility: true,
            });
            expect(wrapper.find(ContextMenu).exists()).to.true;
        });
    });
 
    describe('drag enter', () => {
        it('prevents default', () => {
            let event = createDragOverEvent('Files');
 
            wrapper.find('.contents-wrapper').simulate('dragenter', event);
 
            expect(event.preventDefault.called).to.be.true;
        });
 
        it('increments drag counter', () => {
            wrapper.find('.contents-wrapper').simulate('dragenter', createDragOverEvent('Files'));
 
            expect(wrapper.instance().dragCounter).to.equal(1);
 
            wrapper.find('.contents-wrapper').simulate('dragenter', createDragOverEvent('Files'));
 
            expect(wrapper.instance().dragCounter).to.equal(2);
        });
 
        it('does not change state when dropping state is already true', () => {
            wrapper.find('.contents-wrapper').simulate('dragenter', createDragOverEvent('Files'));
 
            sinon.spy(wrapper.instance(), 'setState');
 
            wrapper.find('.contents-wrapper').simulate('dragenter', createDragOverEvent('Files'));
 
            expect(wrapper.instance().setState.notCalled).to.be.true;
        });
 
        it('changes dropping as true when dragover event has items which is file', () => {
            wrapper.find('.contents-wrapper').simulate('dragenter', createDragOverEvent('Files'));
 
            expect(wrapper.state('dropping')).to.be.true;
        });
 
        it('does not change if any item is not file', () => {
            wrapper.find('.contents-wrapper').simulate('dragenter', createDragOverEvent('Not_Files'));
 
            expect(wrapper.state('dropping')).to.be.false;
        });
    });
 
    describe('drag leave', () => {
        it('prevents default', () => {
            let event = createDragOverEvent();
 
            wrapper.find('.contents-wrapper').simulate('dragleave', event);
 
            expect(event.preventDefault.called).to.be.true;
        });
 
        it('decrements drag counter', () => {
            wrapper.instance().dragCounter = 2;
 
            wrapper.find('.contents-wrapper').simulate('dragleave', createDragOverEvent());
 
            expect(wrapper.instance().dragCounter).to.equal(1);
        });
 
        it('changes dropping state as false only when drag counter is 0', () => {
            wrapper.instance().dragCounter = 2;
            wrapper.setState({dropping: true});
 
            wrapper.find('.contents-wrapper').simulate('dragleave', createDragOverEvent());
 
            expect(wrapper.state('dropping')).to.be.true;
 
            wrapper.find('.contents-wrapper').simulate('dragleave', createDragOverEvent());
 
            expect(wrapper.state('dropping')).to.be.false;
        });
    });
 
    describe('drag over', () => {
        it('prevents default', () => {
            let event = createDragOverEvent();
 
            wrapper.find('.contents-wrapper').simulate('dragover', event);
 
            expect(event.preventDefault.called).to.be.true;
        });
    });
 
    describe('drop', () => {
        it('prevents default', () => {
            let event = createDragOverEvent('Files', []);
 
            wrapper.find('.contents-wrapper').simulate('drop', event);
 
            expect(event.preventDefault.called).to.be.true;
        });
 
        it('set drag counter to 0', () => {
            wrapper.instance().dragCounter = 10;
 
            wrapper.find('.contents-wrapper').simulate('drop', createDragOverEvent('Files', []));
 
            expect(wrapper.instance().dragCounter).to.equal(0);
        });
 
        it('change dropping state as false', () => {
            wrapper.setState({
                dropping: true
            });
 
            wrapper.find('.contents-wrapper').simulate('drop', createDragOverEvent('Files'));
 
            expect(wrapper.state('dropping')).to.be.false;
        });
 
        function assertFilesAreUploaded(files, expected) {
            expect(spySelectUploadFiles.calledWith(files)).to.equal(expected);
            expect(spyUploadFilesAsync.calledWith(files)).to.equal(expected);
            expect(spyOpenFloatingView.called).to.equal(expected);
        }
 
        it('call 3 actions after setState is called with valid file extension', () => {
            sinon.stub(wrapper.instance(), 'setState');
 
            let fileNames = ['a.jpg', 'b.mp4'];
            let files = fileNames.map((name) => ({name}));
            wrapper.find('.contents-wrapper').simulate('drop', createDragOverEvent('Files', fileNames));
 
            const stateCallback = wrapper.instance().setState.lastCall.args[1];
            stateCallback();
 
            assertFilesAreUploaded(files, true);
        });
 
        it('do not call 3 actions after setState is called with invalid file extension', () => {
            sinon.stub(wrapper.instance(), 'setState');
 
            let fileNames = ['a.bad', 'b.mp4'];
            let files = fileNames.map((name) => ({name}));
            wrapper.find('.contents-wrapper').simulate('drop', createDragOverEvent('Files', fileNames));
 
            const stateCallback = wrapper.instance().setState.lastCall.args[1];
            stateCallback();
 
            assertFilesAreUploaded(files, false);
        });
    });
});