1 import { DataExplorerMiddlewareService } from "./data-explorer-middleware-service";
2 import { dataExplorerMiddleware } from "./data-explorer-middleware";
3 import { MiddlewareAPI } from "../../../node_modules/redux";
4 import { columns } from "../../views/project-panel/project-panel";
5 import { DataColumns } from "../../components/data-table/data-table";
6 import { dataExplorerActions } from "./data-explorer-action";
8 // Copyright (C) The Arvados Authors. All rights reserved.
10 // SPDX-License-Identifier: AGPL-3.0
12 describe("DataExplorerMiddleware", () => {
13 it("initializes service with middleware api", () => {
17 requestItems: jest.fn(),
20 const service = new ServiceMock(config);
25 dataExplorerMiddleware(service)(api)(jest.fn());
26 expect(config.setApi).toHaveBeenCalled();
29 it("initializes columns in the store", () => {
37 requestItems: jest.fn(),
40 const service = new ServiceMock(config);
45 const next = jest.fn();
46 dataExplorerMiddleware(service)(api)(next);
48 .toHaveBeenCalledWith(dataExplorerActions.SET_COLUMNS({ id: service.Id, columns: service.Columns }));
51 it("handles only actions that are identified by service id", () => {
59 requestItems: jest.fn(),
62 const service = new ServiceMock(config);
67 const next = jest.fn();
68 const middleware = dataExplorerMiddleware(service)(api)(next);
69 middleware(dataExplorerActions.SET_PAGE({ id: "OtherId", page: 0 }));
70 middleware(dataExplorerActions.SET_PAGE({ id: "ServiceId", page: 0 }));
71 middleware(dataExplorerActions.SET_PAGE({ id: "OtherId", page: 0 }));
72 expect(api.dispatch).toHaveBeenCalledWith(dataExplorerActions.REQUEST_ITEMS({ id: "ServiceId" }));
73 expect(api.dispatch).toHaveBeenCalledTimes(1);
76 it("handles REQUEST_ITEMS action", () => {
84 requestItems: jest.fn(),
87 const service = new ServiceMock(config);
92 const next = jest.fn();
93 const middleware = dataExplorerMiddleware(service)(api)(next);
94 middleware(dataExplorerActions.REQUEST_ITEMS({ id: "ServiceId" }));
95 expect(config.requestItems).toHaveBeenCalled();
98 it("handles SET_PAGE action", () => {
102 requestItems: jest.fn(),
105 const service = new ServiceMock(config);
110 const next = jest.fn();
111 const middleware = dataExplorerMiddleware(service)(api)(next);
112 middleware(dataExplorerActions.SET_PAGE({ id: service.Id, page: 0 }));
113 expect(api.dispatch).toHaveBeenCalledTimes(1);
116 it("handles SET_ROWS_PER_PAGE action", () => {
120 requestItems: jest.fn(),
123 const service = new ServiceMock(config);
128 const next = jest.fn();
129 const middleware = dataExplorerMiddleware(service)(api)(next);
130 middleware(dataExplorerActions.SET_ROWS_PER_PAGE({ id: service.Id, rowsPerPage: 0 }));
131 expect(api.dispatch).toHaveBeenCalledTimes(1);
134 it("handles SET_FILTERS action", () => {
138 requestItems: jest.fn(),
141 const service = new ServiceMock(config);
146 const next = jest.fn();
147 const middleware = dataExplorerMiddleware(service)(api)(next);
148 middleware(dataExplorerActions.SET_FILTERS({ id: service.Id, columnName: "", filters: [] }));
149 expect(api.dispatch).toHaveBeenCalledTimes(2);
152 it("handles SET_ROWS_PER_PAGE action", () => {
156 requestItems: jest.fn(),
159 const service = new ServiceMock(config);
164 const next = jest.fn();
165 const middleware = dataExplorerMiddleware(service)(api)(next);
166 middleware(dataExplorerActions.SET_PAGE({ id: service.Id, page: 0 }));
167 middleware(dataExplorerActions.SET_ROWS_PER_PAGE({ id: service.Id, rowsPerPage: 0 }));
168 middleware(dataExplorerActions.SET_FILTERS({ id: service.Id, columnName: "", filters: [] }));
169 middleware(dataExplorerActions.TOGGLE_SORT({ id: service.Id, columnName: "" }));
170 middleware(dataExplorerActions.TOGGLE_COLUMN({ id: service.Id, columnName: "" }));
171 middleware(dataExplorerActions.REQUEST_ITEMS({ id: service.Id }));
172 middleware(dataExplorerActions.SET_SEARCH_VALUE({ id: service.Id, searchValue: "" }));
173 middleware(dataExplorerActions.RESET_PAGINATION({ id: service.Id }));
174 expect(api.dispatch).toHaveBeenCalledTimes(7);
177 it("handles TOGGLE_SORT action", () => {
181 requestItems: jest.fn(),
184 const service = new ServiceMock(config);
189 const next = jest.fn();
190 const middleware = dataExplorerMiddleware(service)(api)(next);
191 middleware(dataExplorerActions.TOGGLE_SORT({ id: service.Id, columnName: "" }));
192 expect(api.dispatch).toHaveBeenCalledTimes(1);
195 it("handles SET_SEARCH_VALUE action", () => {
199 requestItems: jest.fn(),
202 const service = new ServiceMock(config);
207 const next = jest.fn();
208 const middleware = dataExplorerMiddleware(service)(api)(next);
209 middleware(dataExplorerActions.SET_SEARCH_VALUE({ id: service.Id, searchValue: "" }));
210 expect(api.dispatch).toHaveBeenCalledTimes(2);
213 it("forwards other actions", () => {
217 requestItems: jest.fn(),
220 const service = new ServiceMock(config);
225 const next = jest.fn();
226 const middleware = dataExplorerMiddleware(service)(api)(next);
227 middleware(dataExplorerActions.SET_COLUMNS({ id: service.Id, columns: [] }));
228 middleware(dataExplorerActions.SET_ITEMS({ id: service.Id, items: [], rowsPerPage: 0, itemsAvailable: 0, page: 0 }));
229 middleware(dataExplorerActions.TOGGLE_COLUMN({ id: service.Id, columnName: "" }));
230 expect(api.dispatch).toHaveBeenCalledTimes(0);
231 expect(next).toHaveBeenCalledTimes(4);
236 class ServiceMock extends DataExplorerMiddlewareService {
237 constructor(private config: {
239 columns: DataColumns<any>,
240 requestItems: (api: MiddlewareAPI) => void;
247 return this.config.id;
251 return this.config.columns;
255 this.config.requestItems(this.api);
258 set Api(value: MiddlewareAPI) {
259 this.config.setApi();