Clean up code
[arvados.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 { ColumnSelector } from "../column-selector/column-selector";
11 import { DataTable } from "../data-table/data-table";
12 import { SearchInput } from "../search-input/search-input";
13 import { TablePagination } from "@material-ui/core";
14 import { ProjectIcon } from '../icon/icon';
15 import { DefaultView } from '../default-view/default-view';
16
17 configure({ adapter: new Adapter() });
18
19 describe("<DataExplorer />", () => {
20
21     it("communicates with <SearchInput/>", () => {
22         const onSearch = jest.fn();
23         const dataExplorer = mount(<DataExplorer
24             {...mockDataExplorerProps()}
25             items={[{ name: "item 1" }]}
26             searchValue="search value"
27             onSearch={onSearch} />);
28         expect(dataExplorer.find(SearchInput).prop("value")).toEqual("search value");
29         dataExplorer.find(SearchInput).prop("onSearch")("new value");
30         expect(onSearch).toHaveBeenCalledWith("new value");
31     });
32
33     it("communicates with <ColumnSelector/>", () => {
34         const onColumnToggle = jest.fn();
35         const columns = [{ name: "Column 1", render: jest.fn(), selected: true, configurable: true }];
36         const dataExplorer = mount(<DataExplorer
37             {...mockDataExplorerProps()}
38             columns={columns}
39             onColumnToggle={onColumnToggle}
40             items={[{ name: "item 1" }]} />);
41         expect(dataExplorer.find(ColumnSelector).prop("columns")).toBe(columns);
42         dataExplorer.find(ColumnSelector).prop("onColumnToggle")("columns");
43         expect(onColumnToggle).toHaveBeenCalledWith("columns");
44     });
45
46     it("communicates with <DataTable/>", () => {
47         const onFiltersChange = jest.fn();
48         const onSortToggle = jest.fn();
49         const onRowClick = jest.fn();
50         const columns = [{ name: "Column 1", render: jest.fn(), selected: true, configurable: true }];
51         const items = [{ name: "item 1" }];
52         const dataExplorer = mount(<DataExplorer
53             {...mockDataExplorerProps()}
54             columns={columns}
55             items={items}
56             onFiltersChange={onFiltersChange}
57             onSortToggle={onSortToggle}
58             onRowClick={onRowClick} />);
59         expect(dataExplorer.find(DataTable).prop("columns").slice(0, -1)).toEqual(columns);
60         expect(dataExplorer.find(DataTable).prop("items")).toBe(items);
61         dataExplorer.find(DataTable).prop("onRowClick")("event", "rowClick");
62         dataExplorer.find(DataTable).prop("onFiltersChange")("filtersChange");
63         dataExplorer.find(DataTable).prop("onSortToggle")("sortToggle");
64         expect(onFiltersChange).toHaveBeenCalledWith("filtersChange");
65         expect(onSortToggle).toHaveBeenCalledWith("sortToggle");
66         expect(onRowClick).toHaveBeenCalledWith("rowClick");
67     });
68
69     it("does not render <DataTable/> if there is no items", () => {
70         const dataExplorer = mount(<DataExplorer
71             {...mockDataExplorerProps()}
72             items={[]}
73         />);
74         expect(dataExplorer.find(DataTable)).toHaveLength(0);
75         expect(dataExplorer.find(DefaultView)).toHaveLength(1);
76     });
77
78     it("communicates with <TablePagination/>", () => {
79         const onChangePage = jest.fn();
80         const onChangeRowsPerPage = jest.fn();
81         const dataExplorer = mount(<DataExplorer
82             {...mockDataExplorerProps()}
83             items={[{ name: "item 1" }]}
84             page={10}
85             rowsPerPage={50}
86             onChangePage={onChangePage}
87             onChangeRowsPerPage={onChangeRowsPerPage}
88         />);
89         expect(dataExplorer.find(TablePagination).prop("page")).toEqual(10);
90         expect(dataExplorer.find(TablePagination).prop("rowsPerPage")).toEqual(50);
91         dataExplorer.find(TablePagination).prop("onChangePage")(undefined, 6);
92         dataExplorer.find(TablePagination).prop("onChangeRowsPerPage")({ target: { value: 10 } });
93         expect(onChangePage).toHaveBeenCalledWith(6);
94         expect(onChangeRowsPerPage).toHaveBeenCalledWith(10);
95     });
96 });
97
98 const mockDataExplorerProps = () => ({
99     columns: [],
100     items: [],
101     itemsAvailable: 0,
102     contextActions: [],
103     searchValue: "",
104     page: 0,
105     rowsPerPage: 0,
106     rowsPerPageOptions: [0],
107     onSearch: jest.fn(),
108     onFiltersChange: jest.fn(),
109     onSortToggle: jest.fn(),
110     onRowClick: jest.fn(),
111     onRowDoubleClick: jest.fn(),
112     onColumnToggle: jest.fn(),
113     onChangePage: jest.fn(),
114     onChangeRowsPerPage: jest.fn(),
115     onContextMenu: jest.fn(),
116     defaultIcon: ProjectIcon,
117     defaultMessages: ['testing'],
118 });