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";
13 describe("DataExplorerMiddleware", () => {
15 it("handles only actions that are identified by service id", () => {
22 sortDirection: SortDirection.NONE,
26 requestItems: jest.fn(),
29 const service = new ServiceMock(config);
34 const next = jest.fn();
35 const middleware = dataExplorerMiddleware(service)(api)(next);
36 middleware(dataExplorerActions.SET_PAGE({ id: "OtherId", page: 0 }));
37 middleware(dataExplorerActions.SET_PAGE({ id: "ServiceId", page: 0 }));
38 middleware(dataExplorerActions.SET_PAGE({ id: "OtherId", page: 0 }));
39 expect(api.dispatch).toHaveBeenCalledWith(dataExplorerActions.REQUEST_ITEMS({ id: "ServiceId" }));
40 expect(api.dispatch).toHaveBeenCalledTimes(1);
43 it("handles REQUEST_ITEMS action", () => {
50 sortDirection: SortDirection.NONE,
54 requestItems: jest.fn(),
57 const service = new ServiceMock(config);
62 const next = jest.fn();
63 const middleware = dataExplorerMiddleware(service)(api)(next);
64 middleware(dataExplorerActions.REQUEST_ITEMS({ id: "ServiceId" }));
65 expect(config.requestItems).toHaveBeenCalled();
68 it("handles SET_PAGE action", () => {
72 requestItems: jest.fn(),
75 const service = new ServiceMock(config);
80 const next = jest.fn();
81 const middleware = dataExplorerMiddleware(service)(api)(next);
82 middleware(dataExplorerActions.SET_PAGE({ id: service.getId(), page: 0 }));
83 expect(api.dispatch).toHaveBeenCalledTimes(1);
86 it("handles SET_ROWS_PER_PAGE action", () => {
90 requestItems: jest.fn(),
93 const service = new ServiceMock(config);
98 const next = jest.fn();
99 const middleware = dataExplorerMiddleware(service)(api)(next);
100 middleware(dataExplorerActions.SET_ROWS_PER_PAGE({ id: service.getId(), rowsPerPage: 0 }));
101 expect(api.dispatch).toHaveBeenCalledTimes(1);
104 it("handles SET_FILTERS action", () => {
108 requestItems: jest.fn(),
111 const service = new ServiceMock(config);
116 const next = jest.fn();
117 const middleware = dataExplorerMiddleware(service)(api)(next);
118 middleware(dataExplorerActions.SET_FILTERS({ id: service.getId(), columnName: "", filters: [] }));
119 expect(api.dispatch).toHaveBeenCalledTimes(2);
122 it("handles SET_ROWS_PER_PAGE action", () => {
126 requestItems: jest.fn(),
129 const service = new ServiceMock(config);
134 const next = jest.fn();
135 const middleware = dataExplorerMiddleware(service)(api)(next);
136 middleware(dataExplorerActions.SET_ROWS_PER_PAGE({ id: service.getId(), rowsPerPage: 0 }));
137 expect(api.dispatch).toHaveBeenCalledTimes(1);
140 it("handles TOGGLE_SORT action", () => {
144 requestItems: jest.fn(),
147 const service = new ServiceMock(config);
152 const next = jest.fn();
153 const middleware = dataExplorerMiddleware(service)(api)(next);
154 middleware(dataExplorerActions.TOGGLE_SORT({ id: service.getId(), columnName: "" }));
155 expect(api.dispatch).toHaveBeenCalledTimes(1);
158 it("handles SET_SEARCH_VALUE action", () => {
162 requestItems: jest.fn(),
165 const service = new ServiceMock(config);
170 const next = jest.fn();
171 const middleware = dataExplorerMiddleware(service)(api)(next);
172 middleware(dataExplorerActions.SET_SEARCH_VALUE({ id: service.getId(), searchValue: "" }));
173 expect(api.dispatch).toHaveBeenCalledTimes(2);
176 it("forwards other actions", () => {
180 requestItems: jest.fn(),
183 const service = new ServiceMock(config);
188 const next = jest.fn();
189 const middleware = dataExplorerMiddleware(service)(api)(next);
190 middleware(dataExplorerActions.SET_COLUMNS({ id: service.getId(), columns: [] }));
191 middleware(dataExplorerActions.SET_ITEMS({ id: service.getId(), items: [], rowsPerPage: 0, itemsAvailable: 0, page: 0 }));
192 middleware(dataExplorerActions.TOGGLE_COLUMN({ id: service.getId(), columnName: "" }));
193 expect(api.dispatch).toHaveBeenCalledTimes(0);
194 expect(next).toHaveBeenCalledTimes(3);
199 class ServiceMock extends DataExplorerMiddlewareService {
200 constructor(private config: {
202 columns: DataColumns<any>,
203 requestItems: (api: MiddlewareAPI) => void
209 return this.config.columns;
212 requestItems(api: MiddlewareAPI) {
213 this.config.requestItems(api);