1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from "react";
6 import { Typography, Button } from "@mui/material";
7 import { DataTable } from "./data-table";
8 import { SortDirection, createDataColumn } from "./data-column";
9 import { ThemeProvider } from "@mui/material";
10 import { CustomTheme } from "common/custom-theme";
12 describe("<DataTable />", () => {
13 it("shows only selected columns", () => {
17 render: () => <span />,
23 render: () => <span />,
29 render: () => <span />,
35 <ThemeProvider theme={CustomTheme}>
38 items={[{ key: "1", name: "item 1" }]}
39 onFiltersChange={cy.stub()}
40 onRowClick={cy.stub()}
41 onRowDoubleClick={cy.stub()}
42 onContextMenu={cy.stub()}
43 onSortToggle={cy.stub()}
44 setCheckedListOnStore={cy.stub()}
48 cy.get('th').should('have.length', 3);
51 it("renders column name", () => {
55 render: () => <span />,
61 <ThemeProvider theme={CustomTheme}>
65 onFiltersChange={cy.stub()}
66 onRowClick={cy.stub()}
67 onRowDoubleClick={cy.stub()}
68 onContextMenu={cy.stub()}
69 onSortToggle={cy.stub()}
70 setCheckedListOnStore={cy.stub()}
74 cy.get('th').last().contains('Column 1').should('exist');
77 it("uses renderHeader instead of name prop", () => {
81 renderHeader: () => <span>Column Header</span>,
82 render: () => <span />,
88 <ThemeProvider theme={CustomTheme}>
92 onFiltersChange={cy.stub()}
93 onRowClick={cy.stub()}
94 onRowDoubleClick={cy.stub()}
95 onContextMenu={cy.stub()}
96 onSortToggle={cy.stub()}
97 setCheckedListOnStore={cy.stub()}
101 cy.get('th').last().contains('Column Header').should('exist');
104 it("passes column key prop to corresponding cells", () => {
109 render: () => <span />,
115 <ThemeProvider theme={CustomTheme}>
120 onFiltersChange={cy.stub()}
121 onRowClick={cy.stub()}
122 onRowDoubleClick={cy.stub()}
123 onContextMenu={cy.stub()}
124 onSortToggle={cy.stub()}
125 setCheckedListOnStore={cy.stub()}
130 // cannot access key prop directly, so data-cy is assigned to column.key value
131 cy.get('td').last().should('have.attr', 'data-cy', 'column-1-key');
135 it("renders items", () => {
139 render: item => <Typography>{item}</Typography>,
145 render: item => <Button>{item}</Button>,
151 <ThemeProvider theme={CustomTheme}>
156 onFiltersChange={cy.stub()}
157 onRowClick={cy.stub()}
158 onRowDoubleClick={cy.stub()}
159 onContextMenu={cy.stub()}
160 onSortToggle={cy.stub()}
161 setCheckedListOnStore={cy.stub()}
166 cy.get('p').last().contains('item 1').should('exist');
167 cy.get('button').last().contains('item 1').should('exist');
171 it("passes sorting props to <TableSortLabel />", () => {
175 sort: { direction: SortDirection.ASC, field: "length" },
178 render: item => <Typography>{item}</Typography>,
181 const onSortToggle = cy.spy().as("onSortToggle");
183 <ThemeProvider theme={CustomTheme}>
187 onFiltersChange={cy.stub()}
188 onRowClick={cy.stub()}
189 onRowDoubleClick={cy.stub()}
190 onContextMenu={cy.stub()}
191 onSortToggle={onSortToggle}
192 setCheckedListOnStore={cy.stub()}
197 cy.get('th').last().contains('Column 1').should('exist');
198 cy.get('[data-cy="sort-button"]').should('exist').click();
199 cy.get('@onSortToggle').should('have.been.calledWith', columns[1]);
203 it("does not display <DataTableFiltersPopover /> if there is no filters provided", () => {
210 render: item => <Typography>{item}</Typography>,
213 const onFiltersChange = cy.stub();
215 <ThemeProvider theme={CustomTheme}>
219 onFiltersChange={onFiltersChange}
220 onRowClick={cy.stub()}
221 onRowDoubleClick={cy.stub()}
222 onSortToggle={cy.stub()}
223 onContextMenu={cy.stub()}
224 setCheckedListOnStore={cy.stub()}
228 cy.get('[data-cy=data-table]').should('exist');
229 cy.get('[data-cy=popover]').should('not.exist');
232 it("passes filter props to <DataTableFiltersPopover />", () => {
233 const filters = { Filters : {
236 children: ['Filter 1', 'Filter 2'],
242 value: { name: 'Filter'}
250 render: item => <Typography>{item}</Typography>,
253 const onFiltersChange = cy.spy().as("onFiltersChange");
255 <ThemeProvider theme={CustomTheme}>
259 onFiltersChange={onFiltersChange}
260 onRowClick={cy.stub()}
261 onRowDoubleClick={cy.stub()}
262 onSortToggle={cy.stub()}
263 onContextMenu={cy.stub()}
264 setCheckedListOnStore={cy.stub()}
269 cy.get('span[role="button"]').contains('Column 1').should('exist').click();
270 cy.get('[data-cy="tree-li"]').contains('Filter').should('exist').click();
271 cy.get('@onFiltersChange').should('have.been.calledWith', filters, columns[1]);