Implement data-explorer-middleware tests
[arvados-workbench2.git] / src / store / data-explorer / data-explorer-middleware.test.ts
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";
7
8 // Copyright (C) The Arvados Authors. All rights reserved.
9 //
10 // SPDX-License-Identifier: AGPL-3.0
11
12 describe("DataExplorerMiddleware", () => {
13     it("initializes service with middleware api", () => {
14         const config = {
15             id: "",
16             columns: [],
17             requestItems: jest.fn(),
18             setApi: jest.fn()
19         };
20         const service = new ServiceMock(config);
21         const api = {
22             getState: jest.fn(),
23             dispatch: jest.fn()
24         };
25         dataExplorerMiddleware(service)(api)(jest.fn());
26         expect(config.setApi).toHaveBeenCalled();
27     });
28
29     it("initializes columns in the store", () => {
30         const config = {
31             id: "Id",
32             columns: [{
33                 name: "Column",
34                 selected: true,
35                 render: jest.fn()
36             }],
37             requestItems: jest.fn(),
38             setApi: jest.fn()
39         };
40         const service = new ServiceMock(config);
41         const api = {
42             getState: jest.fn(),
43             dispatch: jest.fn()
44         };
45         const next = jest.fn();
46         dataExplorerMiddleware(service)(api)(next);
47         expect(next)
48             .toHaveBeenCalledWith(dataExplorerActions.SET_COLUMNS({ id: service.Id, columns: service.Columns }));
49     });
50
51     it("handles only actions that are identified by service id", () => {
52         const config = {
53             id: "ServiceId",
54             columns: [{
55                 name: "Column",
56                 selected: true,
57                 render: jest.fn()
58             }],
59             requestItems: jest.fn(),
60             setApi: jest.fn()
61         };
62         const service = new ServiceMock(config);
63         const api = {
64             getState: jest.fn(),
65             dispatch: jest.fn()
66         };
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);
74     });
75
76     it("handles REQUEST_ITEMS action", () => {
77         const config = {
78             id: "ServiceId",
79             columns: [{
80                 name: "Column",
81                 selected: true,
82                 render: jest.fn()
83             }],
84             requestItems: jest.fn(),
85             setApi: jest.fn()
86         };
87         const service = new ServiceMock(config);
88         const api = {
89             getState: jest.fn(),
90             dispatch: jest.fn()
91         };
92         const next = jest.fn();
93         const middleware = dataExplorerMiddleware(service)(api)(next);
94         middleware(dataExplorerActions.REQUEST_ITEMS({ id: "ServiceId" }));
95         expect(config.requestItems).toHaveBeenCalled();
96     });
97
98     it("handles SET_PAGE action", () => {
99         const config = {
100             id: "ServiceId",
101             columns: [],
102             requestItems: jest.fn(),
103             setApi: jest.fn()
104         };
105         const service = new ServiceMock(config);
106         const api = {
107             getState: jest.fn(),
108             dispatch: jest.fn()
109         };
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);
114     });
115
116     it("handles SET_ROWS_PER_PAGE action", () => {
117         const config = {
118             id: "ServiceId",
119             columns: [],
120             requestItems: jest.fn(),
121             setApi: jest.fn()
122         };
123         const service = new ServiceMock(config);
124         const api = {
125             getState: jest.fn(),
126             dispatch: jest.fn()
127         };
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);
132     });
133
134     it("handles SET_FILTERS action", () => {
135         const config = {
136             id: "ServiceId",
137             columns: [],
138             requestItems: jest.fn(),
139             setApi: jest.fn()
140         };
141         const service = new ServiceMock(config);
142         const api = {
143             getState: jest.fn(),
144             dispatch: jest.fn()
145         };
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);
150     });
151
152     it("handles SET_ROWS_PER_PAGE action", () => {
153         const config = {
154             id: "ServiceId",
155             columns: [],
156             requestItems: jest.fn(),
157             setApi: jest.fn()
158         };
159         const service = new ServiceMock(config);
160         const api = {
161             getState: jest.fn(),
162             dispatch: jest.fn()
163         };
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);
175     });
176
177     it("handles TOGGLE_SORT action", () => {
178         const config = {
179             id: "ServiceId",
180             columns: [],
181             requestItems: jest.fn(),
182             setApi: jest.fn()
183         };
184         const service = new ServiceMock(config);
185         const api = {
186             getState: jest.fn(),
187             dispatch: jest.fn()
188         };
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);
193     });
194
195     it("handles SET_SEARCH_VALUE action", () => {
196         const config = {
197             id: "ServiceId",
198             columns: [],
199             requestItems: jest.fn(),
200             setApi: jest.fn()
201         };
202         const service = new ServiceMock(config);
203         const api = {
204             getState: jest.fn(),
205             dispatch: jest.fn()
206         };
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);
211     });
212
213     it("forwards other actions", () => {
214         const config = {
215             id: "ServiceId",
216             columns: [],
217             requestItems: jest.fn(),
218             setApi: jest.fn()
219         };
220         const service = new ServiceMock(config);
221         const api = {
222             getState: jest.fn(),
223             dispatch: jest.fn()
224         };
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);
232     });
233
234 });
235
236 class ServiceMock extends DataExplorerMiddlewareService {
237     constructor(private config: {
238         id: string,
239         columns: DataColumns<any>,
240         requestItems: (api: MiddlewareAPI) => void;
241         setApi: () => void;
242     }) {
243         super();
244     }
245
246     get Id() {
247         return this.config.id;
248     }
249
250     get Columns() {
251         return this.config.columns;
252     }
253
254     requestItems() {
255         this.config.requestItems(this.api);
256     }
257
258     set Api(value: MiddlewareAPI) {
259         this.config.setApi();
260         this.api = value;
261     }
262
263 }