21910: Merge branch 'main' into 21910-remove-api_client_id
[arvados.git] / services / workbench2 / src / store / groups-panel / groups-panel-middleware-service.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import Axios, { AxiosInstance, AxiosResponse } from "axios";
6 import { mockConfig } from "common/config";
7 import { createBrowserHistory } from "history";
8 import { GroupsPanelMiddlewareService } from "./groups-panel-middleware-service";
9 import { dataExplorerMiddleware } from "store/data-explorer/data-explorer-middleware";
10 import { Dispatch, MiddlewareAPI } from "redux";
11 import { DataColumns } from "components/data-table/data-table";
12 import { dataExplorerActions } from "store/data-explorer/data-explorer-action";
13 import { SortDirection } from "components/data-table/data-column";
14 import { createTree } from 'models/tree';
15 import { DataTableFilterItem } from "components/data-table-filters/data-table-filters-tree";
16 import { GROUPS_PANEL_ID } from "./groups-panel-actions";
17 import { RootState, RootStore, configureStore } from "store/store";
18 import { ServiceRepository, createServices } from "services/services";
19 import { ApiActions } from "services/api/api-actions";
20 import { ListResults } from "services/common-service/common-service";
21 import { GroupResource } from "models/group";
22 import { getResource } from "store/resources/resources";
23
24 describe("GroupsPanelMiddlewareService", () => {
25     let axiosInst: AxiosInstance;
26     let store: RootStore;
27     let services: ServiceRepository;
28     const config: any = {};
29     const actions: ApiActions = {
30         progressFn: (id: string, working: boolean) => { },
31         errorFn: (id: string, message: string) => { }
32     };
33
34     beforeEach(() => {
35         axiosInst = Axios.create({ headers: {} });
36         services = createServices(mockConfig({}), actions, axiosInst);
37         store = configureStore(createBrowserHistory(), services, config);
38     });
39
40     it("requests group member counts and updates resource store", async () => {
41         // Given
42         const fakeUuid = "zzzzz-j7d0g-000000000000000";
43         axiosInst.get = jest.fn((url: string) => {
44             if (url === '/groups') {
45                 return Promise.resolve(
46                     { data: {
47                         kind: "",
48                         offset: 0,
49                         limit: 100,
50                         items: [{
51                             can_manage: true,
52                             can_write: true,
53                             created_at: "2023-11-15T20:57:01.723043000Z",
54                             delete_at: null,
55                             description: null,
56                             etag: "0000000000000000000000000",
57                             frozen_by_uuid: null,
58                             group_class: "role",
59                             href: `/groups/${fakeUuid}`,
60                             is_trashed: false,
61                             kind: "arvados#group",
62                             modified_at: "2023-11-15T20:57:01.719986000Z",
63                             modified_by_user_uuid: "zzzzz-tpzed-000000000000000",
64                             name: "Test Group",
65                             owner_uuid: "zzzzz-tpzed-000000000000000",
66                             properties: {},
67                             trash_at: null,
68                             uuid: fakeUuid,
69                             writable_by: [
70                                 "zzzzz-tpzed-000000000000000",
71                             ]
72                         }],
73                         items_available: 1,
74                     }} as AxiosResponse);
75             } else if (url === '/links') {
76                 return Promise.resolve(
77                     { data: {
78                         items: [],
79                         items_available: 234,
80                         kind: "arvados#linkList",
81                         limit: 0,
82                         offset: 0
83                     }} as AxiosResponse);
84             } else {
85                 return Promise.resolve(
86                     { data: {}} as AxiosResponse);
87             }
88         }) as AxiosInstance['get'];
89
90         // When
91         await store.dispatch(dataExplorerActions.REQUEST_ITEMS({id: GROUPS_PANEL_ID}));
92         // Wait for async fetching of group count promises to resolve
93         await new Promise(setImmediate);
94
95         // Expect
96         expect(axiosInst.get).toHaveBeenCalledTimes(2);
97         expect(axiosInst.get).toHaveBeenCalledWith('/groups', expect.anything());
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 = jest.fn((url: string) => {
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                     }} as AxiosResponse);
138             } else if (url === '/links') {
139                 return Promise.reject();
140             } else {
141                 return Promise.resolve({ data: {}} as AxiosResponse);
142             }
143         }) as AxiosInstance['get'];
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(2);
152         expect(axiosInst.get).toHaveBeenCalledWith('/groups', expect.anything());
153         expect(axiosInst.get).toHaveBeenCalledWith('/links', expect.anything());
154         const group = getResource<GroupResource>(fakeUuid)(store.getState().resources);
155         expect(group?.memberCount).toBe(null);
156     });
157
158 });