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 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";
17 configure({ adapter: new Adapter() });
19 describe("<DataExplorer />", () => {
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");
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()}
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");
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()}
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");
69 it("does not render <TablePagination/> if there is no items", () => {
70 const dataExplorer = mount(<DataExplorer
71 {...mockDataExplorerProps()}
74 expect(dataExplorer.find(TablePagination)).toHaveLength(0);
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[]}
85 onChangePage={onChangePage}
86 onChangeRowsPerPage={onChangeRowsPerPage}
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);
97 const mockDataExplorerProps = () => ({
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()