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