Merge branch 'main' into 21720-material-ui-upgrade
[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                             href: `/groups/${fakeUuid}`,
50                             is_trashed: false,
51                             kind: "arvados#group",
52                             modified_at: "2023-11-15T20:57:01.719986000Z",
53                             modified_by_user_uuid: "zzzzz-tpzed-000000000000000",
54                             name: "Test Group",
55                             owner_uuid: "zzzzz-tpzed-000000000000000",
56                             properties: {},
57                             trash_at: null,
58                             uuid: fakeUuid,
59                             writable_by: [
60                                 "zzzzz-tpzed-000000000000000",
61                             ]
62                         }],
63                         items_available: 1,
64                     }});
65             } else if (url === '/links') {
66                 return Promise.resolve(
67                     { data: {
68                         items: [],
69                         items_available: 234,
70                         kind: "arvados#linkList",
71                         limit: 0,
72                         offset: 0
73                     }});
74             } else {
75                 return Promise.resolve(
76                     { data: {}});
77             }
78         });
79
80         // When
81         await store.dispatch(dataExplorerActions.REQUEST_ITEMS({id: GROUPS_PANEL_ID}));
82         // Wait for async fetching of group count promises to resolve
83         await new Promise(setImmediate);
84
85         // Expect
86         expect(axiosInst.get).toHaveBeenCalledTimes(3);
87         expect(axiosInst.get).toHaveBeenNthCalledWith(1, '/groups', {
88             params: expect.objectContaining({
89                 count: 'none',
90             }),
91         });
92         expect(axiosInst.get).toHaveBeenNthCalledWith(2, '/groups', {
93             params: expect.objectContaining({
94                 count: 'exact',
95                 limit: 0,
96             }),
97         });
98         expect(axiosInst.get).toHaveBeenCalledWith('/links', expect.anything());
99         const group = getResource<GroupResource>(fakeUuid)(store.getState().resources);
100         expect(group?.memberCount).toBe(234);
101     });
102
103     it('requests group member count and stores null on failure', async () => {
104         // Given
105         const fakeUuid = "zzzzz-j7d0g-000000000000000";
106         axiosInst.get = cy.spy((url) => {
107             if (url === '/groups') {
108                 return Promise.resolve(
109                     { data: {
110                         kind: "",
111                         offset: 0,
112                         limit: 100,
113                         items: [{
114                             can_manage: true,
115                             can_write: true,
116                             created_at: "2023-11-15T20:57:01.723043000Z",
117                             delete_at: null,
118                             description: null,
119                             etag: "0000000000000000000000000",
120                             frozen_by_uuid: null,
121                             group_class: "role",
122                             href: `/groups/${fakeUuid}`,
123                             is_trashed: false,
124                             kind: "arvados#group",
125                             modified_at: "2023-11-15T20:57:01.719986000Z",
126                             modified_by_user_uuid: "zzzzz-tpzed-000000000000000",
127                             name: "Test Group",
128                             owner_uuid: "zzzzz-tpzed-000000000000000",
129                             properties: {},
130                             trash_at: null,
131                             uuid: fakeUuid,
132                             writable_by: [
133                                 "zzzzz-tpzed-000000000000000",
134                             ]
135                         }],
136                         items_available: 1,
137                     }});
138             } else if (url === '/links') {
139                 return Promise.reject();
140             } else {
141                 return Promise.resolve({ data: {}});
142             }
143         });
144
145         // When
146         await store.dispatch(dataExplorerActions.REQUEST_ITEMS({id: GROUPS_PANEL_ID}));
147         // Wait for async fetching of group count promises to resolve
148         await new Promise(setImmediate);
149
150         // Expect
151         expect(axiosInst.get).toHaveBeenCalledTimes(3);
152         expect(axiosInst.get).toHaveBeenNthCalledWith(1, '/groups', {
153             params: expect.objectContaining({
154                 count: 'none',
155             }),
156         });
157         expect(axiosInst.get).toHaveBeenNthCalledWith(2, '/groups', {
158             params: expect.objectContaining({
159                 count: 'exact',
160                 limit: 0,
161             }),
162         });
163         expect(axiosInst.get).toHaveBeenCalledWith('/links', expect.anything());
164         const group = getResource<GroupResource>(fakeUuid)(store.getState().resources);
165         expect(group?.memberCount).toBe(null);
166     });
167
168 });