Simplify finding sorting columns and filters
[arvados-workbench2.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 { dataExplorerActions } from "./data-explorer-action";
7 import { DataTableFilterItem } from "../../components/data-table-filters/data-table-filters";
8 import { DataColumns } from "../../components/data-table/data-table";
9 import { SortDirection } from "../../components/data-table/data-column";
10
11 describe('data-explorer-reducer', () => {
12     it('should set columns', () => {
13         const columns: DataColumns<any> = [{
14             name: "Column 1",
15             render: jest.fn(),
16             selected: true,
17             configurable: true
18         }];
19         const state = dataExplorerReducer(undefined,
20             dataExplorerActions.SET_COLUMNS({ id: "Data explorer", columns }));
21         expect(state["Data explorer"].columns).toEqual(columns);
22     });
23
24     it('should toggle sorting', () => {
25         const columns: DataColumns<any> = [{
26             name: "Column 1",
27             render: jest.fn(),
28             selected: true,
29             configurable: true,
30             sortDirection: SortDirection.ASC
31         }, {
32             name: "Column 2",
33             render: jest.fn(),
34             selected: true,
35             configurable: true,
36             sortDirection: SortDirection.NONE,
37         }];
38         const state = dataExplorerReducer({ "Data explorer": { ...initialDataExplorer, columns } },
39             dataExplorerActions.TOGGLE_SORT({ id: "Data explorer", columnName: "Column 2" }));
40         expect(state["Data explorer"].columns[0].sortDirection).toEqual("none");
41         expect(state["Data explorer"].columns[1].sortDirection).toEqual("asc");
42     });
43
44     it('should set filters', () => {
45         const columns: DataColumns<any> = [{
46             name: "Column 1",
47             render: jest.fn(),
48             selected: true,
49             configurable: true
50         }];
51
52         const filters: DataTableFilterItem[] = [{
53             name: "Filter 1",
54             selected: true
55         }];
56         const state = dataExplorerReducer({ "Data explorer": { ...initialDataExplorer, columns } },
57             dataExplorerActions.SET_FILTERS({ id: "Data explorer", columnName: "Column 1", filters }));
58         expect(state["Data explorer"].columns[0].filters).toEqual(filters);
59     });
60
61     it('should set items', () => {
62         const state = dataExplorerReducer({},
63             dataExplorerActions.SET_ITEMS({
64                 id: "Data explorer",
65                 items: ["Item 1", "Item 2"],
66                 page: 0,
67                 rowsPerPage: 10,
68                 itemsAvailable: 100
69             }));
70         expect(state["Data explorer"].items).toEqual(["Item 1", "Item 2"]);
71     });
72
73     it('should set page', () => {
74         const state = dataExplorerReducer({},
75             dataExplorerActions.SET_PAGE({ id: "Data explorer", page: 2 }));
76         expect(state["Data explorer"].page).toEqual(2);
77     });
78
79     it('should set rows per page', () => {
80         const state = dataExplorerReducer({},
81             dataExplorerActions.SET_ROWS_PER_PAGE({ id: "Data explorer", rowsPerPage: 5 }));
82         expect(state["Data explorer"].rowsPerPage).toEqual(5);
83     });
84 });