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()}
127 expect(dataTable.find(TableBody).find(TableCell).last().key()).toBe("column-1-key");
130 it("renders items", () => {
131 const columns: DataColumns<string, string> = [
134 render: item => <Typography>{item}</Typography>,
140 render: item => <Button>{item}</Button>,
145 const dataTable = mount(
150 onFiltersChange={jest.fn()}
151 onRowClick={jest.fn()}
152 onRowDoubleClick={jest.fn()}
153 onContextMenu={jest.fn()}
154 onSortToggle={jest.fn()}
155 setCheckedListOnStore={jest.fn()}
158 expect(dataTable.find(TableBody).find(Typography).last().text()).toBe("item 1");
159 expect(dataTable.find(TableBody).find(Button).last().text()).toBe("item 1");
162 it("passes sorting props to <TableSortLabel />", () => {
163 const columns: DataColumns<string, string> = [
166 sort: { direction: SortDirection.ASC, field: "length" },
169 render: item => <Typography>{item}</Typography>,
172 const onSortToggle = jest.fn();
173 const dataTable = mount(
177 onFiltersChange={jest.fn()}
178 onRowClick={jest.fn()}
179 onRowDoubleClick={jest.fn()}
180 onContextMenu={jest.fn()}
181 onSortToggle={onSortToggle}
182 setCheckedListOnStore={jest.fn()}
185 expect(dataTable.find(TableSortLabel).prop("active")).toBeTruthy();
186 dataTable.find(TableSortLabel).at(0).simulate("click");
187 expect(onSortToggle).toHaveBeenCalledWith(columns[1]);
190 it("does not display <DataTableFiltersPopover /> if there is no filters provided", () => {
191 const columns: DataColumns<string, string> = [
197 render: item => <Typography>{item}</Typography>,
200 const onFiltersChange = jest.fn();
201 const dataTable = mount(
205 onFiltersChange={onFiltersChange}
206 onRowClick={jest.fn()}
207 onRowDoubleClick={jest.fn()}
208 onSortToggle={jest.fn()}
209 onContextMenu={jest.fn()}
210 setCheckedListOnStore={jest.fn()}
213 expect(dataTable.find(DataTableFiltersPopover)).toHaveLength(0);
216 it("passes filter props to <DataTableFiltersPopover />", () => {
217 const filters = pipe(() => createTree<DataTableFilterItem>(), setNode(initTreeNode({ id: "filter", value: { name: "filter" } })));
218 const columns: DataColumns<string, string> = [
224 render: item => <Typography>{item}</Typography>,
227 const onFiltersChange = jest.fn();
228 const dataTable = mount(
232 onFiltersChange={onFiltersChange}
233 onRowClick={jest.fn()}
234 onRowDoubleClick={jest.fn()}
235 onSortToggle={jest.fn()}
236 onContextMenu={jest.fn()}
237 setCheckedListOnStore={jest.fn()}
240 expect(dataTable.find(DataTableFiltersPopover).prop("filters")).toBe(columns[1].filters);
241 dataTable.find(DataTableFiltersPopover).prop("onChange")([]);
242 expect(onFiltersChange).toHaveBeenCalledWith([], columns[1]);