Merge branch '22316-test-fixes'
[arvados.git] / services / workbench2 / src / store / groups-panel / groups-panel-middleware-service.cy.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import Axios from "axios";
6 import { mockConfig } from "common/config";
7 import { createBrowserHistory } from "history";
8 import { dataExplorerActions } from "store/data-explorer/data-explorer-action";
9 import { GROUPS_PANEL_ID } from "./groups-panel-actions";
10 import { configureStore } from "store/store";
11 import { createServices } from "services/services";
12 import { getResource } from "store/resources/resources";
13
14 describe("GroupsPanelMiddlewareService", () => {
15     let axiosInst;
16     let store;
17     let services;
18     const config = {};
19     const actions = {
20         progressFn: (id, working) => { },
21         errorFn: (id, message) => { }
22     };
23
24     beforeEach(() => {
25         axiosInst = Axios.create({ headers: {} });
26         services = createServices(mockConfig({}), actions, axiosInst);
27         store = configureStore(createBrowserHistory(), services, config);
28     });
29
30     it("requests group member counts and updates resource store", async () => {
31         // Given
32         const fakeUuid = "zzzzz-j7d0g-000000000000000";
33         axiosInst.get = cy.spy((url) => {
34             if (url === '/groups') {
35                 return Promise.resolve(
36                     { data: {
37                         kind: "",
38                         offset: 0,
39                         limit: 100,
40                         items: [{
41                             can_manage: true,
42                             can_write: true,
43                             created_at: "2023-11-15T20:57:01.723043000Z",
44                             delete_at: null,
45                             description: null,
46                             etag: "0000000000000000000000000",
47                             frozen_by_uuid: null,
48                             group_class: "role",
49                             is_trashed: false,
50                             kind: "arvados#group",
51                             modified_at: "2023-11-15T20:57:01.719986000Z",
52                             modified_by_user_uuid: "zzzzz-tpzed-000000000000000",
53                             name: "Test Group",
54                             owner_uuid: "zzzzz-tpzed-000000000000000",
55                             properties: {},
56                             trash_at: null,
57                             uuid: fakeUuid,
58                             writable_by: [
59                                 "zzzzz-tpzed-000000000000000",
60                             ]
61                         }],
62                         items_available: 1,
63                     }});
64             } else if (url === '/links') {
65                 return Promise.resolve(
66                     { data: {
67                         items: [],
68                         items_available: 234,
69                         kind: "arvados#linkList",
70                         limit: 0,
71                         offset: 0
72                     }});
73             } else {
74                 return Promise.resolve(
75                     { data: {}});
76             }
77         });
78
79         // When
80         await store.dispatch(dataExplorerActions.REQUEST_ITEMS({id: GROUPS_PANEL_ID}));
81         // Wait for async fetching of group count promises to resolve
82         await new Promise(setImmediate);
83
84         // Expect
85         expect(axiosInst.get).to.be.calledThrice;
86         expect(axiosInst.get.getCall(0).args[0]).to.equal('/groups');
87         expect(axiosInst.get.getCall(0).args[1].params).to.deep.include({count: 'none'});
88         expect(axiosInst.get.getCall(1).args[0]).to.equal('/groups');
89         expect(axiosInst.get.getCall(1).args[1].params).to.deep.include({count: 'exact', limit: 0});
90         expect(axiosInst.get.getCall(2).args[0]).to.equal('/links');
91         const group = getResource(fakeUuid)(store.getState().resources);
92         expect(group?.memberCount).to.equal(234);
93     });
94
95     it('requests group member count and stores null on failure', async () => {
96         // Given
97         const fakeUuid = "zzzzz-j7d0g-000000000000000";
98         axiosInst.get = cy.spy((url) => {
99             if (url === '/groups') {
100                 return Promise.resolve(
101                     { data: {
102                         kind: "",
103                         offset: 0,
104                         limit: 100,
105                         items: [{
106                             can_manage: true,
107                             can_write: true,
108                             created_at: "2023-11-15T20:57:01.723043000Z",
109                             delete_at: null,
110                             description: null,
111                             etag: "0000000000000000000000000",
112                             frozen_by_uuid: null,
113                             group_class: "role",
114                             is_trashed: false,
115                             kind: "arvados#group",
116                             modified_at: "2023-11-15T20:57:01.719986000Z",
117                             modified_by_user_uuid: "zzzzz-tpzed-000000000000000",
118                             name: "Test Group",
119                             owner_uuid: "zzzzz-tpzed-000000000000000",
120                             properties: {},
121                             trash_at: null,
122                             uuid: fakeUuid,
123                             writable_by: [
124                                 "zzzzz-tpzed-000000000000000",
125                             ]
126                         }],
127                         items_available: 1,
128                     }});
129             } else if (url === '/links') {
130                 return Promise.reject();
131             } else {
132                 return Promise.resolve({ data: {}});
133             }
134         });
135
136         // When
137         await store.dispatch(dataExplorerActions.REQUEST_ITEMS({id: GROUPS_PANEL_ID}));
138         // Wait for async fetching of group count promises to resolve
139         await new Promise(setImmediate);
140
141         // Expect
142         expect(axiosInst.get).to.be.calledThrice;
143         expect(axiosInst.get.getCall(0).args[0]).to.equal('/groups');
144         expect(axiosInst.get.getCall(0).args[1].params).to.deep.include({count: 'none'});
145         expect(axiosInst.get.getCall(1).args[0]).to.equal('/groups');
146         expect(axiosInst.get.getCall(1).args[1].params).to.deep.include({count: 'exact', limit: 0});
147         expect(axiosInst.get.getCall(2).args[0]).to.equal('/links');
148         const group = getResource(fakeUuid)(store.getState().resources);
149         expect(group?.memberCount).to.equal(null);
150     });
151 });