Merge branch '13969-advanced-tab'
[arvados-workbench2.git] / src / store / data-explorer / data-explorer-middleware.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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
12
13 describe("DataExplorerMiddleware", () => {
14
15     it("handles only actions that are identified by service id", () => {
16         const config = {
17             id: "ServiceId",
18             columns: [{
19                 name: "Column",
20                 selected: true,
21                 configurable: false,
22                 sortDirection: SortDirection.NONE,
23                 filters: [],
24                 render: jest.fn()
25             }],
26             requestItems: jest.fn(),
27             setApi: jest.fn()
28         };
29         const service = new ServiceMock(config);
30         const api = {
31             getState: jest.fn(),
32             dispatch: jest.fn()
33         };
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);
41     });
42
43     it("handles REQUEST_ITEMS action", () => {
44         const config = {
45             id: "ServiceId",
46             columns: [{
47                 name: "Column",
48                 selected: true,
49                 configurable: false,
50                 sortDirection: SortDirection.NONE,
51                 filters: [],
52                 render: jest.fn()
53             }],
54             requestItems: jest.fn(),
55             setApi: jest.fn()
56         };
57         const service = new ServiceMock(config);
58         const api = {
59             getState: jest.fn(),
60             dispatch: jest.fn()
61         };
62         const next = jest.fn();
63         const middleware = dataExplorerMiddleware(service)(api)(next);
64         middleware(dataExplorerActions.REQUEST_ITEMS({ id: "ServiceId" }));
65         expect(config.requestItems).toHaveBeenCalled();
66     });
67
68     it("handles SET_PAGE action", () => {
69         const config = {
70             id: "ServiceId",
71             columns: [],
72             requestItems: jest.fn(),
73             setApi: jest.fn()
74         };
75         const service = new ServiceMock(config);
76         const api = {
77             getState: jest.fn(),
78             dispatch: jest.fn()
79         };
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);
84     });
85
86     it("handles SET_ROWS_PER_PAGE action", () => {
87         const config = {
88             id: "ServiceId",
89             columns: [],
90             requestItems: jest.fn(),
91             setApi: jest.fn()
92         };
93         const service = new ServiceMock(config);
94         const api = {
95             getState: jest.fn(),
96             dispatch: jest.fn()
97         };
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);
102     });
103
104     it("handles SET_FILTERS action", () => {
105         const config = {
106             id: "ServiceId",
107             columns: [],
108             requestItems: jest.fn(),
109             setApi: jest.fn()
110         };
111         const service = new ServiceMock(config);
112         const api = {
113             getState: jest.fn(),
114             dispatch: jest.fn()
115         };
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);
120     });
121
122     it("handles SET_ROWS_PER_PAGE action", () => {
123         const config = {
124             id: "ServiceId",
125             columns: [],
126             requestItems: jest.fn(),
127             setApi: jest.fn()
128         };
129         const service = new ServiceMock(config);
130         const api = {
131             getState: jest.fn(),
132             dispatch: jest.fn()
133         };
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);
138     });
139
140     it("handles TOGGLE_SORT action", () => {
141         const config = {
142             id: "ServiceId",
143             columns: [],
144             requestItems: jest.fn(),
145             setApi: jest.fn()
146         };
147         const service = new ServiceMock(config);
148         const api = {
149             getState: jest.fn(),
150             dispatch: jest.fn()
151         };
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);
156     });
157
158     it("handles SET_SEARCH_VALUE action", () => {
159         const config = {
160             id: "ServiceId",
161             columns: [],
162             requestItems: jest.fn(),
163             setApi: jest.fn()
164         };
165         const service = new ServiceMock(config);
166         const api = {
167             getState: jest.fn(),
168             dispatch: jest.fn()
169         };
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);
174     });
175
176     it("forwards other actions", () => {
177         const config = {
178             id: "ServiceId",
179             columns: [],
180             requestItems: jest.fn(),
181             setApi: jest.fn()
182         };
183         const service = new ServiceMock(config);
184         const api = {
185             getState: jest.fn(),
186             dispatch: jest.fn()
187         };
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);
195     });
196
197 });
198
199 class ServiceMock extends DataExplorerMiddlewareService {
200     constructor(private config: {
201         id: string,
202         columns: DataColumns<any>,
203         requestItems: (api: MiddlewareAPI) => void
204     }) {
205         super(config.id);
206     }
207
208     getColumns() {
209         return this.config.columns;
210     }
211
212     requestItems(api: MiddlewareAPI) {
213         this.config.requestItems(api);
214     }
215 }