2df15c61b11f2472e609fe82491a6cf4fbffa66c
[sdc.git] / openecomp-ui / test / nfvo-components / input / dualListBox / dualListbox.test.js
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 import React from 'react';
18 import TestUtils from 'react-dom/test-utils';
19 import DualListboxView from 'nfvo-components/input/dualListbox/DualListboxView.jsx';
20 import ShallowRenderer from 'react-test-renderer/shallow';
21
22 const ITEMS = [{id: '1', name: 'aaa'}, {id: '2', name: 'bbb'}, {id: '3', name: 'ccc'}];
23
24 describe('dualListBox Module Tests', function () {
25
26
27         it('should render basically', () => {
28                 const renderer = new ShallowRenderer();
29                 renderer.render(<DualListboxView onChange={()=>{}}/>);
30                 var renderedOutput = renderer.getRenderOutput();
31                 expect(renderedOutput).toBeTruthy();
32         });
33
34         it('should render with available list and 4 control buttons', () => {
35                 var view = TestUtils.renderIntoDocument(<DualListboxView availableList={ITEMS} onChange={()=>{}}/>);
36                 expect(view).toBeTruthy();
37                 var results = TestUtils.scryRenderedDOMComponentsWithClass(view, 'dual-list-option');
38                 expect(results.length).toBe(4);
39         });
40
41         it('should add item to selected list', done => {
42                 const onChange = (values)=> {
43                         expect(values).toEqual([ITEMS[2].id, ITEMS[0].id]);
44                         done();
45                 };
46                 const document = TestUtils.renderIntoDocument(
47                         <DualListboxView
48                                 availableList={ITEMS}
49                                 onChange={onChange}
50                                 selectedValuesList={[ITEMS[2].id]}/>);
51
52                 const result = TestUtils.scryRenderedDOMComponentsWithTag(document, 'select');
53                 const options = TestUtils.scryRenderedDOMComponentsWithTag(document, 'option');
54                 const listBox = TestUtils.findRenderedComponentWithType(document, DualListboxView);
55                 expect(result).toBeTruthy();
56                 expect(options).toBeTruthy();
57                 expect(listBox).toBeTruthy();
58
59                 TestUtils.Simulate.change(result[0], {target: {selectedOptions: [options[0]]}});
60                 expect(listBox.state.selectedValues).toEqual([ITEMS[0].id]);
61
62                 listBox.addToSelectedList();
63         });
64
65         it('should remove item from selected list', done => {
66                 const onChange = (values)=> {
67                         expect(values).toEqual([ITEMS[0].id]);
68                         done();
69                 };
70                 const document = TestUtils.renderIntoDocument(
71                         <DualListboxView
72                                 availableList={ITEMS}
73                                 onChange={onChange}
74                                 selectedValuesList={[ITEMS[0].id, ITEMS[1].id]}/>);
75
76                 const result = TestUtils.scryRenderedDOMComponentsWithTag(document, 'select');
77                 const options = TestUtils.scryRenderedDOMComponentsWithTag(document, 'option');
78                 const listBox = TestUtils.findRenderedComponentWithType(document, DualListboxView);
79                 expect(result).toBeTruthy();
80                 expect(options).toBeTruthy();
81                 expect(listBox).toBeTruthy();
82
83                 TestUtils.Simulate.change(result[1], {target: {selectedOptions: [options[2]]}});
84                 expect(listBox.state.selectedValues).toEqual([ITEMS[1].id]);
85
86                 listBox.removeFromSelectedList();
87         });
88
89         it('should add all items to selected list', done => {
90                 let onChange = (value)=> {
91                         expect(value).toEqual(ITEMS.map(item => item.id));
92                         done();
93                 };
94                 var view = new DualListboxView({availableList:ITEMS, onChange, selectedValuesList:[]});
95                 expect(view).toBeTruthy();
96                 view.addAllToSelectedList();
97         });
98
99         it('should remove all items from selected list', done => {
100                 let onChange = (value)=> {
101                         expect(value.length).toBe(0);
102                         done();
103                 };
104                 var view = new DualListboxView({availableList:ITEMS, onChange, selectedValuesList:[]});
105                 expect(view).toBeTruthy();
106                 view.removeAllFromSelectedList();
107         });
108
109
110 });