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, createDataColumn } 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> = [
142 sortDirection: SortDirection.ASC,
145 render: (item) => <Typography>{item}</Typography>
147 const onSortToggle = jest.fn();
148 const dataTable = mount(<DataTable
151 onFiltersChange={jest.fn()}
152 onRowClick={jest.fn()}
153 onRowDoubleClick={jest.fn()}
154 onContextMenu={jest.fn()}
155 onSortToggle={onSortToggle} />);
156 expect(dataTable.find(TableSortLabel).prop("active")).toBeTruthy();
157 dataTable.find(TableSortLabel).at(0).simulate("click");
158 expect(onSortToggle).toHaveBeenCalledWith(columns[0]);
161 it("does not display <DataTableFilter /> if there is no filters provided", () => {
162 const columns: DataColumns<string> = [{
164 sortDirection: SortDirection.ASC,
168 render: (item) => <Typography>{item}</Typography>
170 const onFiltersChange = jest.fn();
171 const dataTable = mount(<DataTable
174 onFiltersChange={onFiltersChange}
175 onRowClick={jest.fn()}
176 onRowDoubleClick={jest.fn()}
177 onSortToggle={jest.fn()}
178 onContextMenu={jest.fn()} />);
179 expect(dataTable.find(DataTableFilters)).toHaveLength(0);
182 it("passes filter props to <DataTableFilter />", () => {
183 const columns: DataColumns<string> = [{
185 sortDirection: SortDirection.ASC,
188 filters: [{ name: "Filter 1", selected: true }],
189 render: (item) => <Typography>{item}</Typography>
191 const onFiltersChange = jest.fn();
192 const dataTable = mount(<DataTable
195 onFiltersChange={onFiltersChange}
196 onRowClick={jest.fn()}
197 onRowDoubleClick={jest.fn()}
198 onSortToggle={jest.fn()}
199 onContextMenu={jest.fn()} />);
200 expect(dataTable.find(DataTableFilters).prop("filters")).toBe(columns[0].filters);
201 dataTable.find(DataTableFilters).prop("onChange")([]);
202 expect(onFiltersChange).toHaveBeenCalledWith([], columns[0]);