table-layout-fix
[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         }, "Item 1");
32         dataExplorer.find(ContextMenu).prop("onActionClick")({ name: "Action 1", icon: "" });
33         expect(onContextAction).toHaveBeenCalledWith({ name: "Action 1", icon: "" }, "Item 1");
34     });
35
36     it("communicates with <SearchInput/>", () => {
37         const onSearch = jest.fn();
38         const dataExplorer = mount(<DataExplorer
39             {...mockDataExplorerProps()}
40             items={["item 1"]}
41             searchValue="search value"
42             onSearch={onSearch} />);
43         expect(dataExplorer.find(SearchInput).prop("value")).toEqual("search value");
44         dataExplorer.find(SearchInput).prop("onSearch")("new value");
45         expect(onSearch).toHaveBeenCalledWith("new value");
46     });
47
48     it("communicates with <ColumnSelector/>", () => {
49         const onColumnToggle = jest.fn();
50         const columns = [{ name: "Column 1", render: jest.fn(), selected: true }];
51         const dataExplorer = mount(<DataExplorer
52             {...mockDataExplorerProps()}
53             columns={columns}
54             onColumnToggle={onColumnToggle}
55             contextActions={[]}
56             items={["Item 1"]} />);
57         expect(dataExplorer.find(ColumnSelector).prop("columns")).toBe(columns);
58         dataExplorer.find(ColumnSelector).prop("onColumnToggle")("columns");
59         expect(onColumnToggle).toHaveBeenCalledWith("columns");
60     });
61
62     it("communicates with <DataTable/>", () => {
63         const onFiltersChange = jest.fn();
64         const onSortToggle = jest.fn();
65         const onRowClick = jest.fn();
66         const columns = [{ name: "Column 1", render: jest.fn(), selected: true }];
67         const items = ["Item 1"];
68         const dataExplorer = mount(<DataExplorer
69             {...mockDataExplorerProps()}
70             columns={columns}
71             items={items}
72             onFiltersChange={onFiltersChange}
73             onSortToggle={onSortToggle}
74             onRowClick={onRowClick} />);
75         expect(dataExplorer.find(DataTable).prop("columns").slice(0, -1)).toEqual(columns);
76         expect(dataExplorer.find(DataTable).prop("items")).toBe(items);
77         dataExplorer.find(DataTable).prop("onRowClick")("event", "rowClick");
78         dataExplorer.find(DataTable).prop("onFiltersChange")("filtersChange");
79         dataExplorer.find(DataTable).prop("onSortToggle")("sortToggle");
80         expect(onFiltersChange).toHaveBeenCalledWith("filtersChange");
81         expect(onSortToggle).toHaveBeenCalledWith("sortToggle");
82         expect(onRowClick).toHaveBeenCalledWith("rowClick");
83     });
84
85     it("does not render <SearchInput/>, <ColumnSelector/> and <TablePagination/> if there is no items", () => {
86         const dataExplorer = mount(<DataExplorer
87             {...mockDataExplorerProps()}
88             items={[]}
89         />);
90         expect(dataExplorer.find(SearchInput)).toHaveLength(0);
91         expect(dataExplorer.find(TablePagination)).toHaveLength(0);
92     });
93
94     it("communicates with <TablePagination/>", () => {
95         const onChangePage = jest.fn();
96         const onChangeRowsPerPage = jest.fn();
97         const dataExplorer = mount(<DataExplorer
98             {...mockDataExplorerProps()}
99             items={["Item 1"]}
100             page={10}
101             rowsPerPage={50}
102             onChangePage={onChangePage}
103             onChangeRowsPerPage={onChangeRowsPerPage}
104         />);
105         expect(dataExplorer.find(TablePagination).prop("page")).toEqual(10);
106         expect(dataExplorer.find(TablePagination).prop("rowsPerPage")).toEqual(50);
107         dataExplorer.find(TablePagination).prop("onChangePage")(undefined, 6);
108         dataExplorer.find(TablePagination).prop("onChangeRowsPerPage")({ target: { value: 10 } });
109         expect(onChangePage).toHaveBeenCalledWith(6);
110         expect(onChangeRowsPerPage).toHaveBeenCalledWith(10);
111     });
112 });
113
114 const mockDataExplorerProps = () => ({
115     columns: [],
116     items: [],
117     contextActions: [],
118     searchValue: "",
119     page: 0,
120     rowsPerPage: 0,
121     onSearch: jest.fn(),
122     onFiltersChange: jest.fn(),
123     onSortToggle: jest.fn(),
124     onRowClick: jest.fn(),
125     onColumnToggle: jest.fn(),
126     onContextAction: jest.fn(),
127     onChangePage: jest.fn(),
128     onChangeRowsPerPage: jest.fn()
129 });