Create data explorer actions and reducer
[arvados.git] / src / store / data-explorer / data-explorer-reducer.test.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import dataExplorerReducer, { initialDataExplorer } from "./data-explorer-reducer";
6 import actions from "./data-explorer-action";
7 import { DataColumn } from "../../components/data-table/data-column";
8 import { DataTableFilterItem } from "../../components/data-table-filters/data-table-filters";
9
10 describe('data-explorer-reducer', () => {
11     it('should set columns', () => {
12         const columns: Array<DataColumn<any>> = [{
13             name: "Column 1",
14             render: jest.fn(),
15             selected: true
16         }];
17         const state = dataExplorerReducer(undefined,
18             actions.SET_COLUMNS({ id: "Data explorer", columns }));
19         expect(state["Data explorer"].columns).toEqual(columns);
20     });
21
22     it('should toggle sorting', () => {
23         const columns: Array<DataColumn<any>> = [{
24             name: "Column 1",
25             render: jest.fn(),
26             selected: true,
27             sortDirection: "asc"
28         }, {
29             name: "Column 2",
30             render: jest.fn(),
31             selected: true,
32             sortDirection: "none",
33         }];
34         const state = dataExplorerReducer({ "Data explorer": { ...initialDataExplorer, columns } },
35             actions.TOGGLE_SORT({ id: "Data explorer", columnName: "Column 2" }));
36         expect(state["Data explorer"].columns[0].sortDirection).toEqual("none");
37         expect(state["Data explorer"].columns[1].sortDirection).toEqual("asc");
38     });
39
40     it('should set filters', () => {
41         const columns: Array<DataColumn<any>> = [{
42             name: "Column 1",
43             render: jest.fn(),
44             selected: true,
45         }];
46
47         const filters: DataTableFilterItem[] = [{
48             name: "Filter 1",
49             selected: true
50         }];
51         const state = dataExplorerReducer({ "Data explorer": { ...initialDataExplorer, columns } },
52             actions.SET_FILTERS({ id: "Data explorer", columnName: "Column 1", filters }));
53         expect(state["Data explorer"].columns[0].filters).toEqual(filters);
54     });
55
56     it('should set items', () => {
57         const state = dataExplorerReducer({ "Data explorer": undefined },
58             actions.SET_ITEMS({ id: "Data explorer", items: ["Item 1", "Item 2"] }));
59         expect(state["Data explorer"].items).toEqual(["Item 1", "Item 2"]);
60     });
61
62     it('should set page', () => {
63         const state = dataExplorerReducer({ "Data explorer": undefined },
64             actions.SET_PAGE({ id: "Data explorer", page: 2 }));
65         expect(state["Data explorer"].page).toEqual(2);
66     });
67     
68     it('should set rows per page', () => {
69         const state = dataExplorerReducer({ "Data explorer": undefined },
70             actions.SET_ROWS_PER_PAGE({ id: "Data explorer", rowsPerPage: 5 }));
71         expect(state["Data explorer"].rowsPerPage).toEqual(5);
72     });
73 });