Add search input communication test, fix context menu communication test
[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 { mount, configure } 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
15 configure({ adapter: new Adapter() });
16
17 describe("<DataExplorer />", () => {
18     it("communicates with <ContextMenu/>", () => {
19         const onContextAction = jest.fn();
20         const dataExplorer = mount(<DataExplorer
21             {...mockDataExplorerProps()}
22             contextActions={[]}
23             onContextAction={onContextAction}
24             items={["Item 1"]}
25             columns={[{ name: "Column 1", render: jest.fn(), selected: true }]} />);
26         expect(dataExplorer.find(ContextMenu).prop("actions")).toEqual([]);
27         dataExplorer.find(DataTable).prop("onRowContextMenu")({
28             preventDefault: jest.fn()
29         }, "Item 1");
30         dataExplorer.find(ContextMenu).prop("onActionClick")({ name: "Action 1", icon: "" });
31         expect(onContextAction).toHaveBeenCalledWith({ name: "Action 1", icon: "" }, "Item 1");
32     });
33     
34     it("communicates with <SearchInput/>", () => {
35         const onSearch = jest.fn();
36         const dataExplorer = mount(<DataExplorer
37             {...mockDataExplorerProps()}
38             searchValue="search value"
39             onSearch={onSearch}/>);
40         expect(dataExplorer.find(SearchInput).prop("value")).toEqual("search value");
41         dataExplorer.find(SearchInput).prop("onSearch")("new value");
42         expect(onSearch).toHaveBeenCalledWith("new value");
43     });
44
45     it("communicates with <ColumnSelector/>", () => {
46         const onColumnToggle = jest.fn();
47         const columns = [{ name: "Column 1", render: jest.fn(), selected: true }];
48         const dataExplorer = mount(<DataExplorer
49             {...mockDataExplorerProps()}
50             columns={columns}
51             onColumnToggle={onColumnToggle}
52             contextActions={[]}
53             items={["Item 1"]} />);
54         expect(dataExplorer.find(ColumnSelector).prop("columns")).toBe(columns);
55         dataExplorer.find(ColumnSelector).prop("onColumnToggle")("columns");
56         expect(onColumnToggle).toHaveBeenCalledWith("columns");
57     });
58
59     it("communicates with <DataTable/>", () => {
60         const onFiltersChange = jest.fn();
61         const onSortToggle = jest.fn();
62         const onRowClick = jest.fn();
63         const columns = [{ name: "Column 1", render: jest.fn(), selected: true }];
64         const items = ["Item 1"];
65         const dataExplorer = mount(<DataExplorer
66             {...mockDataExplorerProps()}
67             columns={columns}
68             items={items}
69             onFiltersChange={onFiltersChange}
70             onSortToggle={onSortToggle}
71             onRowClick={onRowClick} />);
72         expect(dataExplorer.find(DataTable).prop("columns")).toBe(columns);
73         expect(dataExplorer.find(DataTable).prop("items")).toBe(items);
74         dataExplorer.find(DataTable).prop("onRowClick")("event", "rowClick");
75         dataExplorer.find(DataTable).prop("onFiltersChange")("filtersChange");
76         dataExplorer.find(DataTable).prop("onSortToggle")("sortToggle");
77         expect(onFiltersChange).toHaveBeenCalledWith("filtersChange");
78         expect(onSortToggle).toHaveBeenCalledWith("sortToggle");
79         expect(onRowClick).toHaveBeenCalledWith("rowClick");
80     });
81 });
82
83 const mockDataExplorerProps = () => ({
84     columns: [],
85     items: [],
86     contextActions: [],
87     searchValue: "",
88     onSearch: jest.fn(),
89     onFiltersChange: jest.fn(),
90     onSortToggle: jest.fn(),
91     onRowClick: jest.fn(),
92     onColumnToggle: jest.fn(),
93     onContextAction: jest.fn()
94 });