1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from "react";
6 import { mount, configure } from "enzyme";
7 import { pipe } from "lodash/fp";
8 import { TableHead, TableCell, Typography, TableBody, Button, TableSortLabel } from "@material-ui/core";
9 import Adapter from "enzyme-adapter-react-16";
10 import { DataTable, DataColumns } from "./data-table";
11 import { SortDirection, createDataColumn } from "./data-column";
12 import { DataTableFiltersPopover } from "components/data-table-filters/data-table-filters-popover";
13 import { createTree, setNode, initTreeNode } from "models/tree";
14 import { DataTableFilterItem } from "components/data-table-filters/data-table-filters-tree";
16 configure({ adapter: new Adapter() });
18 describe("<DataTable />", () => {
19 it("shows only selected columns", () => {
20 const columns: DataColumns<string, string> = [
23 render: () => <span />,
29 render: () => <span />,
35 render: () => <span />,
40 const dataTable = mount(
43 items={[{ key: "1", name: "item 1" }]}
44 onFiltersChange={jest.fn()}
45 onRowClick={jest.fn()}
46 onRowDoubleClick={jest.fn()}
47 onContextMenu={jest.fn()}
48 onSortToggle={jest.fn()}
49 setCheckedListOnStore={jest.fn()}
52 expect(dataTable.find(TableHead).find(TableCell)).toHaveLength(3);
55 it("renders column name", () => {
56 const columns: DataColumns<string, string> = [
59 render: () => <span />,
64 const dataTable = mount(
68 onFiltersChange={jest.fn()}
69 onRowClick={jest.fn()}
70 onRowDoubleClick={jest.fn()}
71 onContextMenu={jest.fn()}
72 onSortToggle={jest.fn()}
73 setCheckedListOnStore={jest.fn()}
76 expect(dataTable.find(TableHead).find(TableCell).last().text()).toBe("Column 1");
79 it("uses renderHeader instead of name prop", () => {
80 const columns: DataColumns<string, string> = [
83 renderHeader: () => <span>Column Header</span>,
84 render: () => <span />,
89 const dataTable = mount(
93 onFiltersChange={jest.fn()}
94 onRowClick={jest.fn()}
95 onRowDoubleClick={jest.fn()}
96 onContextMenu={jest.fn()}
97 onSortToggle={jest.fn()}
98 setCheckedListOnStore={jest.fn()}
101 expect(dataTable.find(TableHead).find(TableCell).last().text()).toBe("Column Header");
104 it("passes column key prop to corresponding cells", () => {
105 const columns: DataColumns<string, string> = [
109 render: () => <span />,
114 const dataTable = mount(
119 onFiltersChange={jest.fn()}
120 onRowClick={jest.fn()}
121 onRowDoubleClick={jest.fn()}
122 onContextMenu={jest.fn()}
123 onSortToggle={jest.fn()}
124 setCheckedListOnStore={jest.fn()}
128 expect(dataTable.find(TableBody).find(TableCell).last().key()).toBe("column-1-key");
132 it("renders items", () => {
133 const columns: DataColumns<string, string> = [
136 render: item => <Typography>{item}</Typography>,
142 render: item => <Button>{item}</Button>,
147 const dataTable = mount(
152 onFiltersChange={jest.fn()}
153 onRowClick={jest.fn()}
154 onRowDoubleClick={jest.fn()}
155 onContextMenu={jest.fn()}
156 onSortToggle={jest.fn()}
157 setCheckedListOnStore={jest.fn()}
161 expect(dataTable.find(TableBody).find(Typography).last().text()).toBe("item 1");
162 expect(dataTable.find(TableBody).find(Button).last().text()).toBe("item 1");
166 it("passes sorting props to <TableSortLabel />", () => {
167 const columns: DataColumns<string, string> = [
170 sort: { direction: SortDirection.ASC, field: "length" },
173 render: item => <Typography>{item}</Typography>,
176 const onSortToggle = jest.fn();
177 const dataTable = mount(
181 onFiltersChange={jest.fn()}
182 onRowClick={jest.fn()}
183 onRowDoubleClick={jest.fn()}
184 onContextMenu={jest.fn()}
185 onSortToggle={onSortToggle}
186 setCheckedListOnStore={jest.fn()}
189 expect(dataTable.find(TableSortLabel).prop("active")).toBeTruthy();
190 dataTable.find(TableSortLabel).at(0).simulate("click");
191 expect(onSortToggle).toHaveBeenCalledWith(columns[1]);
194 it("does not display <DataTableFiltersPopover /> if there is no filters provided", () => {
195 const columns: DataColumns<string, string> = [
201 render: item => <Typography>{item}</Typography>,
204 const onFiltersChange = jest.fn();
205 const dataTable = mount(
209 onFiltersChange={onFiltersChange}
210 onRowClick={jest.fn()}
211 onRowDoubleClick={jest.fn()}
212 onSortToggle={jest.fn()}
213 onContextMenu={jest.fn()}
214 setCheckedListOnStore={jest.fn()}
217 expect(dataTable.find(DataTableFiltersPopover)).toHaveLength(0);
220 it("passes filter props to <DataTableFiltersPopover />", () => {
221 const filters = pipe(() => createTree<DataTableFilterItem>(), setNode(initTreeNode({ id: "filter", value: { name: "filter" } })));
222 const columns: DataColumns<string, string> = [
228 render: item => <Typography>{item}</Typography>,
231 const onFiltersChange = jest.fn();
232 const dataTable = mount(
236 onFiltersChange={onFiltersChange}
237 onRowClick={jest.fn()}
238 onRowDoubleClick={jest.fn()}
239 onSortToggle={jest.fn()}
240 onContextMenu={jest.fn()}
241 setCheckedListOnStore={jest.fn()}
244 expect(dataTable.find(DataTableFiltersPopover).prop("filters")).toBe(columns[1].filters);
245 dataTable.find(DataTableFiltersPopover).prop("onChange")([]);
246 expect(onFiltersChange).toHaveBeenCalledWith([], columns[1]);