|
| 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 | +}); |
0 commit comments