1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from "react";
6 import { configure, mount } from "enzyme";
7 import * as Adapter from 'enzyme-adapter-react-16';
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";
15 configure({ adapter: new Adapter() });
17 describe("<DataExplorer />", () => {
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");
31 it("communicates with <ColumnSelector/>", () => {
32 const onColumnToggle = jest.fn();
33 const columns = [{ name: "Column 1", render: jest.fn(), selected: true }];
34 const dataExplorer = mount(<DataExplorer
35 {...mockDataExplorerProps()}
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");
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 }];
49 const items = [{ name: "item 1" }];
50 const dataExplorer = mount(<DataExplorer
51 {...mockDataExplorerProps()}
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");
67 it("does not render <TablePagination/> if there is no items", () => {
68 const dataExplorer = mount(<DataExplorer
69 {...mockDataExplorerProps()}
72 expect(dataExplorer.find(TablePagination)).toHaveLength(0);
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" }]}
83 onChangePage={onChangePage}
84 onChangeRowsPerPage={onChangeRowsPerPage}
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);
95 const mockDataExplorerProps = () => ({
104 onFiltersChange: jest.fn(),
105 onSortToggle: jest.fn(),
106 onRowClick: jest.fn(),
107 onRowDoubleClick: jest.fn(),
108 onColumnToggle: jest.fn(),
109 onChangePage: jest.fn(),
110 onChangeRowsPerPage: jest.fn(),
111 onContextMenu: jest.fn()