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