22141: Refactoring to reduce circular import dependencies
[arvados.git] / services / workbench2 / src / store / workflow-panel / workflow-panel-actions.cy.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { isEqual } from "lodash";
6 import { createServices } from "services/services";
7 import { configureStore } from "../store";
8 import { createBrowserHistory } from "history";
9 import { mockConfig } from 'common/config';
10 import Axios from "axios";
11 import MockAdapter from "axios-mock-adapter";
12 import { openRunProcess } from './workflow-panel-actions';
13 import { runProcessPanelActions } from 'store/run-process-panel/run-process-panel-actions';
14 import { initialize } from 'redux-form';
15 import { RUN_PROCESS_INPUTS_FORM, RUN_PROCESS_BASIC_FORM } from 'store/run-process-panel/run-process-panel-actions';
16 import { ResourceKind } from 'models/resource';
17
18 describe('workflow-panel-actions', () => {
19     const axiosInst = Axios.create({ headers: {} });
20     const axiosMock = new MockAdapter(axiosInst);
21
22     let store;
23     let services;
24     const config = {};
25     const actions = {
26         progressFn: (id, working) => { },
27         errorFn: (id, message) => { }
28     };
29     let importMocks;
30
31     beforeEach(() => {
32         axiosMock.reset();
33         services = createServices(mockConfig({}), actions, axiosInst);
34         store = configureStore(createBrowserHistory(), services, config);
35         localStorage.clear();
36         importMocks = [];
37     });
38
39     afterEach(() => {
40         importMocks.map(m => m.restore());
41     });
42
43     it('opens the run process panel', async () => {
44         const wflist = [{
45             uuid: "zzzzz-7fd4e-0123456789abcde",
46             name: "foo",
47             description: "",
48             definition: "$graph: []",
49             kind: ResourceKind.WORKFLOW,
50             ownerUuid: "",
51             createdAt: "",
52             modifiedByUserUuid: "",
53             modifiedAt: "",
54             href: "",
55             etag: ""
56         }];
57         axiosMock
58             .onGet("/workflows")
59             .reply(200, {
60                 items: wflist
61             }).onGet("/links")
62             .reply(200, {
63                 items: []
64             });
65
66         const dispatchMock = cy.spy().as('dispatchMock');
67         const dispatchWrapper = (action ) => {
68             dispatchMock(action);
69             return store.dispatch(action);
70         };
71
72         const expectedBasicArgs = {
73             type: "@@redux-form/INITIALIZE",
74             meta: {
75               form: RUN_PROCESS_BASIC_FORM,
76               keepDirty: undefined,
77             },
78             payload: {
79               name: "testing",
80               owner: undefined,
81             }
82           }
83
84         await openRunProcess("zzzzz-7fd4e-0123456789abcde", "zzzzz-tpzed-0123456789abcde", "testing", { inputparm: "value" })(dispatchWrapper, store.getState, services);
85         cy.get('@dispatchMock').then((dispatchMock) => {
86             expect(dispatchMock).to.be.calledWith(runProcessPanelActions.SET_WORKFLOWS(wflist));
87             expect(dispatchMock).to.be.calledWith(runProcessPanelActions.SET_SELECTED_WORKFLOW(wflist[0]));
88             expect(arrayDeeplyIncludesObject(dispatchMock.args, expectedBasicArgs)).to.be.true;
89             expect(dispatchMock).to.be.calledWith(initialize(RUN_PROCESS_INPUTS_FORM, { inputparm: "value" }));
90         });
91     });
92 });
93
94 const arrayDeeplyIncludesObject = (array, object) => {
95     return array.some((item) => {
96         if (isEqual(item, object)) {
97             return true;
98         }
99         if (typeof item === 'object') {
100             return arrayDeeplyIncludesObject(Object.values(item), object);
101         }
102         return false;
103     });
104 };