import React from "react"; import { render, screen } from "@testing-library/react"; import "@testing-library/jest-dom"; import { ChessPuzzle } from "../.."; import { PuzzleBoard } from "../PuzzleBoard"; import { Puzzle } from "../../../../utils"; import { ChessGame } from "@react-chess-tools/react-chess-game"; describe("ChessPuzzle.PuzzleBoard", () => { const mockPuzzle: Puzzle = { fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", moves: ["e4", "e5"], }; it("should have correct displayName", () => { expect(PuzzleBoard.displayName).toBe("ChessPuzzle.PuzzleBoard"); }); it("should forward ref to underlying Board component", () => { const ref = React.createRef(); render( , ); expect(ref.current).toBeInstanceOf(HTMLDivElement); }); it("should allow focusing via ref", () => { const ref = React.createRef(); render( , ); expect(ref.current).toBeInstanceOf(HTMLDivElement); // Note: focus() doesn't work in JSDOM, but we can verify the ref points to the element // In a real browser, ref.current?.focus() would set document.activeElement to ref.current }); it("should apply custom className", () => { const { container } = render( , ); const board = container.querySelector('[style*="position: relative"]'); expect(board).toHaveClass("custom-puzzle-board-class"); }); it("should apply custom style", () => { const customStyle = { border: "2px solid blue", margin: "15px" }; const { container } = render( , ); const board = container.querySelector('[style*="position: relative"]'); expect(board).toHaveStyle({ border: "2px solid blue" }); expect(board).toHaveStyle({ margin: "15px" }); }); it("should apply custom id", () => { const { container } = render( , ); const board = container.querySelector('[style*="position: relative"]'); expect(board).toHaveAttribute("id", "custom-puzzle-board-id"); }); it("should apply data-* attributes", () => { render( , ); const board = screen.getByTestId("puzzle-board"); expect(board).toHaveAttribute("data-custom", "puzzle-value"); }); it("should apply aria-* attributes", () => { const { container } = render( , ); const board = container.querySelector('[style*="position: relative"]'); expect(board).toHaveAttribute("aria-label", "Puzzle board"); expect(board).toHaveAttribute("aria-describedby", "puzzle-desc"); }); it("should accept custom onClick handler", () => { const handleClick = jest.fn(); const { container } = render( , ); const board = container.querySelector( '[style*="position: relative"]', ) as HTMLElement; board?.click(); expect(handleClick).toHaveBeenCalledTimes(1); }); it("should throw error when used outside ChessPuzzle.Root", () => { // Suppress console.error for this test const consoleError = jest .spyOn(console, "error") .mockImplementation(() => {}); expect(() => { render( , ); }).toThrow( "useChessPuzzleContext must be used within a ChessPuzzle component", ); consoleError.mockRestore(); }); });