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