c4f34e7fd8b318441c0d1c8efad005394f7d8a22
[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 'use strict';
18
19 import { NAME, ASC, DESC } from 'features/catalog/catalogConstants';
20 import catalogReducer, { initialState } from 'features/catalog/catalogReducer';
21 import { updateWorkflow, resetWorkflow } from 'features/catalog/catalogActions';
22
23 describe('Catalog Reducer', () => {
24     const state = {
25         hasMore: true,
26         results: [
27             {
28                 id: '755eab7752374a2380544065b59b082d',
29                 name: 'Workflow 1',
30                 description: 'description description 1'
31             },
32             {
33                 id: 'ef8159204dac4c10a85b29ec30b4bd56',
34                 name: 'Workflow 2',
35                 description: 'description description 2'
36             }
37         ],
38         total: 0,
39         sort: {
40             [NAME]: ASC
41         }
42     };
43     const sort = {
44         [NAME]: DESC
45     };
46     const page = 0;
47     const data = {
48         total: 20,
49         size: 100,
50         page,
51         sort,
52         results: [
53             {
54                 id: '755eab7752374a2380544065b59b082d',
55                 name: 'Workflow 11',
56                 description: 'description description 11'
57             },
58             {
59                 id: 'ef8159204dac4c10a85b29ec30b4bd56',
60                 name: 'Workflow 22',
61                 description: 'description description 22'
62             }
63         ]
64     };
65
66     it('returns the initial state', () => {
67         expect(catalogReducer(undefined, {})).toEqual(initialState);
68     });
69
70     it('should replace results when page is first', () => {
71         expect(catalogReducer(state, updateWorkflow({ ...data }))).toEqual({
72             ...initialState,
73             ...data,
74             hasMore: data.results.length < data.total,
75             page,
76             sort
77         });
78     });
79
80     it('should add results when page is not first', () => {
81         expect(
82             catalogReducer(state, updateWorkflow({ ...data, page: 1 })).results
83         ).toEqual(expect.arrayContaining([...data.results, ...state.results]));
84     });
85
86     it('should reset state', () => {
87         expect(catalogReducer({ ...state, sort }, resetWorkflow())).toEqual({
88             ...initialState,
89             sort
90         });
91     });
92 });