Skip to content

Commit 86bee36

Browse files
committed
add tests
1 parent 1725a21 commit 86bee36

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { describe, expect, it } from "vitest";
2+
import { fireEvent, render, screen } from "@testing-library/react";
3+
import "@testing-library/jest-dom";
4+
import { Table } from "@/plugins/mui/Table";
5+
import { createChangeHandler } from "@/plugins/mui/common.test";
6+
7+
describe("Table", () => {
8+
const rows = [
9+
{ id: 1, data: { firstName: "John", lastName: "Doe" } },
10+
{ id: 2, data: { firstName: "Johnie", lastName: "Undoe" } },
11+
];
12+
const columns = [
13+
{ id: "firstName", label: "First Name" },
14+
{ id: "lastName", label: "Last Name" },
15+
];
16+
17+
it("should render the Table component", () => {
18+
render(
19+
<Table
20+
id="table"
21+
type={"Table"}
22+
rows={rows}
23+
columns={columns}
24+
onChange={() => {}}
25+
/>,
26+
);
27+
28+
const table = screen.getByRole("table");
29+
expect(table).toBeDefined();
30+
columns.forEach((column) => {
31+
expect(screen.getByText(column.label)).toBeInTheDocument();
32+
});
33+
rows.forEach((row) => {
34+
expect(screen.getByText(row.data.firstName)).toBeInTheDocument();
35+
expect(screen.getByText(row.data.lastName)).toBeInTheDocument();
36+
});
37+
});
38+
39+
it("should not render the Table component when no columns provided", () => {
40+
render(<Table id="table" type={"Table"} rows={rows} onChange={() => {}} />);
41+
42+
const table = screen.queryByRole("table");
43+
expect(table).toBeNull();
44+
});
45+
46+
it("should not render the Table component when no rows provided", () => {
47+
render(<Table id="table" type={"Table"} rows={rows} onChange={() => {}} />);
48+
49+
const table = screen.queryByRole("table");
50+
expect(table).toBeNull();
51+
});
52+
53+
it("should call onChange on row click", () => {
54+
const { recordedEvents, onChange } = createChangeHandler();
55+
render(
56+
<Table
57+
id="table"
58+
type={"Table"}
59+
rows={rows}
60+
columns={columns}
61+
onChange={onChange}
62+
/>,
63+
);
64+
65+
fireEvent.click(screen.getAllByRole("row")[1]);
66+
expect(recordedEvents.length).toEqual(1);
67+
expect(recordedEvents[0]).toEqual({
68+
componentType: "Table",
69+
id: "table",
70+
property: "value",
71+
value: {
72+
id: 1,
73+
data: {
74+
firstName: "John",
75+
lastName: "Doe",
76+
},
77+
},
78+
});
79+
});
80+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from chartlets.components.table import TableColumn, TableRowData, Table
2+
from tests.component_test import make_base
3+
4+
5+
class TableTest(make_base(Table)):
6+
7+
def test_is_json_serializable_empty(self):
8+
self.assert_is_json_serializable(
9+
self.cls(),
10+
{
11+
"type": "Table",
12+
},
13+
)
14+
15+
columns: list[TableColumn] = [
16+
{"id": "id", "label": "ID"},
17+
{"id": "firstName", "label": "First Name"},
18+
{"id": "lastName", "label": "Last Name"},
19+
{"id": "age", "label": "Age"},
20+
]
21+
rows: list[TableRowData] = [
22+
{
23+
"id": "1",
24+
"data": {"firstName": "John", "lastName": "Doe", "age": 30},
25+
},
26+
{"id": "2", "data": {"firstName": "Jane", "lastName": "Smith", "age": 25}},
27+
{"id": 3, "data": {"firstName": "Johnie", "lastName": "Undoe", "age": 40}},
28+
]
29+
hover: bool = True
30+
style = {"background-color": "lightgray", "width": "100%"}
31+
32+
self.assert_is_json_serializable(
33+
self.cls(columns=columns, rows=rows, style=style, hover=hover),
34+
{
35+
"type": "Table",
36+
"columns": columns,
37+
"rows": rows,
38+
"style": style,
39+
"hover": hover,
40+
},
41+
)

0 commit comments

Comments
 (0)