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

100% Statements 156/156
100% Branches 0/0
100% Functions 44/44
100% Lines 156/156
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 3491x 1x 1x 1x 1x 1x 1x 1x   1x                           1x 31x                     31x                     31x                     31x   31x 31x 31x 31x 31x 31x 31x 31x   31x 31x 31x               1x 31x 31x 31x 31x     1x 1x 1x 1x 1x 1x       1x         1x 5x 5x       5x 5x   5x   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 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   9x               9x 9x     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      
import React from 'react';
import {expect} from 'chai';
import {mount, shallow} from "enzyme";
import sinon from 'sinon';
import momentTimezone from 'moment-timezone';
import * as FileUtil from '../../../src/helpers/utility';
import * as ReactWrapper from '../../../src/wrapper/findDOMNodeWrapper';
import ContentsItemView from "../../../src/component/contents/contents-item-view";
 
describe('Contents Item View Spec', () => {
    let contentsItem;
    let playlistItem;
    let thumbnailIsUndefind;
    let wrapper;
    let spyDelete;
    let spyPreview;
    let spyOnClick;
    let spyOnDoubleClick;
    let spyUpdateContentsItemAsync;
    let spyOnContextMenu;
    let spyUpdateContentsItemData;
    let spySetSelectionRange;
 
    beforeEach(() => {
        contentsItem = {
            id: 1,
            name: 'first.file',
            lastModifiedTimestamp: '2017-09-07T17:15:51.283Z',
            type: 'image',
            fileSizeBytes: 213674,
            thumbnailUrls: {
                thumbnail: 'first.thumbnail.file',
            }
        };
 
        playlistItem = {
            id: 2,
            name: 'first.file',
            lastModifiedTimestamp: '2017-09-07T17:15:51.283Z',
            type: 'playlist',
            fileSizeBytes: 213674,
            thumbnailUrls: {
                thumbnail: 'first.thumbnail.file',
            }
        };
 
        thumbnailIsUndefind = {
            id: 3,
            name: 'first.file',
            lastModifiedTimestamp: '2017-09-07T17:15:51.283Z',
            type: 'playlist',
            fileSizeBytes: 213674,
            thumbnailUrls: {
                thumbnail: undefined,
            }
        };
 
        momentTimezone.tz.setDefault('Asia/Seoul');
 
        spyDelete = sinon.spy();
        spyPreview = sinon.spy();
        spyOnClick = sinon.spy();
        spyOnDoubleClick = sinon.spy();
        spyUpdateContentsItemAsync = sinon.spy();
        spyOnContextMenu = sinon.spy();
        spyUpdateContentsItemData = sinon.spy();
        spySetSelectionRange = sinon.spy();
 
        sinon.spy(FileUtil, "convertBytesToKilobytes");
        sinon.stub(global, 'setTimeout');
        wrapper = shallow(<ContentsItemView contentsItem={contentsItem} onDelete={spyDelete} onPreview={spyPreview}
                                            onClick={spyOnClick} onDoubleClick={spyOnDoubleClick}
                                            updateContentsItemAsync={spyUpdateContentsItemAsync}
                                            onContextMenu={spyOnContextMenu}
                                            updateContentsItemData={spyUpdateContentsItemData}
        />);
    });
 
    afterEach(() => {
        FileUtil.convertBytesToKilobytes.restore();
        global.setTimeout.restore();
        momentTimezone.tz.setDefault();
        spySetSelectionRange.reset();
    });
 
    describe('Component Will Unmount', () => {
        it('does not called clearTimeout when timeOutID is undefined', () => {
            const spyClearTimeout = sinon.spy(global, 'clearTimeout');
            wrapper.instance().componentWillUnmount();
            expect(spyClearTimeout.called).to.false;
            global.clearTimeout.restore();
        });
    });
 
    describe('Component Updated', () => {
        let timeout;
        let instance;
        let stubQuerySelector;
 
        beforeEach(() => {
            stubQuerySelector = sinon.stub().returns('something hover');
            sinon.stub(ReactWrapper, 'findDOMNode').returns({
                querySelector: stubQuerySelector
            });
 
            instance = wrapper.instance();
            instance.componentDidUpdate();
 
            timeout = global.setTimeout.lastCall.args[0];
 
            timeout();
        });
 
        afterEach(() => {
            ReactWrapper.findDOMNode.restore();
        });
 
        it('sets timeout for 50 milliseconds', () => {
            expect(global.setTimeout.calledWith(sinon.match.func, 50)).to.be.true;
        });
 
        it("finds it's dom node", () => {
            expect(ReactWrapper.findDOMNode.calledWith(wrapper.instance())).to.be.true;
        });
 
        it('queries :hover node', () => {
            expect(stubQuerySelector.calledWith(":hover")).to.be.true;
        });
 
        it('does not set isHover when it is not :hover', () => {
            stubQuerySelector.returns(undefined);
 
            wrapper.setState({isHover: false});
            timeout();
 
            expect(instance.state.isHover).to.be.false;
        });
 
        it('does not set isHover when it is not :hover', () => {
            stubQuerySelector.returns(undefined);
 
            wrapper.setState({isHover: false});
            timeout();
 
            expect(instance.state.isHover).to.be.false;
        });
    });
 
    it('has contents-item class', () => {
        expect(wrapper.find('.contents-item').length).to.equal(1);
    });
 
    it('has --selected modifier if status field is "new"', () => {
        wrapper.setProps({
            selectedContentsItemIds: [1]
        });
 
        expect(wrapper.find('.contents-item--selected').length).to.equal(1);
    });
 
    it('first column has thumbnail url', () => {
        expect(wrapper.find('.contents-item__thumbnail').getNode().props.style.backgroundImage).to.equal('url(first.thumbnail.file)');
    });
 
    it('first column has black image when thumbnail is not extist', () => {
        wrapper.setProps({
            contentsItem: Object.assign({}, thumbnailIsUndefind, {status: 'selected'})
        });
 
        expect(wrapper.find('.contents-item__thumbnail').getNode().props.style.backgroundColor).to.equal('black');
    });
 
    it('second column has file name', () => {
        expect(wrapper.find('.contents-item__name').text()).to.equal('first.file');
    });
 
    it('second column has file type icon', () => {
        expect(wrapper.find('.contents-item__type--image').length).to.equal(1);
 
        wrapper.setProps({contentsItem: Object.assign({}, contentsItem, {type: 'video'})});
 
        expect(wrapper.find('.contents-item__type--video').length).to.equal(1);
    });
 
    it('6th column has shows "YYYY.MM.DD" formatted time according to timezone"', () => {
        expect(wrapper.find('td').at(5).text()).to.equal('09-08-2017');
 
        momentTimezone.tz.setDefault('Europe/London');
 
        wrapper = shallow(<ContentsItemView contentsItem={contentsItem}/>);
 
        expect(wrapper.find('td').at(5).text()).to.equal('09-07-2017');
    });
 
    it('7th column shows file size throught fileSizeHelper', () => {
        expect(FileUtil.convertBytesToKilobytes.calledWith(213674)).to.be.true;
    });
 
    it('7th column has file size', () => {
        expect(wrapper.find('td').at(6).text()).to.equal('208.7KB');
    });
 
    it('calls onPreview when preview is clicked', () => {
        wrapper.setState({isHover: true});
        wrapper.find('.contents-item-preview').simulate('click');
 
        expect(spyPreview.called).to.be.true;
    });
 
    it('calls onClick when component is clicked', () => {
        wrapper.find('.contents-item').simulate('click');
 
        expect(spyOnClick.called).to.true;
    });
 
    it('calls onDoubleClick when component is double clicked', () => {
        wrapper.find('.contents-item').simulate('doubleClick');
 
        expect(spyOnDoubleClick.called).to.true;
    });
 
    it('highlight search keyword on name', () => {
        expect(wrapper.find('.contents-item__name-highlight')).to.be.lengthOf(0);
 
        wrapper.setProps({searchKeyword: 'first'});
 
        expect(wrapper.find('.contents-item__name-highlight')).to.be.lengthOf(1);
    });
 
    describe('click trash can', () => {
        beforeEach(() => {
            wrapper.find(".contents-item").at(0).simulate('click');
 
            wrapper.find('.contents-item__trash-can').simulate('click');
        });
 
        it('calls onDeleteItem with index', () => {
            expect(spyDelete.called).to.be.true;
        });
    });
 
    describe('input key & blur event', () => {
        const newContentsItem = Object.freeze({
            id: "someContentsItemId",
            name: "someContentsItemName.jpg",
            status: "new",
            lastModifiedTimestamp: '2017-09-07T17:15:51.283Z',
            type: 'image',
            fileSizeBytes: 213674,
            thumbnailUrls: {
                thumbnail: 'first.thumbnail.file',
            }
        });
 
        beforeEach(() => {
            //TODO: Please remove warning.
            wrapper = mount(
                <ContentsItemView contentsItem={newContentsItem} onDelete={spyDelete} onPreview={spyPreview}
                                  onClick={spyOnClick} onDoubleClick={spyOnDoubleClick}
                                  updateContentsItemAsync={spyUpdateContentsItemAsync}
                                  onContextMenu={spyOnContextMenu} updateContentsItemData={spyUpdateContentsItemData}
                />
            );
 
            wrapper.instance().titleInput.setSelectionRange = spySetSelectionRange;
            wrapper.instance().titleInput.value = 'new contents item name';
        });
 
        it('focus out when esc key down', () => {
            expect(document.activeElement.classList.contains('contents-item__name--new')).to.be.true;
 
            wrapper.find('.contents-item__name--new').simulate('keyup', {key: 'Escape'});
 
            expect(document.activeElement.classList.contains('contents-item__name--new')).to.be.false;
        });
 
        it('focus out when esc key down', () => {
            expect(document.activeElement.classList.contains('contents-item__name--new')).to.be.true;
 
            wrapper.find('.contents-item__name--new').simulate('keyup', {key: 'Enter'});
 
            expect(document.activeElement.classList.contains('contents-item__name--new')).to.be.false;
        });
 
        it('titleInput is focused and setSelectionRange is called when componentDidMount call', () => {
            const focusTitleInputAndSelect = sinon.spy(wrapper.instance(), 'focusTitleInputAndSelect');
            wrapper.instance().componentDidMount();
 
            expect(document.activeElement.classList.contains('contents-item__name--new')).to.be.true;
            expect(focusTitleInputAndSelect.called).to.true;
        });
 
        it('titleInput is focused and setSelectionRange is called when componentDidUpdate call', () => {
            const focusTitleInputAndSelect = sinon.spy(wrapper.instance(), 'focusTitleInputAndSelect');
            wrapper.instance().componentDidUpdate();
 
            expect(document.activeElement.classList.contains('contents-item__name--new')).to.be.true;
            expect(focusTitleInputAndSelect.called).to.true;
        });
 
        it('when focusTitleInputAndSelect then call setSelectionRange and set input value is "new contents item name"', () => {
            wrapper.instance().focusTitleInputAndSelect();
 
            expect(wrapper.instance().titleInput.value).to.equal('someContentsItemName.jpg');
 
            expect(spySetSelectionRange.calledWith(0, 'someContentsItemName'.length)).to.true;
        });
 
        it('titleInput is not blured when keyup except in Escape or Enter key', () => {
            expect(document.activeElement.classList.contains('contents-item__name--new')).to.be.true;
            wrapper.find('.contents-item__name--new').simulate('keyup', {key: 'Space'});
 
            expect(document.activeElement.classList.contains('contents-item__name--new')).to.be.true;
        });
 
        describe('contentsItemNameInputBlurred method test', () => {
            it('call updateContentsItemAsync with contents item name when focus is out', () => {
                wrapper.find('.contents-item__name--new').simulate('blur');
 
                expect(spyUpdateContentsItemAsync.calledWith(newContentsItem, 'new contents item name')).to.be.true;
            });
 
            it('when input value and contents name is same', () => {
                wrapper.instance().titleInput.value = 'someContentsItemName.jpg';
                wrapper.find('.contents-item__name--new').simulate('blur');
 
                expect(spyUpdateContentsItemData.called).to.true;
            });
 
            it('call getContentsItem with error state', () => {
                expect(wrapper.instance().getContentsItem({status: 'error'}).status).to.equal('new');
                expect(wrapper.instance().getContentsItem({status: 'new'})).to.not.have.property('status');
            });
        });
    });
 
    describe('render', () => {
        it('selected background not exists when contentsItem is not selected', () => {
            expect(wrapper.find('.contents-item--selected')).to.be.lengthOf(0);
        });
 
        it('show selected background when folder is selected', () => {
            wrapper.setProps({
                selectedContentsItemIds: [1]
            });
            expect(wrapper.find('.contents-item--selected')).to.be.lengthOf(1);
        });
    });
});