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