Use bindDataExplorerActions in middleware, remove api field from service class, updat...
[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 "../../../node_modules/redux";
8 import { DataColumns } from "../../components/data-table/data-table";
9 import { dataExplorerActions } from "./data-explorer-action";
10
11
12 describe("DataExplorerMiddleware", () => {
13
14     it("initializes columns in the store", () => {
15         const config = {
16             id: "Id",
17             columns: [{
18                 name: "Column",
19                 selected: true,
20                 render: jest.fn()
21             }],
22             requestItems: jest.fn(),
23             setApi: jest.fn()
24         };
25         const service = new ServiceMock(config);
26         const api = {
27             getState: jest.fn(),
28             dispatch: jest.fn()
29         };
30         const next = jest.fn();
31         dataExplorerMiddleware(service)(api)(next);
32         expect(next)
33             .toHaveBeenCalledWith(dataExplorerActions.SET_COLUMNS({ id: service.getId(), columns: service.getColumns() }));
34     });
35
36     it("handles only actions that are identified by service id", () => {
37         const config = {
38             id: "ServiceId",
39             columns: [{
40                 name: "Column",
41                 selected: true,
42                 render: jest.fn()
43             }],
44             requestItems: jest.fn(),
45             setApi: jest.fn()
46         };
47         const service = new ServiceMock(config);
48         const api = {
49             getState: jest.fn(),
50             dispatch: jest.fn()
51         };
52         const next = jest.fn();
53         const middleware = dataExplorerMiddleware(service)(api)(next);
54         middleware(dataExplorerActions.SET_PAGE({ id: "OtherId", page: 0 }));
55         middleware(dataExplorerActions.SET_PAGE({ id: "ServiceId", page: 0 }));
56         middleware(dataExplorerActions.SET_PAGE({ id: "OtherId", page: 0 }));
57         expect(api.dispatch).toHaveBeenCalledWith(dataExplorerActions.REQUEST_ITEMS({ id: "ServiceId" }));
58         expect(api.dispatch).toHaveBeenCalledTimes(1);
59     });
60
61     it("handles REQUEST_ITEMS action", () => {
62         const config = {
63             id: "ServiceId",
64             columns: [{
65                 name: "Column",
66                 selected: true,
67                 render: jest.fn()
68             }],
69             requestItems: jest.fn(),
70             setApi: jest.fn()
71         };
72         const service = new ServiceMock(config);
73         const api = {
74             getState: jest.fn(),
75             dispatch: jest.fn()
76         };
77         const next = jest.fn();
78         const middleware = dataExplorerMiddleware(service)(api)(next);
79         middleware(dataExplorerActions.REQUEST_ITEMS({ id: "ServiceId" }));
80         expect(config.requestItems).toHaveBeenCalled();
81     });
82
83     it("handles SET_PAGE action", () => {
84         const config = {
85             id: "ServiceId",
86             columns: [],
87             requestItems: jest.fn(),
88             setApi: jest.fn()
89         };
90         const service = new ServiceMock(config);
91         const api = {
92             getState: jest.fn(),
93             dispatch: jest.fn()
94         };
95         const next = jest.fn();
96         const middleware = dataExplorerMiddleware(service)(api)(next);
97         middleware(dataExplorerActions.SET_PAGE({ id: service.getId(), page: 0 }));
98         expect(api.dispatch).toHaveBeenCalledTimes(1);
99     });
100
101     it("handles SET_ROWS_PER_PAGE action", () => {
102         const config = {
103             id: "ServiceId",
104             columns: [],
105             requestItems: jest.fn(),
106             setApi: jest.fn()
107         };
108         const service = new ServiceMock(config);
109         const api = {
110             getState: jest.fn(),
111             dispatch: jest.fn()
112         };
113         const next = jest.fn();
114         const middleware = dataExplorerMiddleware(service)(api)(next);
115         middleware(dataExplorerActions.SET_ROWS_PER_PAGE({ id: service.getId(), rowsPerPage: 0 }));
116         expect(api.dispatch).toHaveBeenCalledTimes(1);
117     });
118
119     it("handles SET_FILTERS action", () => {
120         const config = {
121             id: "ServiceId",
122             columns: [],
123             requestItems: jest.fn(),
124             setApi: jest.fn()
125         };
126         const service = new ServiceMock(config);
127         const api = {
128             getState: jest.fn(),
129             dispatch: jest.fn()
130         };
131         const next = jest.fn();
132         const middleware = dataExplorerMiddleware(service)(api)(next);
133         middleware(dataExplorerActions.SET_FILTERS({ id: service.getId(), columnName: "", filters: [] }));
134         expect(api.dispatch).toHaveBeenCalledTimes(2);
135     });
136
137     it("handles SET_ROWS_PER_PAGE action", () => {
138         const config = {
139             id: "ServiceId",
140             columns: [],
141             requestItems: jest.fn(),
142             setApi: jest.fn()
143         };
144         const service = new ServiceMock(config);
145         const api = {
146             getState: jest.fn(),
147             dispatch: jest.fn()
148         };
149         const next = jest.fn();
150         const middleware = dataExplorerMiddleware(service)(api)(next);
151         middleware(dataExplorerActions.SET_ROWS_PER_PAGE({ id: service.getId(), rowsPerPage: 0 }));
152         expect(api.dispatch).toHaveBeenCalledTimes(1);
153     });
154
155     it("handles TOGGLE_SORT action", () => {
156         const config = {
157             id: "ServiceId",
158             columns: [],
159             requestItems: jest.fn(),
160             setApi: jest.fn()
161         };
162         const service = new ServiceMock(config);
163         const api = {
164             getState: jest.fn(),
165             dispatch: jest.fn()
166         };
167         const next = jest.fn();
168         const middleware = dataExplorerMiddleware(service)(api)(next);
169         middleware(dataExplorerActions.TOGGLE_SORT({ id: service.getId(), columnName: "" }));
170         expect(api.dispatch).toHaveBeenCalledTimes(1);
171     });
172
173     it("handles SET_SEARCH_VALUE action", () => {
174         const config = {
175             id: "ServiceId",
176             columns: [],
177             requestItems: jest.fn(),
178             setApi: jest.fn()
179         };
180         const service = new ServiceMock(config);
181         const api = {
182             getState: jest.fn(),
183             dispatch: jest.fn()
184         };
185         const next = jest.fn();
186         const middleware = dataExplorerMiddleware(service)(api)(next);
187         middleware(dataExplorerActions.SET_SEARCH_VALUE({ id: service.getId(), searchValue: "" }));
188         expect(api.dispatch).toHaveBeenCalledTimes(2);
189     });
190
191     it("forwards other actions", () => {
192         const config = {
193             id: "ServiceId",
194             columns: [],
195             requestItems: jest.fn(),
196             setApi: jest.fn()
197         };
198         const service = new ServiceMock(config);
199         const api = {
200             getState: jest.fn(),
201             dispatch: jest.fn()
202         };
203         const next = jest.fn();
204         const middleware = dataExplorerMiddleware(service)(api)(next);
205         middleware(dataExplorerActions.SET_COLUMNS({ id: service.getId(), columns: [] }));
206         middleware(dataExplorerActions.SET_ITEMS({ id: service.getId(), items: [], rowsPerPage: 0, itemsAvailable: 0, page: 0 }));
207         middleware(dataExplorerActions.TOGGLE_COLUMN({ id: service.getId(), columnName: "" }));
208         expect(api.dispatch).toHaveBeenCalledTimes(0);
209         expect(next).toHaveBeenCalledTimes(4);
210     });
211
212 });
213
214 class ServiceMock extends DataExplorerMiddlewareService {
215     constructor(private config: {
216         id: string,
217         columns: DataColumns<any>,
218         requestItems: (api: MiddlewareAPI) => void;
219         setApi: () => void;
220     }) {
221         super(config.id);
222     }
223
224     getColumns() {
225         return this.config.columns;
226     }
227
228     requestItems(api: MiddlewareAPI) {
229         this.config.requestItems(api);
230     }
231 }