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";
12 configure({ adapter: new Adapter() });
14 describe("<DataTable />", () => {
15 it("shows only selected columns", () => {
16 const columns: DataColumns<string> = [
19 render: () => <span />,
24 render: () => <span />,
29 render: () => <span />,
33 const dataTable = mount(<DataTable
36 onFiltersChange={jest.fn()}
37 onRowClick={jest.fn()}
38 onRowContextMenu={jest.fn()}
39 onSortToggle={jest.fn()} />);
40 expect(dataTable.find(TableHead).find(TableCell)).toHaveLength(2);
43 it("renders column name", () => {
44 const columns: DataColumns<string> = [
47 render: () => <span />,
51 const dataTable = mount(<DataTable
54 onFiltersChange={jest.fn()}
55 onRowClick={jest.fn()}
56 onRowContextMenu={jest.fn()}
57 onSortToggle={jest.fn()} />);
58 expect(dataTable.find(TableHead).find(TableCell).text()).toBe("Column 1");
61 it("uses renderHeader instead of name prop", () => {
62 const columns: DataColumns<string> = [
65 renderHeader: () => <span>Column Header</span>,
66 render: () => <span />,
70 const dataTable = mount(<DataTable
73 onFiltersChange={jest.fn()}
74 onRowClick={jest.fn()}
75 onRowContextMenu={jest.fn()}
76 onSortToggle={jest.fn()} />);
77 expect(dataTable.find(TableHead).find(TableCell).text()).toBe("Column Header");
80 it("passes column key prop to corresponding cells", () => {
81 const columns: DataColumns<string> = [
85 render: () => <span />,
89 const dataTable = mount(<DataTable
92 onFiltersChange={jest.fn()}
93 onRowClick={jest.fn()}
94 onRowContextMenu={jest.fn()}
95 onSortToggle={jest.fn()} />);
96 expect(dataTable.find(TableHead).find(TableCell).key()).toBe("column-1-key");
97 expect(dataTable.find(TableBody).find(TableCell).key()).toBe("column-1-key");
100 it("renders items", () => {
101 const columns: DataColumns<string> = [
104 render: (item) => <Typography>{item}</Typography>,
109 render: (item) => <Button>{item}</Button>,
113 const dataTable = mount(<DataTable
116 onFiltersChange={jest.fn()}
117 onRowClick={jest.fn()}
118 onRowContextMenu={jest.fn()}
119 onSortToggle={jest.fn()} />);
120 expect(dataTable.find(TableBody).find(Typography).text()).toBe("item 1");
121 expect(dataTable.find(TableBody).find(Button).text()).toBe("item 1");
124 it("passes sorting props to <TableSortLabel />", () => {
125 const columns: DataColumns<string> = [{
127 sortDirection: "asc",
129 render: (item) => <Typography>{item}</Typography>
131 const onSortToggle = jest.fn();
132 const dataTable = mount(<DataTable
135 onFiltersChange={jest.fn()}
136 onRowClick={jest.fn()}
137 onRowContextMenu={jest.fn()}
138 onSortToggle={onSortToggle}/>);
139 expect(dataTable.find(TableSortLabel).prop("active")).toBeTruthy();
140 dataTable.find(TableSortLabel).at(0).simulate("click");
141 expect(onSortToggle).toHaveBeenCalledWith(columns[0]);
144 it("passes filter props to <DataTableFilter />", () => {
145 const columns: DataColumns<string> = [{
147 sortDirection: "asc",
149 filters: [{name: "Filter 1", selected: true}],
150 render: (item) => <Typography>{item}</Typography>
152 const onFiltersChange = jest.fn();
153 const dataTable = mount(<DataTable
156 onFiltersChange={onFiltersChange}
157 onRowClick={jest.fn()}
158 onRowContextMenu={jest.fn()}
159 onSortToggle={jest.fn()}/>);
160 expect(dataTable.find(DataTableFilters).prop("filters")).toBe(columns[0].filters);
161 dataTable.find(DataTableFilters).prop("onChange")([]);
162 expect(onFiltersChange).toHaveBeenCalledWith([], columns[0]);