b923f4a459be0ed7d063029fed1cf4987db5ae13
[sdc/sdc-workflow-designer.git] /
1 /*
2 * Copyright © 2018 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 {
18     STRING,
19     INPUTS,
20     OUTPUTS
21 } from 'features/version/inputOutput/inputOutputConstants';
22 import inputOutputReducer, {
23     initialState,
24     defaultInputOutput
25 } from 'features/version/inputOutput/inputOutputReducer';
26 import {
27     setInputsOutputs,
28     changeError,
29     showInputs,
30     showOutputs,
31     search,
32     add,
33     changeName,
34     changeType,
35     changeMandatory,
36     remove
37 } from 'features/version/inputOutput/inputOutputActions';
38
39 describe('Input/Output Reducer', () => {
40     it('should return initialState', () => {
41         expect(inputOutputReducer(undefined, {})).toEqual(initialState);
42     });
43
44     it('should set inputs/outputs', () => {
45         const payload = {
46             inputs: [
47                 {
48                     name: 'Input',
49                     type: STRING,
50                     mandatory: false
51                 }
52             ],
53             outputs: {
54                 name: 'Output',
55                 type: STRING,
56                 mandatory: false
57             }
58         };
59
60         expect(
61             inputOutputReducer(undefined, setInputsOutputs(payload))
62         ).toEqual({ ...initialState, ...payload });
63     });
64
65     it('should change input/output error', () => {
66         const payload = {
67             alreadyExists: [1, 2],
68             invalidCharacteres: [3, 4]
69         };
70         [INPUTS, OUTPUTS].forEach(current => {
71             const state = { ...initialState, current };
72             expect(inputOutputReducer(state, changeError(payload))).toEqual({
73                 ...state,
74                 error: {
75                     ...state.error,
76                     [current]: payload
77                 }
78             });
79         });
80     });
81
82     it('should show inputs', () => {
83         expect(inputOutputReducer(undefined, showInputs())).toEqual({
84             ...initialState,
85             current: INPUTS
86         });
87     });
88
89     it('should show outputs', () => {
90         expect(inputOutputReducer(undefined, showOutputs())).toEqual({
91             ...initialState,
92             current: OUTPUTS
93         });
94     });
95
96     it('should add input/output', () => {
97         [INPUTS, OUTPUTS].forEach(current => {
98             const state = { ...initialState, current };
99             expect(inputOutputReducer(state, add())).toEqual({
100                 ...state,
101                 [current]: [...state[current], defaultInputOutput[current]]
102             });
103         });
104     });
105
106     it('should add search', () => {
107         const payload = 'Search string';
108         expect(inputOutputReducer(undefined, search(payload))).toEqual({
109             ...initialState,
110             search: payload
111         });
112     });
113
114     it('should change input/output name/type/mandatory', () => {
115         const name = 'New name';
116         const type = 'New Type';
117         const mandatory = true;
118         const key = 0;
119         const state = {
120             ...initialState,
121             [INPUTS]: [
122                 {
123                     name: 'Old name',
124                     type: 'Old type',
125                     mandatory: false
126                 }
127             ],
128             [OUTPUTS]: [
129                 {
130                     name: 'Old name',
131                     type: 'Old type',
132                     mandatory: false
133                 }
134             ]
135         };
136         [INPUTS, OUTPUTS].forEach(current => {
137             [
138                 {
139                     action: changeName(name, key),
140                     field: 'name',
141                     value: name
142                 },
143                 {
144                     action: changeType(type, key),
145                     field: 'type',
146                     value: type
147                 },
148                 {
149                     action: changeMandatory(mandatory, key),
150                     field: 'mandatory',
151                     value: mandatory
152                 }
153             ].forEach(actionMap => {
154                 const actual = inputOutputReducer(
155                     { ...state, current },
156                     actionMap.action
157                 )[current][key][actionMap.field];
158
159                 const expected = actionMap.value;
160
161                 expect(actual).toEqual(expected);
162             });
163         });
164     });
165
166     it('should remove input/output ', () => {
167         const key = 0;
168         const state = {
169             ...initialState,
170             [INPUTS]: [
171                 {
172                     name: 'Name',
173                     type: 'String',
174                     mandatory: true
175                 }
176             ],
177             [OUTPUTS]: [
178                 {
179                     name: 'Name',
180                     type: 'String',
181                     mandatory: true
182                 }
183             ]
184         };
185         [INPUTS, OUTPUTS].forEach(current => {
186             expect(
187                 inputOutputReducer({ ...state, current }, remove(key))[current]
188             ).toEqual([]);
189         });
190     });
191 });