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