// Copyright (C) The Arvados Authors. All rights reserved.
//
// SPDX-License-Identifier: AGPL-3.0
import * as React from "react";
import { mount, configure } from "enzyme";
import { TableHead, TableCell, Typography, TableBody, Button, TableSortLabel } from "@material-ui/core";
import * as Adapter from "enzyme-adapter-react-16";
import { DataTable, DataColumns } from "./data-table";
import { DataTableFilters } from "../data-table-filters/data-table-filters";
import { SortDirection } from "./data-column";
configure({ adapter: new Adapter() });
describe("", () => {
it("shows only selected columns", () => {
const columns: DataColumns = [
{
name: "Column 1",
render: () => ,
selected: true
},
{
name: "Column 2",
render: () => ,
selected: true
},
{
name: "Column 3",
render: () => ,
selected: false
}
];
const dataTable = mount();
expect(dataTable.find(TableHead).find(TableCell)).toHaveLength(2);
});
it("renders column name", () => {
const columns: DataColumns = [
{
name: "Column 1",
render: () => ,
selected: true
}
];
const dataTable = mount();
expect(dataTable.find(TableHead).find(TableCell).text()).toBe("Column 1");
});
it("uses renderHeader instead of name prop", () => {
const columns: DataColumns = [
{
name: "Column 1",
renderHeader: () => Column Header,
render: () => ,
selected: true
}
];
const dataTable = mount();
expect(dataTable.find(TableHead).find(TableCell).text()).toBe("Column Header");
});
it("passes column key prop to corresponding cells", () => {
const columns: DataColumns = [
{
name: "Column 1",
key: "column-1-key",
render: () => ,
selected: true
}
];
const dataTable = mount();
expect(dataTable.find(TableHead).find(TableCell).key()).toBe("column-1-key");
expect(dataTable.find(TableBody).find(TableCell).key()).toBe("column-1-key");
});
it("renders items", () => {
const columns: DataColumns = [
{
name: "Column 1",
render: (item) => {item},
selected: true
},
{
name: "Column 2",
render: (item) => ,
selected: true
}
];
const dataTable = mount();
expect(dataTable.find(TableBody).find(Typography).text()).toBe("item 1");
expect(dataTable.find(TableBody).find(Button).text()).toBe("item 1");
});
it("passes sorting props to ", () => {
const columns: DataColumns = [{
name: "Column 1",
sortDirection: SortDirection.ASC,
selected: 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[0]);
});
it("passes filter props to ", () => {
const columns: DataColumns = [{
name: "Column 1",
sortDirection: SortDirection.ASC,
selected: true,
filters: [{ name: "Filter 1", selected: true }],
render: (item) => {item}
}];
const onFiltersChange = jest.fn();
const dataTable = mount();
expect(dataTable.find(DataTableFilters).prop("filters")).toBe(columns[0].filters);
dataTable.find(DataTableFilters).prop("onChange")([]);
expect(onFiltersChange).toHaveBeenCalledWith([], columns[0]);
});
});