Make configurable explicit, remove getDataExplorer
[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                 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         dataExplorerMiddleware(service)(api)(next);
33         expect(next)
34             .toHaveBeenCalledWith(dataExplorerActions.SET_COLUMNS({ id: service.getId(), columns: service.getColumns() }));
35     });
36
37     it("handles only actions that are identified by service id", () => {
38         const config = {
39             id: "ServiceId",
40             columns: [{
41                 name: "Column",
42                 selected: true,
43                 configurable: false,
44                 render: jest.fn()
45             }],
46             requestItems: jest.fn(),
47             setApi: jest.fn()
48         };
49         const service = new ServiceMock(config);
50         const api = {
51             getState: jest.fn(),
52             dispatch: jest.fn()
53         };
54         const next = jest.fn();
55         const middleware = dataExplorerMiddleware(service)(api)(next);
56         middleware(dataExplorerActions.SET_PAGE({ id: "OtherId", page: 0 }));
57         middleware(dataExplorerActions.SET_PAGE({ id: "ServiceId", page: 0 }));
58         middleware(dataExplorerActions.SET_PAGE({ id: "OtherId", page: 0 }));
59         expect(api.dispatch).toHaveBeenCalledWith(dataExplorerActions.REQUEST_ITEMS({ id: "ServiceId" }));
60         expect(api.dispatch).toHaveBeenCalledTimes(1);
61     });
62
63     it("handles REQUEST_ITEMS action", () => {
64         const config = {
65             id: "ServiceId",
66             columns: [{
67                 name: "Column",
68                 selected: true,
69                 configurable: false,
70                 render: jest.fn()
71             }],
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.REQUEST_ITEMS({ id: "ServiceId" }));
83         expect(config.requestItems).toHaveBeenCalled();
84     });
85
86     it("handles SET_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_PAGE({ id: service.getId(), page: 0 }));
101         expect(api.dispatch).toHaveBeenCalledTimes(1);
102     });
103
104     it("handles SET_ROWS_PER_PAGE 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_ROWS_PER_PAGE({ id: service.getId(), rowsPerPage: 0 }));
119         expect(api.dispatch).toHaveBeenCalledTimes(1);
120     });
121
122     it("handles SET_FILTERS 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_FILTERS({ id: service.getId(), columnName: "", filters: [] }));
137         expect(api.dispatch).toHaveBeenCalledTimes(2);
138     });
139
140     it("handles SET_ROWS_PER_PAGE 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.SET_ROWS_PER_PAGE({ id: service.getId(), rowsPerPage: 0 }));
155         expect(api.dispatch).toHaveBeenCalledTimes(1);
156     });
157
158     it("handles TOGGLE_SORT 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.TOGGLE_SORT({ id: service.getId(), columnName: "" }));
173         expect(api.dispatch).toHaveBeenCalledTimes(1);
174     });
175
176     it("handles SET_SEARCH_VALUE action", () => {
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_SEARCH_VALUE({ id: service.getId(), searchValue: "" }));
191         expect(api.dispatch).toHaveBeenCalledTimes(2);
192     });
193
194     it("forwards other actions", () => {
195         const config = {
196             id: "ServiceId",
197             columns: [],
198             requestItems: jest.fn(),
199             setApi: jest.fn()
200         };
201         const service = new ServiceMock(config);
202         const api = {
203             getState: jest.fn(),
204             dispatch: jest.fn()
205         };
206         const next = jest.fn();
207         const middleware = dataExplorerMiddleware(service)(api)(next);
208         middleware(dataExplorerActions.SET_COLUMNS({ id: service.getId(), columns: [] }));
209         middleware(dataExplorerActions.SET_ITEMS({ id: service.getId(), items: [], rowsPerPage: 0, itemsAvailable: 0, page: 0 }));
210         middleware(dataExplorerActions.TOGGLE_COLUMN({ id: service.getId(), columnName: "" }));
211         expect(api.dispatch).toHaveBeenCalledTimes(0);
212         expect(next).toHaveBeenCalledTimes(4);
213     });
214
215 });
216
217 class ServiceMock extends DataExplorerMiddlewareService {
218     constructor(private config: {
219         id: string,
220         columns: DataColumns<any>,
221         requestItems: (api: MiddlewareAPI) => void;
222         setApi: () => void;
223     }) {
224         super(config.id);
225     }
226
227     getColumns() {
228         return this.config.columns;
229     }
230
231     requestItems(api: MiddlewareAPI) {
232         this.config.requestItems(api);
233     }
234 }