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