// Copyright (C) The Arvados Authors. All rights reserved.
//
// SPDX-License-Identifier: AGPL-3.0
import React from "react";
import { mount, configure } from "enzyme";
import { pipe } from "lodash/fp";
import { TableHead, TableCell, Typography, TableBody, Button, TableSortLabel } from "@material-ui/core";
import Adapter from "enzyme-adapter-react-16";
import { DataTable, DataColumns } from "./data-table";
import { SortDirection, createDataColumn } from "./data-column";
import { DataTableFiltersPopover } from "components/data-table-filters/data-table-filters-popover";
import { createTree, setNode, initTreeNode } from "models/tree";
import { DataTableFilterItem } from "components/data-table-filters/data-table-filters-tree";
configure({ adapter: new Adapter() });
describe("", () => {
it("shows only selected columns", () => {
const columns: DataColumns = [
createDataColumn({
name: "Column 1",
render: () => ,
selected: true,
configurable: true,
}),
createDataColumn({
name: "Column 2",
render: () => ,
selected: true,
configurable: true,
}),
createDataColumn({
name: "Column 3",
render: () => ,
selected: false,
configurable: true,
}),
];
const dataTable = mount(
);
expect(dataTable.find(TableHead).find(TableCell)).toHaveLength(3);
});
it("renders column name", () => {
const columns: DataColumns = [
createDataColumn({
name: "Column 1",
render: () => ,
selected: true,
configurable: true,
}),
];
const dataTable = mount(
);
expect(dataTable.find(TableHead).find(TableCell).last().text()).toBe("Column 1");
});
it("uses renderHeader instead of name prop", () => {
const columns: DataColumns = [
createDataColumn({
name: "Column 1",
renderHeader: () => Column Header,
render: () => ,
selected: true,
configurable: true,
}),
];
const dataTable = mount(
);
expect(dataTable.find(TableHead).find(TableCell).last().text()).toBe("Column Header");
});
it("passes column key prop to corresponding cells", () => {
const columns: DataColumns = [
createDataColumn({
name: "Column 1",
key: "column-1-key",
render: () => ,
selected: true,
configurable: true,
}),
];
const dataTable = mount(
);
expect(dataTable.find(TableBody).find(TableCell).last().key()).toBe("column-1-key");
});
it("renders items", () => {
const columns: DataColumns = [
createDataColumn({
name: "Column 1",
render: item => {item},
selected: true,
configurable: true,
}),
createDataColumn({
name: "Column 2",
render: item => ,
selected: true,
configurable: true,
}),
];
const dataTable = mount(
);
expect(dataTable.find(TableBody).find(Typography).last().text()).toBe("item 1");
expect(dataTable.find(TableBody).find(Button).last().text()).toBe("item 1");
});
it("passes sorting props to ", () => {
const columns: DataColumns = [
createDataColumn({
name: "Column 1",
sort: { direction: SortDirection.ASC, field: "length" },
selected: true,
configurable: true,
render: item => {item},
}),
];
const onSortToggle = jest.fn();
const dataTable = mount(
);
expect(dataTable.find(TableSortLabel).prop("active")).toBeTruthy();
dataTable.find(TableSortLabel).at(0).simulate("click");
expect(onSortToggle).toHaveBeenCalledWith(columns[1]);
});
it("does not display if there is no filters provided", () => {
const columns: DataColumns = [
{
name: "Column 1",
selected: true,
configurable: true,
filters: [],
render: item => {item},
},
];
const onFiltersChange = jest.fn();
const dataTable = mount(
);
expect(dataTable.find(DataTableFiltersPopover)).toHaveLength(0);
});
it("passes filter props to ", () => {
const filters = pipe(() => createTree(), setNode(initTreeNode({ id: "filter", value: { name: "filter" } })));
const columns: DataColumns = [
{
name: "Column 1",
selected: true,
configurable: true,
filters: filters(),
render: item => {item},
},
];
const onFiltersChange = jest.fn();
const dataTable = mount(
);
expect(dataTable.find(DataTableFiltersPopover).prop("filters")).toBe(columns[1].filters);
dataTable.find(DataTableFiltersPopover).prop("onChange")([]);
expect(onFiltersChange).toHaveBeenCalledWith([], columns[1]);
});
});