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