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

100% Statements 104/104
100% Branches 0/0
100% Functions 28/28
100% Lines 104/104
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 2951x 1x 1x 1x 1x 1x 1x   1x                         1x 1x 1x                                           1x                                         1x 18x 18x 18x 18x 18x 18x 18x 18x 18x   18x                           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 8x 8x 8x   8x 8x                       8x               8x               8x         8x       8x               1x 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   1x      
import React from 'react';
import {expect} from 'chai';
import {shallow} from "enzyme";
import PanelGroup from "react-panelgroup";
import * as sinon from "sinon";
import {HORIZONTAL_LAYOUTS as LAYOUTS} from "../../../../src/constants/layouts";
import SceneEditorView from "../../../../src/component/scene/scene-editor/scene-editor-view";
 
describe('Scene Editor View Spec', () => {
    let wrapper;
 
    let spyUpdateLayout;
    let spyIsDraggedOverPanel;
    let spySetDraggedOverPanelId;
    let spyIsSelectedPanel;
    let spySetSelectedPanelId;
    let spyGetCSSWrapperName;
    let spyUpdateFitMode;
    let spyAddContentItem;
    let spyRemoveContentItem;
 
    const ORIENTATION = 'horizontal';
    const SELECTED_TAB_ID = 'layout';
    const LAYOUT_WITH_CONTENTITEM = {
        id: '1',
        index: 0,
        panels: [
            {
                id: 'g1',
                domAttribute: {size: 960, minSize: 5, resize: 'dynamic'},
                type: 'group',
                direction: 'row',
                panels: [
                    {
                        id: 'p1',
                        domAttribute: {size: 960, minSize: 5, resize: 'dynamic'},
                        type: 'panel',
                        contentItems: [{id: 'c1', fitMode: 'cover'}]
                    }
                ]
            },
        ],
        type: '1-1',
    };
 
    const LAYOUT_WITHOUT_CONTENTITEM = {
        id: '1',
        index: 0,
        panels: [
            {
                id: 'g1',
                domAttribute: {size: 960, minSize: 5, resize: 'dynamic'},
                type: 'group',
                direction: 'row',
                panels: [
                    {
                        id: 'p1',
                        domAttribute: {size: 960, minSize: 5, resize: 'dynamic'},
                        type: 'panel',
                    }
                ]
            },
        ],
        type: '1-1',
    };
 
    beforeEach(() => {
        spyUpdateLayout = sinon.spy();
        spyIsDraggedOverPanel = sinon.spy();
        spySetDraggedOverPanelId = sinon.spy();
        spyIsSelectedPanel = sinon.spy();
        spySetSelectedPanelId = sinon.spy();
        spyGetCSSWrapperName = sinon.stub().returns('--horizontal');
        spyAddContentItem = sinon.spy();
        spyRemoveContentItem = sinon.spy();
        spyUpdateFitMode = sinon.spy();
 
        wrapper = shallow(<SceneEditorView layout={LAYOUTS[0]}
                                           orientation={ORIENTATION}
                                           getCSSWrapperName={spyGetCSSWrapperName}
                                           addContentItem={spyAddContentItem}
                                           removeContentItem={spyRemoveContentItem}
                                           selectedTabId={SELECTED_TAB_ID}
                                           isDraggedOverPanel={spyIsDraggedOverPanel}
                                           isSelectedPanel={spyIsSelectedPanel}
                                           setDraggedOverPanelId={spySetDraggedOverPanelId}
                                           setSelectedPanelId={spySetSelectedPanelId}
                                           updateFitMode={spyUpdateFitMode}
        />);
    });
 
    describe('render', () => {
        it('has layout wrapper', () => {
            expect(wrapper.find('.layout-wrapper--horizontal').length).to.be.equal(1);
        });
 
        it('has two panelGroup when layout id is "1"', () => {
            expect(wrapper.find(PanelGroup).length).to.be.equal(2);
        });
 
        it('has three panelGroups when layout id is "1hf1v1b3"', () => {
            wrapper.setProps({layout: LAYOUTS[5]});
 
            expect(wrapper.find(PanelGroup).length).to.be.equal(3);
        });
 
        it('display panel guide message when tab id is not "layout"', () => {
            wrapper.setProps({selectedTabId: 'contents'});
 
            expect(wrapper.find('.layout-panel__guide-message')).to.have.lengthOf(1);
        });
 
        it('not display panel guide message when tab id is "layout"', () => {
            expect(wrapper.find('.layout-panel__guide-message')).to.have.lengthOf(0);
        });
 
        it('show layout-panel__body__status--dragged-over at first panel when first panel is draggedOverPanel', () => {
            wrapper.setProps({selectedTabId: 'contents', isDraggedOverPanel: sinon.stub().returns(true)});
 
            expect(wrapper.find('.layout-panel__body__status--dragged-over')).to.have.lengthOf(1);
        });
 
        it('show layout-panel__body__status--selected at first panel when first panel is selectedPanel', () => {
            wrapper.setProps({selectedTabId: 'contents', isSelectedPanel: sinon.stub().returns(true)});
 
            expect(wrapper.find('.layout-panel__body__status--selected')).to.have.lengthOf(1);
        });
 
        it('show layout-panel__controller--active at first panel when first panel is selectedPanel and has contentItem', () => {
            wrapper.setProps({layout: LAYOUT_WITH_CONTENTITEM, isSelectedPanel: sinon.stub().returns(true)});
 
            expect(wrapper.find('.layout-panel__controller--active')).to.have.lengthOf(1);
        });
 
        it('panel has black background color when contentItem exist', () => {
            wrapper.setProps({layout: LAYOUT_WITH_CONTENTITEM, isSelectedPanel: sinon.stub().returns(true)});
 
            expect(wrapper.find('#p1').getNode().props.style.backgroundColor).to.equal('black');
        });
 
        it('panel has white background color when contentItem not exist', () => {
            wrapper.setProps({layout: LAYOUT_WITHOUT_CONTENTITEM, isSelectedPanel: sinon.stub().returns(true)});
 
            expect(wrapper.find('#p1').getNode().props.style.backgroundColor).to.equal('white');
        });
    });
 
    describe('events', () => {
        let spyClearData;
        let spyPreventDefault;
        let stubGetData;
 
        let mockDragDrop;
        let mockDragOver;
        let mockClick;
        let mockClearClick;
 
        beforeEach(() => {
            spyClearData = sinon.spy();
            spyPreventDefault = sinon.spy();
            spyUpdateLayout = sinon.spy();
 
            let layout = JSON.parse(JSON.stringify(LAYOUTS[5]));
            wrapper = shallow(<SceneEditorView layout={layout}
                                               orientation={ORIENTATION}
                                               getCSSWrapperName={spyGetCSSWrapperName}
                                               addContentItem={spyAddContentItem}
                                               removeContentItem={spyRemoveContentItem}
                                               selectedTabId={SELECTED_TAB_ID}
                                               isDraggedOverPanel={spyIsDraggedOverPanel}
                                               isSelectedPanel={spyIsSelectedPanel}
                                               setDraggedOverPanelId={spySetDraggedOverPanelId}
                                               updateFitMode={spyUpdateFitMode}
                                               setSelectedPanelId={spySetSelectedPanelId}/>);
 
            stubGetData = sinon.stub().returns(JSON.stringify(
                {
                    id: 'some id',
                    type: 'image',
                    metaData: {contentUrl: 'some url'}
                }
            ));
 
            mockDragDrop = {
                dataTransfer: {
                    getData: stubGetData,
                    clearData: spyClearData
                },
                currentTarget: {id: 'some id'}
            };
 
            mockDragOver = {
                preventDefault: spyPreventDefault,
                currentTarget: {id: 'some id'}
            };
 
            mockClick = {
                currentTarget: {id: 'some id'}
            };
 
            mockClearClick = {
                target: {
                    className: 'scene-editor-wrapper--horizontal'
                },
                stopPropagation: sinon.spy()
            }
        });
 
        describe('contentsItem drag & drop on panel', () => {
            it('set draggedOverPanelId when drag over at layout-panel__body__status', () => {
                wrapper.find('.layout-panel__body__status').at(0).simulate('dragOver', mockDragOver);
                expect(spySetDraggedOverPanelId.calledWith('some id')).to.true;
            });
 
            it('call updateLayout, clear draggedOverPanelId and set selectedPanelId when drag drop at layout-panel__body__status', () => {
                wrapper.find('.layout-panel__body__status').at(0).simulate('drop', mockDragDrop);
 
                expect(spyAddContentItem.called).to.true;
                expect(spySetDraggedOverPanelId.calledWith('')).to.true;
                expect(spySetSelectedPanelId.calledWith('some id')).to.true;
            });
        });
 
        describe('click panel', () => {
            it('click panel', () => {
                wrapper.instance().onPanelClicked(mockClick);
 
                expect(spySetSelectedPanelId.calledWith('some id')).to.true;
            });
 
            it('click panel', () => {
                wrapper.find('.layout-panel__body__status').at(0).simulate('click', mockClick);
 
                expect(spySetSelectedPanelId.calledWith('some id')).to.true;
            });
 
            describe('click wrapper', () => {
                it('horizontal', () => {
                    wrapper.find('.scene-editor-wrapper--horizontal').at(0).simulate('click', mockClearClick);
 
                    expect(spySetDraggedOverPanelId.called).to.true;
                    expect(spySetSelectedPanelId.called).to.true;
                });
 
                it('vertical', () => {
                    wrapper = shallow(<SceneEditorView layout={JSON.parse(JSON.stringify(LAYOUTS[5]))}
                                                       orientation={'vertical'}
                                                       getCSSWrapperName={() => '--vertical'}
                                                       addContentItem={spyAddContentItem}
                                                       removeContentItem={spyRemoveContentItem}
                                                       selectedTabId={SELECTED_TAB_ID}
                                                       isDraggedOverPanel={spyIsDraggedOverPanel}
                                                       isSelectedPanel={spyIsSelectedPanel}
                                                       setDraggedOverPanelId={spySetDraggedOverPanelId}
                                                       updateFitMode={spyUpdateFitMode}
                                                       setSelectedPanelId={spySetSelectedPanelId}/>);
 
                    const mockVerticalClearClick = {
                        target: {
                            className: 'scene-editor-wrapper--vertical'
                        },
                        stopPropagation: sinon.spy()
                    };
 
                    wrapper.find('.scene-editor-wrapper--vertical').at(0).simulate('click', mockVerticalClearClick);
 
                    expect(spySetDraggedOverPanelId.called).to.true;
                    expect(spySetSelectedPanelId.called).to.true;
                });
            });
 
        });
 
        describe('click & trash click', () => {
            it('clear draggedOverPanelId and selectedPanelId and call updateLayout when click at layout-panel__controller--trash-can', () => {
                let stubIsSelectedPanel = sinon.stub();
                stubIsSelectedPanel.returns(false);
                stubIsSelectedPanel.withArgs('p1').returns(true);
 
                wrapper.setProps({isSelectedPanel: stubIsSelectedPanel});
                wrapper.find('.layout-panel__controller--trash-can').at(0).simulate('click');
 
                expect(spySetDraggedOverPanelId.calledWith('')).to.be.true;
                expect(spySetSelectedPanelId.calledWith('')).to.be.true;
                expect(spyRemoveContentItem.called).to.be.true;
            });
        });
 
        it('call updateFitMode when fitMode item selected', () => {
            wrapper.instance().onFitModeDropDownSelected('contain', 'p1', 'c1');
 
            expect(spyUpdateFitMode.calledWith('contain', 'p1', 'c1'));
        });
    });
});