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";
12 import { DataTableDefaultView } from '~/components/data-table-default-view/data-table-default-view';
14 configure({ adapter: new Adapter() });
16 describe("<DataTable />", () => {
17 it("shows only selected columns", () => {
18 const columns: DataColumns<string> = [
21 render: () => <span />,
27 render: () => <span />,
33 render: () => <span />,
38 const dataTable = mount(<DataTable
40 items={[{ key: "1", name: "item 1" }]}
41 onFiltersChange={jest.fn()}
42 onRowClick={jest.fn()}
43 onRowDoubleClick={jest.fn()}
44 onContextMenu={jest.fn()}
45 onSortToggle={jest.fn()} />);
46 expect(dataTable.find(TableHead).find(TableCell)).toHaveLength(2);
49 it("renders column name", () => {
50 const columns: DataColumns<string> = [
53 render: () => <span />,
58 const dataTable = mount(<DataTable
61 onFiltersChange={jest.fn()}
62 onRowClick={jest.fn()}
63 onRowDoubleClick={jest.fn()}
64 onContextMenu={jest.fn()}
65 onSortToggle={jest.fn()} />);
66 expect(dataTable.find(TableHead).find(TableCell).text()).toBe("Column 1");
69 it("uses renderHeader instead of name prop", () => {
70 const columns: DataColumns<string> = [
73 renderHeader: () => <span>Column Header</span>,
74 render: () => <span />,
79 const dataTable = mount(<DataTable
82 onFiltersChange={jest.fn()}
83 onRowClick={jest.fn()}
84 onRowDoubleClick={jest.fn()}
85 onContextMenu={jest.fn()}
86 onSortToggle={jest.fn()} />);
87 expect(dataTable.find(TableHead).find(TableCell).text()).toBe("Column Header");
90 it("passes column key prop to corresponding cells", () => {
91 const columns: DataColumns<string> = [
95 render: () => <span />,
100 const dataTable = mount(<DataTable
103 onFiltersChange={jest.fn()}
104 onRowClick={jest.fn()}
105 onRowDoubleClick={jest.fn()}
106 onContextMenu={jest.fn()}
107 onSortToggle={jest.fn()} />);
108 expect(dataTable.find(TableHead).find(TableCell).key()).toBe("column-1-key");
109 expect(dataTable.find(TableBody).find(TableCell).key()).toBe("column-1-key");
112 it("renders items", () => {
113 const columns: DataColumns<string> = [
116 render: (item) => <Typography>{item}</Typography>,
122 render: (item) => <Button>{item}</Button>,
127 const dataTable = mount(<DataTable
130 onFiltersChange={jest.fn()}
131 onRowClick={jest.fn()}
132 onRowDoubleClick={jest.fn()}
133 onContextMenu={jest.fn()}
134 onSortToggle={jest.fn()} />);
135 expect(dataTable.find(TableBody).find(Typography).text()).toBe("item 1");
136 expect(dataTable.find(TableBody).find(Button).text()).toBe("item 1");
139 it("passes sorting props to <TableSortLabel />", () => {
140 const columns: DataColumns<string> = [
143 sortDirection: SortDirection.ASC,
146 render: (item) => <Typography>{item}</Typography>
148 const onSortToggle = jest.fn();
149 const dataTable = mount(<DataTable
152 onFiltersChange={jest.fn()}
153 onRowClick={jest.fn()}
154 onRowDoubleClick={jest.fn()}
155 onContextMenu={jest.fn()}
156 onSortToggle={onSortToggle} />);
157 expect(dataTable.find(TableSortLabel).prop("active")).toBeTruthy();
158 dataTable.find(TableSortLabel).at(0).simulate("click");
159 expect(onSortToggle).toHaveBeenCalledWith(columns[0]);
162 it("does not display <DataTableFilter /> if there is no filters provided", () => {
163 const columns: DataColumns<string> = [{
165 sortDirection: SortDirection.ASC,
169 render: (item) => <Typography>{item}</Typography>
171 const onFiltersChange = jest.fn();
172 const dataTable = mount(<DataTable
175 onFiltersChange={onFiltersChange}
176 onRowClick={jest.fn()}
177 onRowDoubleClick={jest.fn()}
178 onSortToggle={jest.fn()}
179 onContextMenu={jest.fn()} />);
180 expect(dataTable.find(DataTableFilters)).toHaveLength(0);
183 it("passes filter props to <DataTableFilter />", () => {
184 const columns: DataColumns<string> = [{
186 sortDirection: SortDirection.ASC,
189 filters: [{ name: "Filter 1", selected: true }],
190 render: (item) => <Typography>{item}</Typography>
192 const onFiltersChange = jest.fn();
193 const dataTable = mount(<DataTable
196 onFiltersChange={onFiltersChange}
197 onRowClick={jest.fn()}
198 onRowDoubleClick={jest.fn()}
199 onSortToggle={jest.fn()}
200 onContextMenu={jest.fn()} />);
201 expect(dataTable.find(DataTableFilters).prop("filters")).toBe(columns[0].filters);
202 dataTable.find(DataTableFilters).prop("onChange")([]);
203 expect(onFiltersChange).toHaveBeenCalledWith([], columns[0]);
206 it("shows default view if there is no items", () => {
207 const columns: DataColumns<string> = [
210 render: () => <span />,
215 const dataTable = mount(<DataTable
218 onFiltersChange={jest.fn()}
219 onRowClick={jest.fn()}
220 onRowDoubleClick={jest.fn()}
221 onContextMenu={jest.fn()}
222 onSortToggle={jest.fn()} />);
223 expect(dataTable.find(DataTableDefaultView)).toHaveLength(1);