1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from "react";
6 import { mount, configure } from "enzyme";
7 import { TableHead, TableCell, Typography, TableBody, Button, TableSortLabel } from "@material-ui/core";
8 import * as Adapter from "enzyme-adapter-react-16";
9 import { DataTable, DataColumns } from "./data-table";
10 import { DataTableFilters } from "../data-table-filters/data-table-filters";
11 import { SortDirection } from "./data-column";
13 configure({ adapter: new Adapter() });
15 describe("<DataTable />", () => {
16 it("shows only selected columns", () => {
17 const columns: DataColumns<string> = [
20 render: () => <span />,
26 render: () => <span />,
32 render: () => <span />,
37 const dataTable = mount(<DataTable
39 items={[{ key: "1", name: "item 1" }]}
40 onFiltersChange={jest.fn()}
41 onRowClick={jest.fn()}
42 onRowDoubleClick={jest.fn()}
43 onContextMenu={jest.fn()}
44 onSortToggle={jest.fn()} />);
45 expect(dataTable.find(TableHead).find(TableCell)).toHaveLength(2);
48 it("renders column name", () => {
49 const columns: DataColumns<string> = [
52 render: () => <span />,
57 const dataTable = mount(<DataTable
60 onFiltersChange={jest.fn()}
61 onRowClick={jest.fn()}
62 onRowDoubleClick={jest.fn()}
63 onContextMenu={jest.fn()}
64 onSortToggle={jest.fn()} />);
65 expect(dataTable.find(TableHead).find(TableCell).text()).toBe("Column 1");
68 it("uses renderHeader instead of name prop", () => {
69 const columns: DataColumns<string> = [
72 renderHeader: () => <span>Column Header</span>,
73 render: () => <span />,
78 const dataTable = mount(<DataTable
81 onFiltersChange={jest.fn()}
82 onRowClick={jest.fn()}
83 onRowDoubleClick={jest.fn()}
84 onContextMenu={jest.fn()}
85 onSortToggle={jest.fn()} />);
86 expect(dataTable.find(TableHead).find(TableCell).text()).toBe("Column Header");
89 it("passes column key prop to corresponding cells", () => {
90 const columns: DataColumns<string> = [
94 render: () => <span />,
99 const dataTable = mount(<DataTable
102 onFiltersChange={jest.fn()}
103 onRowClick={jest.fn()}
104 onRowDoubleClick={jest.fn()}
105 onContextMenu={jest.fn()}
106 onSortToggle={jest.fn()} />);
107 expect(dataTable.find(TableHead).find(TableCell).key()).toBe("column-1-key");
108 expect(dataTable.find(TableBody).find(TableCell).key()).toBe("column-1-key");
111 it("renders items", () => {
112 const columns: DataColumns<string> = [
115 render: (item) => <Typography>{item}</Typography>,
121 render: (item) => <Button>{item}</Button>,
126 const dataTable = mount(<DataTable
129 onFiltersChange={jest.fn()}
130 onRowClick={jest.fn()}
131 onRowDoubleClick={jest.fn()}
132 onContextMenu={jest.fn()}
133 onSortToggle={jest.fn()} />);
134 expect(dataTable.find(TableBody).find(Typography).text()).toBe("item 1");
135 expect(dataTable.find(TableBody).find(Button).text()).toBe("item 1");
138 it("passes sorting props to <TableSortLabel />", () => {
139 const columns: DataColumns<string> = [{
141 sortDirection: SortDirection.ASC,
144 render: (item) => <Typography>{item}</Typography>
146 const onSortToggle = jest.fn();
147 const dataTable = mount(<DataTable
150 onFiltersChange={jest.fn()}
151 onRowClick={jest.fn()}
152 onRowDoubleClick={jest.fn()}
153 onContextMenu={jest.fn()}
154 onSortToggle={onSortToggle} />);
155 expect(dataTable.find(TableSortLabel).prop("active")).toBeTruthy();
156 dataTable.find(TableSortLabel).at(0).simulate("click");
157 expect(onSortToggle).toHaveBeenCalledWith(columns[0]);
160 it("passes filter props to <DataTableFilter />", () => {
161 const columns: DataColumns<string> = [{
163 sortDirection: SortDirection.ASC,
166 filters: [{ name: "Filter 1", selected: true }],
167 render: (item) => <Typography>{item}</Typography>
169 const onFiltersChange = jest.fn();
170 const dataTable = mount(<DataTable
173 onFiltersChange={onFiltersChange}
174 onRowClick={jest.fn()}
175 onRowDoubleClick={jest.fn()}
176 onSortToggle={jest.fn()}
177 onContextMenu={jest.fn()} />);
178 expect(dataTable.find(DataTableFilters).prop("filters")).toBe(columns[0].filters);
179 dataTable.find(DataTableFilters).prop("onChange")([]);
180 expect(onFiltersChange).toHaveBeenCalledWith([], columns[0]);