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";
14 import { ProjectIcon } from '../icon/icon';
15 import { DefaultView } from '../default-view/default-view';
16 import { SortDirection } from '../data-table/data-column';
18 configure({ adapter: new Adapter() });
20 describe("<DataExplorer />", () => {
22 it("communicates with <SearchInput/>", () => {
23 const onSearch = jest.fn();
24 const onSetColumns = jest.fn();
25 const dataExplorer = mount(<DataExplorer
26 {...mockDataExplorerProps()}
27 items={[{ name: "item 1" }]}
28 searchValue="search value"
30 onSetColumns={onSetColumns} />);
31 expect(dataExplorer.find(SearchInput).prop("value")).toEqual("search value");
32 dataExplorer.find(SearchInput).prop("onSearch")("new value");
33 expect(onSearch).toHaveBeenCalledWith("new value");
36 it("communicates with <ColumnSelector/>", () => {
37 const onColumnToggle = jest.fn();
38 const onSetColumns = jest.fn();
39 const columns = [{ name: "Column 1", render: jest.fn(), selected: true, configurable: true, sortDirection: SortDirection.ASC, filters: [] }];
40 const dataExplorer = mount(<DataExplorer
41 {...mockDataExplorerProps()}
43 onColumnToggle={onColumnToggle}
44 items={[{ name: "item 1" }]}
45 onSetColumns={onSetColumns} />);
46 expect(dataExplorer.find(ColumnSelector).prop("columns")).toBe(columns);
47 dataExplorer.find(ColumnSelector).prop("onColumnToggle")("columns");
48 expect(onColumnToggle).toHaveBeenCalledWith("columns");
51 it("communicates with <DataTable/>", () => {
52 const onFiltersChange = jest.fn();
53 const onSortToggle = jest.fn();
54 const onRowClick = jest.fn();
55 const onSetColumns = jest.fn();
56 const columns = [{ name: "Column 1", render: jest.fn(), selected: true, configurable: true, sortDirection: SortDirection.ASC, filters: [] }];
57 const items = [{ name: "item 1" }];
58 const dataExplorer = mount(<DataExplorer
59 {...mockDataExplorerProps()}
62 onFiltersChange={onFiltersChange}
63 onSortToggle={onSortToggle}
64 onRowClick={onRowClick}
65 onSetColumns={onSetColumns} />);
66 expect(dataExplorer.find(DataTable).prop("columns").slice(0, -1)).toEqual(columns);
67 expect(dataExplorer.find(DataTable).prop("items")).toBe(items);
68 dataExplorer.find(DataTable).prop("onRowClick")("event", "rowClick");
69 dataExplorer.find(DataTable).prop("onFiltersChange")("filtersChange");
70 dataExplorer.find(DataTable).prop("onSortToggle")("sortToggle");
71 expect(onFiltersChange).toHaveBeenCalledWith("filtersChange");
72 expect(onSortToggle).toHaveBeenCalledWith("sortToggle");
73 expect(onRowClick).toHaveBeenCalledWith("rowClick");
76 it("communicates with <TablePagination/>", () => {
77 const onChangePage = jest.fn();
78 const onChangeRowsPerPage = jest.fn();
79 const onSetColumns = jest.fn();
80 const dataExplorer = mount(<DataExplorer
81 {...mockDataExplorerProps()}
82 items={[{ name: "item 1" }]}
85 onChangePage={onChangePage}
86 onChangeRowsPerPage={onChangeRowsPerPage}
87 onSetColumns={onSetColumns} />);
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 = () => ({
105 rowsPerPageOptions: [0],
107 onFiltersChange: jest.fn(),
108 onSortToggle: jest.fn(),
109 onRowClick: jest.fn(),
110 onRowDoubleClick: jest.fn(),
111 onColumnToggle: jest.fn(),
112 onChangePage: jest.fn(),
113 onChangeRowsPerPage: jest.fn(),
114 onContextMenu: jest.fn(),
115 defaultIcon: ProjectIcon,
116 onSetColumns: jest.fn(),
117 defaultMessages: ['testing'],
118 contextMenuColumn: true