X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/f5e55e7a71f2fc2390d392af752c61b4d3135cb6..b5b6d3dd22b597c9626a396b6449c4f7c9557794:/src/components/data-table/data-table.test.tsx

diff --git a/src/components/data-table/data-table.test.tsx b/src/components/data-table/data-table.test.tsx
index 2ee35072..16957b40 100644
--- a/src/components/data-table/data-table.test.tsx
+++ b/src/components/data-table/data-table.test.tsx
@@ -6,96 +6,103 @@ 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, DataItem } from "./data-table";
-import DataTableFilters from "../data-table-filters/data-table-filters";
-import { SortDirection } from "./data-column";
+import { DataTable, DataColumns } from "./data-table";
+import { DataTableFilters } from "../data-table-filters/data-table-filters";
+import { SortDirection, createDataColumn } from "./data-column";
+import { DataTableDefaultView } from '~/components/data-table-default-view/data-table-default-view';
 
 configure({ adapter: new Adapter() });
 
-export interface MockItem extends DataItem {
-    name: string;
-}
-
 describe("<DataTable />", () => {
     it("shows only selected columns", () => {
-        const columns: DataColumns<MockItem> = [
-            {
+        const columns: DataColumns<string> = [
+            createDataColumn({
                 name: "Column 1",
                 render: () => <span />,
-                selected: true
-            },
-            {
+                selected: true,
+                configurable: true
+            }),
+            createDataColumn({
                 name: "Column 2",
                 render: () => <span />,
-                selected: true
-            },
-            {
+                selected: true,
+                configurable: true
+            }),
+            createDataColumn({
                 name: "Column 3",
                 render: () => <span />,
-                selected: false
-            }
+                selected: false,
+                configurable: true
+            }),
         ];
         const dataTable = mount(<DataTable
             columns={columns}
-            items={[{ key: "1", name: "item 1" }] as MockItem[]}
+            items={[{ key: "1", name: "item 1" }]}
             onFiltersChange={jest.fn()}
             onRowClick={jest.fn()}
+            onRowDoubleClick={jest.fn()}
             onContextMenu={jest.fn()}
             onSortToggle={jest.fn()} />);
         expect(dataTable.find(TableHead).find(TableCell)).toHaveLength(2);
     });
 
     it("renders column name", () => {
-        const columns: DataColumns<MockItem> = [
-            {
+        const columns: DataColumns<string> = [
+            createDataColumn({
                 name: "Column 1",
                 render: () => <span />,
-                selected: true
-            }
+                selected: true,
+                configurable: true
+            }),
         ];
         const dataTable = mount(<DataTable
             columns={columns}
-            items={[{ key: "1", name: "item 1" }] as MockItem[]}
+            items={["item 1"]}
             onFiltersChange={jest.fn()}
             onRowClick={jest.fn()}
+            onRowDoubleClick={jest.fn()}
             onContextMenu={jest.fn()}
             onSortToggle={jest.fn()} />);
         expect(dataTable.find(TableHead).find(TableCell).text()).toBe("Column 1");
     });
 
     it("uses renderHeader instead of name prop", () => {
-        const columns: DataColumns<MockItem> = [
-            {
+        const columns: DataColumns<string> = [
+            createDataColumn({
                 name: "Column 1",
                 renderHeader: () => <span>Column Header</span>,
                 render: () => <span />,
-                selected: true
-            }
+                selected: true,
+                configurable: true
+            }),
         ];
         const dataTable = mount(<DataTable
             columns={columns}
-            items={[{ key: "1", name: "item 1" }] as MockItem[]}
+            items={[]}
             onFiltersChange={jest.fn()}
             onRowClick={jest.fn()}
+            onRowDoubleClick={jest.fn()}
             onContextMenu={jest.fn()}
             onSortToggle={jest.fn()} />);
         expect(dataTable.find(TableHead).find(TableCell).text()).toBe("Column Header");
     });
 
     it("passes column key prop to corresponding cells", () => {
-        const columns: DataColumns<MockItem> = [
-            {
+        const columns: DataColumns<string> = [
+            createDataColumn({
                 name: "Column 1",
                 key: "column-1-key",
                 render: () => <span />,
-                selected: true
-            }
+                selected: true,
+                configurable: true
+            })
         ];
         const dataTable = mount(<DataTable
             columns={columns}
-            items={[{ key: "1", name: "item 1" }] as MockItem[]}
+            items={["item 1"]}
             onFiltersChange={jest.fn()}
             onRowClick={jest.fn()}
+            onRowDoubleClick={jest.fn()}
             onContextMenu={jest.fn()}
             onSortToggle={jest.fn()} />);
         expect(dataTable.find(TableHead).find(TableCell).key()).toBe("column-1-key");
@@ -103,23 +110,26 @@ describe("<DataTable />", () => {
     });
 
     it("renders items", () => {
-        const columns: DataColumns<MockItem> = [
-            {
+        const columns: DataColumns<string> = [
+            createDataColumn({
                 name: "Column 1",
-                render: (item) => <Typography>{item.name}</Typography>,
-                selected: true
-            },
-            {
+                render: (item) => <Typography>{item}</Typography>,
+                selected: true,
+                configurable: true
+            }),
+            createDataColumn({
                 name: "Column 2",
-                render: (item) => <Button>{item.name}</Button>,
-                selected: true
-            }
+                render: (item) => <Button>{item}</Button>,
+                selected: true,
+                configurable: true
+            })
         ];
         const dataTable = mount(<DataTable
             columns={columns}
-            items={[{ key: "1", name: "item 1" }] as MockItem[]}
+            items={["item 1"]}
             onFiltersChange={jest.fn()}
             onRowClick={jest.fn()}
+            onRowDoubleClick={jest.fn()}
             onContextMenu={jest.fn()}
             onSortToggle={jest.fn()} />);
         expect(dataTable.find(TableBody).find(Typography).text()).toBe("item 1");
@@ -127,18 +137,21 @@ describe("<DataTable />", () => {
     });
 
     it("passes sorting props to <TableSortLabel />", () => {
-        const columns: DataColumns<MockItem> = [{
+        const columns: DataColumns<string> = [
+            createDataColumn({
             name: "Column 1",
-            sortDirection: SortDirection.Asc,
+            sortDirection: SortDirection.ASC,
             selected: true,
-            render: (item) => <Typography>{item.name}</Typography>
-        }];
+            configurable: true,
+            render: (item) => <Typography>{item}</Typography>
+        })];
         const onSortToggle = jest.fn();
         const dataTable = mount(<DataTable
             columns={columns}
-            items={[{ key: "1", name: "item 1" }] as MockItem[]}
+            items={["item 1"]}
             onFiltersChange={jest.fn()}
             onRowClick={jest.fn()}
+            onRowDoubleClick={jest.fn()}
             onContextMenu={jest.fn()}
             onSortToggle={onSortToggle} />);
         expect(dataTable.find(TableSortLabel).prop("active")).toBeTruthy();
@@ -146,20 +159,43 @@ describe("<DataTable />", () => {
         expect(onSortToggle).toHaveBeenCalledWith(columns[0]);
     });
 
+    it("does not display <DataTableFilter /> if there is no filters provided", () => {
+        const columns: DataColumns<string> = [{
+            name: "Column 1",
+            sortDirection: SortDirection.ASC,
+            selected: true,
+            configurable: true,
+            filters: [],
+            render: (item) => <Typography>{item}</Typography>
+        }];
+        const onFiltersChange = jest.fn();
+        const dataTable = mount(<DataTable
+            columns={columns}
+            items={[]}
+            onFiltersChange={onFiltersChange}
+            onRowClick={jest.fn()}
+            onRowDoubleClick={jest.fn()}
+            onSortToggle={jest.fn()}
+            onContextMenu={jest.fn()} />);
+        expect(dataTable.find(DataTableFilters)).toHaveLength(0);
+    });
+
     it("passes filter props to <DataTableFilter />", () => {
-        const columns: DataColumns<MockItem> = [{
+        const columns: DataColumns<string> = [{
             name: "Column 1",
-            sortDirection: SortDirection.Asc,
+            sortDirection: SortDirection.ASC,
             selected: true,
+            configurable: true,
             filters: [{ name: "Filter 1", selected: true }],
-            render: (item) => <Typography>{item.name}</Typography>
+            render: (item) => <Typography>{item}</Typography>
         }];
         const onFiltersChange = jest.fn();
         const dataTable = mount(<DataTable
             columns={columns}
-            items={[{ key: "1", name: "item 1" }] as MockItem[]}
+            items={[]}
             onFiltersChange={onFiltersChange}
             onRowClick={jest.fn()}
+            onRowDoubleClick={jest.fn()}
             onSortToggle={jest.fn()}
             onContextMenu={jest.fn()} />);
         expect(dataTable.find(DataTableFilters).prop("filters")).toBe(columns[0].filters);
@@ -167,5 +203,23 @@ describe("<DataTable />", () => {
         expect(onFiltersChange).toHaveBeenCalledWith([], columns[0]);
     });
 
-
-});
\ No newline at end of file
+    it("shows default view if there is no items", () => {
+        const columns: DataColumns<string> = [
+            createDataColumn({
+                name: "Column 1",
+                render: () => <span />,
+                selected: true,
+                configurable: true
+            }),
+        ];
+        const dataTable = mount(<DataTable
+            columns={columns}
+            items={[]}
+            onFiltersChange={jest.fn()}
+            onRowClick={jest.fn()}
+            onRowDoubleClick={jest.fn()}
+            onContextMenu={jest.fn()}
+            onSortToggle={jest.fn()} />);
+        expect(dataTable.find(DataTableDefaultView)).toHaveLength(1);
+    });
+});
