1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { DataExplorerMiddlewareService } from "./data-explorer-middleware-service";
6 import { dataExplorerMiddleware } from "./data-explorer-middleware";
7 import { MiddlewareAPI } from "redux";
8 import { DataColumns } from "~/components/data-table/data-table";
9 import { dataExplorerActions } from "./data-explorer-action";
10 import { SortDirection } from "~/components/data-table/data-column";
11 import { createTree } from '~/models/tree';
12 import { DataTableFilterItem } from "~/components/data-table-filters/data-table-filters-tree";
15 describe("DataExplorerMiddleware", () => {
17 it("handles only actions that are identified by service id", () => {
24 sortDirection: SortDirection.NONE,
25 filters: createTree<DataTableFilterItem>(),
28 requestItems: jest.fn(),
31 const service = new ServiceMock(config);
36 const next = jest.fn();
37 const middleware = dataExplorerMiddleware(service)(api)(next);
38 middleware(dataExplorerActions.SET_PAGE({ id: "OtherId", page: 0 }));
39 middleware(dataExplorerActions.SET_PAGE({ id: "ServiceId", page: 0 }));
40 middleware(dataExplorerActions.SET_PAGE({ id: "OtherId", page: 0 }));
41 expect(api.dispatch).toHaveBeenCalledWith(dataExplorerActions.REQUEST_ITEMS({ id: "ServiceId", criteriaChanged: false }));
42 expect(api.dispatch).toHaveBeenCalledTimes(1);
45 it("handles REQUEST_ITEMS action", () => {
52 sortDirection: SortDirection.NONE,
53 filters: createTree<DataTableFilterItem>(),
56 requestItems: jest.fn(),
59 const service = new ServiceMock(config);
64 const next = jest.fn();
65 const middleware = dataExplorerMiddleware(service)(api)(next);
66 middleware(dataExplorerActions.REQUEST_ITEMS({ id: "ServiceId" }));
67 expect(config.requestItems).toHaveBeenCalled();
70 it("handles SET_PAGE action", () => {
74 requestItems: jest.fn(),
77 const service = new ServiceMock(config);
82 const next = jest.fn();
83 const middleware = dataExplorerMiddleware(service)(api)(next);
84 middleware(dataExplorerActions.SET_PAGE({ id: service.getId(), page: 0 }));
85 expect(api.dispatch).toHaveBeenCalledTimes(1);
88 it("handles SET_ROWS_PER_PAGE action", () => {
92 requestItems: jest.fn(),
95 const service = new ServiceMock(config);
100 const next = jest.fn();
101 const middleware = dataExplorerMiddleware(service)(api)(next);
102 middleware(dataExplorerActions.SET_ROWS_PER_PAGE({ id: service.getId(), rowsPerPage: 0 }));
103 expect(api.dispatch).toHaveBeenCalledTimes(1);
106 it("handles SET_FILTERS action", () => {
110 requestItems: jest.fn(),
113 const service = new ServiceMock(config);
118 const next = jest.fn();
119 const middleware = dataExplorerMiddleware(service)(api)(next);
120 middleware(dataExplorerActions.SET_FILTERS({ id: service.getId(), columnName: "", filters: createTree() }));
121 expect(api.dispatch).toHaveBeenCalledTimes(2);
124 it("handles SET_ROWS_PER_PAGE action", () => {
128 requestItems: jest.fn(),
131 const service = new ServiceMock(config);
136 const next = jest.fn();
137 const middleware = dataExplorerMiddleware(service)(api)(next);
138 middleware(dataExplorerActions.SET_ROWS_PER_PAGE({ id: service.getId(), rowsPerPage: 0 }));
139 expect(api.dispatch).toHaveBeenCalledTimes(1);
142 it("handles TOGGLE_SORT action", () => {
146 requestItems: jest.fn(),
149 const service = new ServiceMock(config);
154 const next = jest.fn();
155 const middleware = dataExplorerMiddleware(service)(api)(next);
156 middleware(dataExplorerActions.TOGGLE_SORT({ id: service.getId(), columnName: "" }));
157 expect(api.dispatch).toHaveBeenCalledTimes(1);
160 it("handles SET_SEARCH_VALUE action", () => {
164 requestItems: jest.fn(),
167 const service = new ServiceMock(config);
172 const next = jest.fn();
173 const middleware = dataExplorerMiddleware(service)(api)(next);
174 middleware(dataExplorerActions.SET_EXPLORER_SEARCH_VALUE({ id: service.getId(), searchValue: "" }));
175 expect(api.dispatch).toHaveBeenCalledTimes(2);
178 it("forwards other actions", () => {
182 requestItems: jest.fn(),
185 const service = new ServiceMock(config);
190 const next = jest.fn();
191 const middleware = dataExplorerMiddleware(service)(api)(next);
192 middleware(dataExplorerActions.SET_COLUMNS({ id: service.getId(), columns: [] }));
193 middleware(dataExplorerActions.SET_ITEMS({ id: service.getId(), items: [], rowsPerPage: 0, itemsAvailable: 0, page: 0 }));
194 middleware(dataExplorerActions.TOGGLE_COLUMN({ id: service.getId(), columnName: "" }));
195 expect(api.dispatch).toHaveBeenCalledTimes(0);
196 expect(next).toHaveBeenCalledTimes(3);
201 class ServiceMock extends DataExplorerMiddlewareService {
202 constructor(private config: {
204 columns: DataColumns<any>,
205 requestItems: (api: MiddlewareAPI) => void
211 return this.config.columns;
214 requestItems(api: MiddlewareAPI) {
215 this.config.requestItems(api);