Merge remote-tracking branch 'origin' into 13694-Data-operations-Project-creation
[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 <ContextMenu/>", () => {
22         const onContextAction = jest.fn();
23         const dataExplorer = mount(<DataExplorer
24             {...mockDataExplorerProps()}
25             items={[{ key: "1", name: "item 1" }] as MockItem[]}
26             columns={[{ name: "Column 1", render: jest.fn(), selected: true }]} />);
27         expect(dataExplorer.find(ContextMenu).prop("actions")).toEqual([]);
28         dataExplorer.find(DataTable).prop("onRowContextMenu")({
29             preventDefault: jest.fn(),
30             stopPropagation: jest.fn()
31         }, "Item 1");
32         dataExplorer.find(ContextMenu).prop("onActionClick")({ name: "Action 1", icon: "" });
33         expect(onContextAction).toHaveBeenCalledWith({ name: "Action 1", icon: "" }, "Item 1");
34     });
35
36     it("communicates with <SearchInput/>", () => {
37         const onSearch = jest.fn();
38         const dataExplorer = mount(<DataExplorer
39             {...mockDataExplorerProps()}
40             items={[{ key: "1", name: "item 1" }] as MockItem[]}
41             searchValue="search value"
42             onSearch={onSearch} />);
43         expect(dataExplorer.find(SearchInput).prop("value")).toEqual("search value");
44         dataExplorer.find(SearchInput).prop("onSearch")("new value");
45         expect(onSearch).toHaveBeenCalledWith("new value");
46     });
47
48     it("communicates with <ColumnSelector/>", () => {
49         const onColumnToggle = jest.fn();
50         const columns = [{ name: "Column 1", render: jest.fn(), selected: true }];
51         const dataExplorer = mount(<DataExplorer
52             {...mockDataExplorerProps()}
53             columns={columns}
54             onColumnToggle={onColumnToggle}
55             items={[{ key: "1", name: "item 1" }] as MockItem[]} />);
56         expect(dataExplorer.find(ColumnSelector).prop("columns")).toBe(columns);
57         dataExplorer.find(ColumnSelector).prop("onColumnToggle")("columns");
58         expect(onColumnToggle).toHaveBeenCalledWith("columns");
59     });
60
61     it("communicates with <DataTable/>", () => {
62         const onFiltersChange = jest.fn();
63         const onSortToggle = jest.fn();
64         const onRowClick = jest.fn();
65         const columns = [{ name: "Column 1", render: jest.fn(), selected: true }];
66         const items = [{ key: "1", name: "item 1" }] as MockItem[];
67         const dataExplorer = mount(<DataExplorer
68             {...mockDataExplorerProps()}
69             columns={columns}
70             items={items}
71             onFiltersChange={onFiltersChange}
72             onSortToggle={onSortToggle}
73             onRowClick={onRowClick} />);
74         expect(dataExplorer.find(DataTable).prop("columns").slice(0, -1)).toEqual(columns);
75         expect(dataExplorer.find(DataTable).prop("items")).toBe(items);
76         dataExplorer.find(DataTable).prop("onRowClick")("event", "rowClick");
77         dataExplorer.find(DataTable).prop("onFiltersChange")("filtersChange");
78         dataExplorer.find(DataTable).prop("onSortToggle")("sortToggle");
79         expect(onFiltersChange).toHaveBeenCalledWith("filtersChange");
80         expect(onSortToggle).toHaveBeenCalledWith("sortToggle");
81         expect(onRowClick).toHaveBeenCalledWith("rowClick");
82     });
83
84     it("does not render <TablePagination/> if there is no items", () => {
85         const dataExplorer = mount(<DataExplorer
86             {...mockDataExplorerProps()}
87             items={[]}
88         />);
89         expect(dataExplorer.find(TablePagination)).toHaveLength(0);
90     });
91
92     it("communicates with <TablePagination/>", () => {
93         const onChangePage = jest.fn();
94         const onChangeRowsPerPage = jest.fn();
95         const dataExplorer = mount(<DataExplorer
96             {...mockDataExplorerProps()}
97             items={[{ key: "1", name: "item 1" }] as MockItem[]}
98             page={10}
99             rowsPerPage={50}
100             onChangePage={onChangePage}
101             onChangeRowsPerPage={onChangeRowsPerPage}
102         />);
103         expect(dataExplorer.find(TablePagination).prop("page")).toEqual(10);
104         expect(dataExplorer.find(TablePagination).prop("rowsPerPage")).toEqual(50);
105         dataExplorer.find(TablePagination).prop("onChangePage")(undefined, 6);
106         dataExplorer.find(TablePagination).prop("onChangeRowsPerPage")({ target: { value: 10 } });
107         expect(onChangePage).toHaveBeenCalledWith(6);
108         expect(onChangeRowsPerPage).toHaveBeenCalledWith(10);
109     });
110 });
111
112 const mockDataExplorerProps = () => ({
113     columns: [],
114     items: [],
115     itemsAvailable: 0,
116     contextActions: [],
117     searchValue: "",
118     page: 0,
119     rowsPerPage: 0,
120     onSearch: jest.fn(),
121     onFiltersChange: jest.fn(),
122     onSortToggle: jest.fn(),
123     onRowClick: jest.fn(),
124     onColumnToggle: jest.fn(),
125     onChangePage: jest.fn(),
126     onChangeRowsPerPage: jest.fn(),
127     onContextMenu: jest.fn()
128 });