import { createRef } from "react"; import { render, screen } from "@testing-library/react"; import { Table } from "."; const TABLE_CLASS_NAME = "mobius-table"; const TABLE_HEAD_CLASS_NAME = "mobius-table__head"; const TABLE_HEAD_CELL_CLASS_NAME = "mobius-table__head-cell"; const TABLE_BODY_CLASS_NAME = "mobius-table__body"; const TABLE_FOOT_CLASS_NAME = "mobius-table__foot"; const TABLE_ROW_CLASS_NAME = "mobius-table__row"; describe("Test for Table", () => { it("renders an empty table", () => { const view = render(); expect(view).toBeTruthy(); }); describe("uses correct class names", () => { describe("uses the Mobius class name", () => { beforeEach(() => { render(
Browser Cell
, ); }); it.each([ ["Table", "table"], ["Table.Head", "head"], ["Table.Body", "body"], ["Table.Foot", "foot"], ["Table.Row", "row"], ["Table.HeaderCell", "headerCell"], ["Table.Cell", "cell"], ])("for the %s component", (_component, testId) => { expect(screen.getByTestId(testId)).toHaveClass("mobius"); }); }); it("uses table component class names", () => { render( Browser Number of unique visitors Header Header Header Cell Cell Cell Cell Cell
, ); // Table and head expect(screen.getByTestId("test")).toHaveClass(TABLE_CLASS_NAME); const head = screen.getByTestId("test").firstChild; expect(head).toHaveClass(TABLE_HEAD_CLASS_NAME); expect(head?.firstChild).toHaveClass(TABLE_ROW_CLASS_NAME); expect(head?.firstChild?.firstChild).toHaveClass( TABLE_HEAD_CELL_CLASS_NAME, ); // Body const body = screen.getByTestId("test").childNodes[1]; expect(body).toHaveClass(TABLE_BODY_CLASS_NAME); expect(body?.firstChild).toHaveClass(TABLE_ROW_CLASS_NAME); expect(body?.firstChild?.firstChild).toHaveClass( TABLE_HEAD_CELL_CLASS_NAME, ); // Foot const foot = screen.getByTestId("test").childNodes[2]; expect(foot).toHaveClass(TABLE_FOOT_CLASS_NAME); }); it("includes custom class name", () => { render( Browser Number of unique visitors Header Header Header Cell Cell Cell Cell Cell
, ); // Table and head expect(screen.getByTestId("test")).toHaveClass(TABLE_CLASS_NAME); expect(screen.getByTestId("test")).toHaveClass("my-table"); const head = screen.getByTestId("test").firstChild; expect(head).toHaveClass(TABLE_HEAD_CLASS_NAME); expect(head).toHaveClass("my-header"); expect(head?.firstChild).toHaveClass(TABLE_ROW_CLASS_NAME); expect(head?.firstChild).toHaveClass("my-header-row"); expect(head?.firstChild?.firstChild).toHaveClass( TABLE_HEAD_CELL_CLASS_NAME, ); expect(head?.firstChild?.firstChild).toHaveClass("my-header-cell"); // Body const body = screen.getByTestId("test").childNodes[1]; expect(body).toHaveClass(TABLE_BODY_CLASS_NAME); expect(body).toHaveClass("my-body"); expect(body?.firstChild).toHaveClass(TABLE_ROW_CLASS_NAME); expect(body?.firstChild).toHaveClass("my-body-row"); expect(body?.firstChild?.firstChild).toHaveClass( TABLE_HEAD_CELL_CLASS_NAME, ); expect(body?.firstChild?.firstChild).toHaveClass("my-body-cell"); // Foot const foot = screen.getByTestId("test").childNodes[2]; expect(foot).toHaveClass(TABLE_FOOT_CLASS_NAME); expect(foot).toHaveClass("my-footer"); }); }); describe("semantic HTML structure", () => { it("Table renders a element", () => { const { container } = render(
); expect(container.firstChild?.nodeName).toBe("TABLE"); }); it("Head renders a element", () => { render(
Header
, ); expect(screen.getByTestId("head").nodeName).toBe("THEAD"); }); it("Body renders a element", () => { render( Cell
, ); expect(screen.getByTestId("body").nodeName).toBe("TBODY"); }); it("Foot renders a element", () => { render(
, ); expect(screen.getByTestId("foot").nodeName).toBe("TFOOT"); }); it("Row renders a element", () => { render( Cell
, ); expect(screen.getByTestId("row").nodeName).toBe("TR"); }); it("Cell renders a element", () => { render( Cell
, ); expect(screen.getByTestId("cell").nodeName).toBe("TD"); }); it("HeaderCell renders a element", () => { render( Header
, ); expect(screen.getByTestId("header-cell").nodeName).toBe("TH"); }); it("HeaderCell applies scope='col' by default", () => { render( Header
, ); expect(screen.getByTestId("header-cell")).toHaveAttribute("scope", "col"); }); }); describe("forwards ref", () => { it("Table forwards ref", () => { const ref = createRef(); render(); expect(ref.current).toBeInstanceOf(HTMLTableElement); }); it("Table.Head forwards ref", () => { const ref = createRef(); render(
, ); expect(ref.current).toBeInstanceOf(HTMLTableSectionElement); }); it("Table.Body forwards ref", () => { const ref = createRef(); render(
, ); expect(ref.current).toBeInstanceOf(HTMLTableSectionElement); }); it("Table.Foot forwards ref", () => { const ref = createRef(); render(
, ); expect(ref.current).toBeInstanceOf(HTMLTableSectionElement); }); it("Table.Row forwards ref", () => { const ref = createRef(); render(
, ); expect(ref.current).toBeInstanceOf(HTMLTableRowElement); }); it("Table.Cell forwards ref", () => { const ref = createRef(); render( Cell
, ); expect(ref.current).toBeInstanceOf(HTMLTableCellElement); }); it("Table.HeaderCell forwards ref", () => { const ref = createRef(); render( Header
, ); expect(ref.current).toBeInstanceOf(HTMLTableCellElement); }); }); });