94c7be6dab933ff6991edda0a56ebf4b1edc0d2d
[arvados-workbench2.git] / src / components / data-explorer / data-explorer.test.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from "react";
6 import { configure, mount } from "enzyme";
7 import * as Adapter from 'enzyme-adapter-react-16';
8
9 import DataExplorer from "./data-explorer";
10 import ContextMenu from "../context-menu/context-menu";
11 import ColumnSelector from "../column-selector/column-selector";
12 import DataTable from "../data-table/data-table";
13 import SearchInput from "../search-input/search-input";
14 import { TablePagination } from "@material-ui/core";
15
16 configure({ adapter: new Adapter() });
17
18 describe("<DataExplorer />", () => {
19
20     it("communicates with <ContextMenu/>", () => {
21         const onContextAction = jest.fn();
22         const dataExplorer = mount(<DataExplorer
23             {...mockDataExplorerProps()}
24             contextActions={[]}
25             onContextAction={onContextAction}
26             items={["Item 1"]}
27             columns={[{ name: "Column 1", render: jest.fn(), selected: true }]} />);
28         expect(dataExplorer.find(ContextMenu).prop("actions")).toEqual([]);
29         dataExplorer.find(DataTable).prop("onRowContextMenu")({
30             preventDefault: jest.fn(),
31             stopPropagation: jest.fn()
32         }, "Item 1");
33         dataExplorer.find(ContextMenu).prop("onActionClick")({ name: "Action 1", icon: "" });
34         expect(onContextAction).toHaveBeenCalledWith({ name: "Action 1", icon: "" }, "Item 1");
35     });
36
37     it("communicates with <SearchInput/>", () => {
38         const onSearch = jest.fn();
39         const dataExplorer = mount(<DataExplorer
40             {...mockDataExplorerProps()}
41             items={["item 1"]}
42             searchValue="search value"
43             onSearch={onSearch} />);
44         expect(dataExplorer.find(SearchInput).prop("value")).toEqual("search value");
45         dataExplorer.find(SearchInput).prop("onSearch")("new value");
46         expect(onSearch).toHaveBeenCalledWith("new value");
47     });
48
49     it("communicates with <ColumnSelector/>", () => {
50         const onColumnToggle = jest.fn();
51         const columns = [{ name: "Column 1", render: jest.fn(), selected: true }];
52         const dataExplorer = mount(<DataExplorer
53             {...mockDataExplorerProps()}
54             columns={columns}
55             onColumnToggle={onColumnToggle}
56             contextActions={[]}
57             items={["Item 1"]} />);
58         expect(dataExplorer.find(ColumnSelector).prop("columns")).toBe(columns);
59         dataExplorer.find(ColumnSelector).prop("onColumnToggle")("columns");
60         expect(onColumnToggle).toHaveBeenCalledWith("columns");
61     });
62
63     it("communicates with <DataTable/>", () => {
64         const onFiltersChange = jest.fn();
65         const onSortToggle = jest.fn();
66         const onRowClick = jest.fn();
67         const columns = [{ name: "Column 1", render: jest.fn(), selected: true }];
68         const items = ["Item 1"];
69         const dataExplorer = mount(<DataExplorer
70             {...mockDataExplorerProps()}
71             columns={columns}
72             items={items}
73             onFiltersChange={onFiltersChange}
74             onSortToggle={onSortToggle}
75             onRowClick={onRowClick} />);
76         expect(dataExplorer.find(DataTable).prop("columns").slice(0, -1)).toEqual(columns);
77         expect(dataExplorer.find(DataTable).prop("items")).toBe(items);
78         dataExplorer.find(DataTable).prop("onRowClick")("event", "rowClick");
79         dataExplorer.find(DataTable).prop("onFiltersChange")("filtersChange");
80         dataExplorer.find(DataTable).prop("onSortToggle")("sortToggle");
81         expect(onFiltersChange).toHaveBeenCalledWith("filtersChange");
82         expect(onSortToggle).toHaveBeenCalledWith("sortToggle");
83         expect(onRowClick).toHaveBeenCalledWith("rowClick");
84     });
85
86     it("does not render <SearchInput/>, <ColumnSelector/> and <TablePagination/> if there is no items", () => {
87         const dataExplorer = mount(<DataExplorer
88             {...mockDataExplorerProps()}
89             items={[]}
90         />);
91         expect(dataExplorer.find(SearchInput)).toHaveLength(0);
92         expect(dataExplorer.find(TablePagination)).toHaveLength(0);
93     });
94
95     it("communicates with <TablePagination/>", () => {
96         const onChangePage = jest.fn();
97         const onChangeRowsPerPage = jest.fn();
98         const dataExplorer = mount(<DataExplorer
99             {...mockDataExplorerProps()}
100             items={["Item 1"]}
101             page={10}
102             rowsPerPage={50}
103             onChangePage={onChangePage}
104             onChangeRowsPerPage={onChangeRowsPerPage}
105         />);
106         expect(dataExplorer.find(TablePagination).prop("page")).toEqual(10);
107         expect(dataExplorer.find(TablePagination).prop("rowsPerPage")).toEqual(50);
108         dataExplorer.find(TablePagination).prop("onChangePage")(undefined, 6);
109         dataExplorer.find(TablePagination).prop("onChangeRowsPerPage")({ target: { value: 10 } });
110         expect(onChangePage).toHaveBeenCalledWith(6);
111         expect(onChangeRowsPerPage).toHaveBeenCalledWith(10);
112     });
113 });
114
115 const mockDataExplorerProps = () => ({
116     columns: [],
117     items: [],
118     contextActions: [],
119     searchValue: "",
120     page: 0,
121     rowsPerPage: 0,
122     onSearch: jest.fn(),
123     onFiltersChange: jest.fn(),
124     onSortToggle: jest.fn(),
125     onRowClick: jest.fn(),
126     onColumnToggle: jest.fn(),
127     onContextAction: jest.fn(),
128     onChangePage: jest.fn(),
129     onChangeRowsPerPage: jest.fn()
130 });