1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
24 describe("GroupsPanelMiddlewareService", () => {
25 let axiosInst: AxiosInstance;
27 let services: ServiceRepository;
28 const config: any = {};
29 const actions: ApiActions = {
30 progressFn: (id: string, working: boolean) => { },
31 errorFn: (id: string, message: string) => { }
35 axiosInst = Axios.create({ headers: {} });
36 services = createServices(mockConfig({}), actions, axiosInst);
37 store = configureStore(createBrowserHistory(), services, config);
40 it("requests group member counts and updates resource store", async () => {
42 const fakeUuid = "zzzzz-j7d0g-000000000000000";
43 axiosInst.get = jest.fn((url: string) => {
44 if (url === '/groups') {
45 return Promise.resolve(
53 created_at: "2023-11-15T20:57:01.723043000Z",
56 etag: "0000000000000000000000000",
59 href: `/groups/${fakeUuid}`,
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",
66 owner_uuid: "zzzzz-tpzed-000000000000000",
71 "zzzzz-tpzed-000000000000000",
76 } else if (url === '/links') {
77 return Promise.resolve(
81 kind: "arvados#linkList",
86 return Promise.resolve(
87 { data: {}} as AxiosResponse);
89 }) as AxiosInstance['get'];
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);
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);
104 it('requests group member count and stores null on failure', async () => {
106 const fakeUuid = "zzzzz-j7d0g-000000000000000";
107 axiosInst.get = jest.fn((url: string) => {
108 if (url === '/groups') {
109 return Promise.resolve(
117 created_at: "2023-11-15T20:57:01.723043000Z",
120 etag: "0000000000000000000000000",
121 frozen_by_uuid: null,
123 href: `/groups/${fakeUuid}`,
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",
130 owner_uuid: "zzzzz-tpzed-000000000000000",
135 "zzzzz-tpzed-000000000000000",
139 }} as AxiosResponse);
140 } else if (url === '/links') {
141 return Promise.reject();
143 return Promise.resolve({ data: {}} as AxiosResponse);
145 }) as AxiosInstance['get'];
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);
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);