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> = [
23 render: () => <span />,
29 render: () => <span />,
35 render: () => <span />,
40 const dataTable = mount(<DataTable
42 items={[{ key: "1", name: "item 1" }]}
43 onFiltersChange={jest.fn()}
44 onRowClick={jest.fn()}
45 onRowDoubleClick={jest.fn()}
46 onContextMenu={jest.fn()}
47 onSortToggle={jest.fn()} />);
48 expect(dataTable.find(TableHead).find(TableCell)).toHaveLength(2);
51 it("renders column name", () => {
52 const columns: DataColumns<string> = [
55 render: () => <span />,
60 const dataTable = mount(<DataTable
63 onFiltersChange={jest.fn()}
64 onRowClick={jest.fn()}
65 onRowDoubleClick={jest.fn()}
66 onContextMenu={jest.fn()}
67 onSortToggle={jest.fn()} />);
68 expect(dataTable.find(TableHead).find(TableCell).text()).toBe("Column 1");
71 it("uses renderHeader instead of name prop", () => {
72 const columns: DataColumns<string> = [
75 renderHeader: () => <span>Column Header</span>,
76 render: () => <span />,
81 const dataTable = mount(<DataTable
84 onFiltersChange={jest.fn()}
85 onRowClick={jest.fn()}
86 onRowDoubleClick={jest.fn()}
87 onContextMenu={jest.fn()}
88 onSortToggle={jest.fn()} />);
89 expect(dataTable.find(TableHead).find(TableCell).text()).toBe("Column Header");
92 it("passes column key prop to corresponding cells", () => {
93 const columns: DataColumns<string> = [
97 render: () => <span />,
102 const dataTable = mount(<DataTable
105 onFiltersChange={jest.fn()}
106 onRowClick={jest.fn()}
107 onRowDoubleClick={jest.fn()}
108 onContextMenu={jest.fn()}
109 onSortToggle={jest.fn()} />);
110 expect(dataTable.find(TableHead).find(TableCell).key()).toBe("column-1-key");
111 expect(dataTable.find(TableBody).find(TableCell).key()).toBe("column-1-key");
114 it("renders items", () => {
115 const columns: DataColumns<string> = [
118 render: (item) => <Typography>{item}</Typography>,
124 render: (item) => <Button>{item}</Button>,
129 const dataTable = mount(<DataTable
132 onFiltersChange={jest.fn()}
133 onRowClick={jest.fn()}
134 onRowDoubleClick={jest.fn()}
135 onContextMenu={jest.fn()}
136 onSortToggle={jest.fn()} />);
137 expect(dataTable.find(TableBody).find(Typography).text()).toBe("item 1");
138 expect(dataTable.find(TableBody).find(Button).text()).toBe("item 1");
141 it("passes sorting props to <TableSortLabel />", () => {
142 const columns: DataColumns<string> = [
145 sortDirection: SortDirection.ASC,
148 render: (item) => <Typography>{item}</Typography>
150 const onSortToggle = jest.fn();
151 const dataTable = mount(<DataTable
154 onFiltersChange={jest.fn()}
155 onRowClick={jest.fn()}
156 onRowDoubleClick={jest.fn()}
157 onContextMenu={jest.fn()}
158 onSortToggle={onSortToggle} />);
159 expect(dataTable.find(TableSortLabel).prop("active")).toBeTruthy();
160 dataTable.find(TableSortLabel).at(0).simulate("click");
161 expect(onSortToggle).toHaveBeenCalledWith(columns[0]);
164 it("does not display <DataTableFiltersPopover /> if there is no filters provided", () => {
165 const columns: DataColumns<string> = [{
167 sortDirection: SortDirection.ASC,
171 render: (item) => <Typography>{item}</Typography>
173 const onFiltersChange = jest.fn();
174 const dataTable = mount(<DataTable
177 onFiltersChange={onFiltersChange}
178 onRowClick={jest.fn()}
179 onRowDoubleClick={jest.fn()}
180 onSortToggle={jest.fn()}
181 onContextMenu={jest.fn()} />);
182 expect(dataTable.find(DataTableFiltersPopover)).toHaveLength(0);
185 it("passes filter props to <DataTableFiltersPopover />", () => {
186 const filters = pipe(
187 () => createTree<DataTableFilterItem>(),
188 setNode(initTreeNode({ id: 'filter', value: { name: 'filter' } }))
190 const columns: DataColumns<string> = [{
192 sortDirection: SortDirection.ASC,
196 render: (item) => <Typography>{item}</Typography>
198 const onFiltersChange = jest.fn();
199 const dataTable = mount(<DataTable
202 onFiltersChange={onFiltersChange}
203 onRowClick={jest.fn()}
204 onRowDoubleClick={jest.fn()}
205 onSortToggle={jest.fn()}
206 onContextMenu={jest.fn()} />);
207 expect(dataTable.find(DataTableFiltersPopover).prop("filters")).toBe(columns[0].filters);
208 dataTable.find(DataTableFiltersPopover).prop("onChange")([]);
209 expect(onFiltersChange).toHaveBeenCalledWith([], columns[0]);