Fix failing tests
[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 import { MockItem } from "../data-table/data-table.test";
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={[{ key: "1", name: "item 1" }] as MockItem[]}
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 }];
36         const dataExplorer = mount(<DataExplorer
37             {...mockDataExplorerProps()}
38             columns={columns}
39             onColumnToggle={onColumnToggle}
40             items={[{ key: "1", name: "item 1" }] as MockItem[]} />);
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 }];
51         const items = [{ key: "1", name: "item 1" }] as MockItem[];
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 <TablePagination/> if there is no items", () => {
70         const dataExplorer = mount(<DataExplorer
71             {...mockDataExplorerProps()}
72             items={[]}
73         />);
74         expect(dataExplorer.find(TablePagination)).toHaveLength(0);
75     });
76
77     it("communicates with <TablePagination/>", () => {
78         const onChangePage = jest.fn();
79         const onChangeRowsPerPage = jest.fn();
80         const dataExplorer = mount(<DataExplorer
81             {...mockDataExplorerProps()}
82             items={[{ key: "1", name: "item 1" }] as MockItem[]}
83             page={10}
84             rowsPerPage={50}
85             onChangePage={onChangePage}
86             onChangeRowsPerPage={onChangeRowsPerPage}
87         />);
88         expect(dataExplorer.find(TablePagination).prop("page")).toEqual(10);
89         expect(dataExplorer.find(TablePagination).prop("rowsPerPage")).toEqual(50);
90         dataExplorer.find(TablePagination).prop("onChangePage")(undefined, 6);
91         dataExplorer.find(TablePagination).prop("onChangeRowsPerPage")({ target: { value: 10 } });
92         expect(onChangePage).toHaveBeenCalledWith(6);
93         expect(onChangeRowsPerPage).toHaveBeenCalledWith(10);
94     });
95 });
96
97 const mockDataExplorerProps = () => ({
98     columns: [],
99     items: [],
100     itemsAvailable: 0,
101     contextActions: [],
102     searchValue: "",
103     page: 0,
104     rowsPerPage: 0,
105     onSearch: jest.fn(),
106     onFiltersChange: jest.fn(),
107     onSortToggle: jest.fn(),
108     onRowClick: jest.fn(),
109     onColumnToggle: jest.fn(),
110     onChangePage: jest.fn(),
111     onChangeRowsPerPage: jest.fn(),
112     onContextMenu: jest.fn()
113 });