debounce search input catalog
[sdc/sdc-workflow-designer.git] / workflow-designer-ui / src / main / frontend / src / features / catalog / __tests__ / catalogSagas-test.js
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 { runSaga } from 'redux-saga';
20 import { takeLatest } from 'redux-saga/effects';
21
22 import {
23     NAME,
24     DESC,
25     LIMIT,
26     SEARCH_CHANGED
27 } from 'features/catalog/catalogConstants';
28 import catalogApi from '../catalogApi';
29 import { fetchWorkflow, updateWorkflow } from 'features/catalog/catalogActions';
30 import catalogSaga, {
31     fetchWorkflowSaga,
32     debounceSearchChanged
33 } from 'features/catalog/catalogSagas';
34
35 jest.mock('../catalogApi');
36
37 describe('Catalog Sagas', () => {
38     it('should watch for `fetchWorkflow` action', () => {
39         const gen = catalogSaga();
40
41         expect(gen.next().value).toEqual(
42             takeLatest(fetchWorkflow, fetchWorkflowSaga)
43         );
44         expect(gen.next().value).toEqual(
45             takeLatest(SEARCH_CHANGED, debounceSearchChanged)
46         );
47         expect(gen.next().done).toBe(true);
48     });
49
50     it('should get workflows and put `updateWorkflow` action', async () => {
51         const sort = {
52             [NAME]: DESC
53         };
54         const offset = 0;
55         const searchNameFilter = undefined;
56         const data = {
57             paging: {
58                 offset,
59                 limit: 10,
60                 count: 2,
61                 hasMore: false,
62                 total: 2
63             },
64             searchNameFilter: 'w',
65             items: [
66                 {
67                     id: 'c5b7ca1a0f7944bfa948b85b32c5f314',
68                     name: 'Workflow_2',
69                     description: null,
70                     versionStates: ['DRAFT'],
71                     versions: null
72                 },
73                 {
74                     id: '221336ef3f1645c686bc81899368ac27',
75                     name: 'Workflow_1',
76                     description: null,
77                     versionStates: ['DRAFT'],
78                     versions: null
79                 }
80             ]
81         };
82         const dispatched = [];
83
84         catalogApi.getWorkflows.mockReturnValue(data);
85
86         await runSaga(
87             {
88                 dispatch: action => dispatched.push(action)
89             },
90             fetchWorkflowSaga,
91             fetchWorkflow(sort, offset)
92         ).done;
93
94         expect(dispatched).toEqual(
95             expect.arrayContaining([updateWorkflow({ sort, ...data })])
96         );
97
98         expect(catalogApi.getWorkflows).toBeCalledWith(
99             sort,
100             LIMIT,
101             offset + LIMIT,
102             searchNameFilter
103         );
104     });
105 });