@react-chess-tools/react-chess-puzzle 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,51 +1,71 @@
1
1
  import React from "react";
2
- import { isClickableElement, type Puzzle, type Status } from "../../../utils";
2
+ import { Slot } from "@radix-ui/react-slot";
3
+ import { type Puzzle, type Status } from "../../../utils";
3
4
  import { useChessPuzzleContext, type ChessPuzzleContextType } from "../../..";
4
5
 
5
- export interface ResetProps {
6
+ export interface ResetProps extends Omit<
7
+ React.ButtonHTMLAttributes<HTMLButtonElement>,
8
+ "onReset"
9
+ > {
6
10
  asChild?: boolean;
7
11
  puzzle?: Puzzle;
8
12
  onReset?: (puzzleContext: ChessPuzzleContextType) => void;
13
+ /**
14
+ * The puzzle statuses in which the reset button should be visible.
15
+ * @default ["failed", "solved"]
16
+ */
9
17
  showOn?: Status[];
10
18
  }
11
19
 
12
20
  const defaultShowOn: Status[] = ["failed", "solved"];
13
21
 
14
- export const Reset: React.FC<React.PropsWithChildren<ResetProps>> = ({
15
- children,
16
- asChild,
17
- puzzle,
18
- onReset,
19
- showOn = defaultShowOn,
20
- }) => {
21
- const puzzleContext = useChessPuzzleContext();
22
- if (!puzzleContext) {
23
- throw new Error("PuzzleContext not found");
24
- }
25
- const { changePuzzle, status } = puzzleContext;
26
- const handleClick = () => {
27
- changePuzzle(puzzle || puzzleContext.puzzle);
28
- onReset?.(puzzleContext);
29
- };
22
+ export const Reset = React.forwardRef<
23
+ HTMLElement,
24
+ React.PropsWithChildren<ResetProps>
25
+ >(
26
+ (
27
+ {
28
+ children,
29
+ asChild,
30
+ puzzle,
31
+ onReset,
32
+ showOn = defaultShowOn,
33
+ className,
34
+ ...rest
35
+ },
36
+ ref,
37
+ ) => {
38
+ const puzzleContext = useChessPuzzleContext();
39
+ if (!puzzleContext) {
40
+ throw new Error("PuzzleContext not found");
41
+ }
42
+ const { changePuzzle, puzzle: contextPuzzle, status } = puzzleContext;
30
43
 
31
- if (!showOn.includes(status)) {
32
- return null;
33
- }
44
+ const handleClick = React.useCallback(() => {
45
+ changePuzzle(puzzle || contextPuzzle);
46
+ onReset?.(puzzleContext);
47
+ }, [changePuzzle, puzzle, contextPuzzle, puzzleContext, onReset]);
34
48
 
35
- if (asChild) {
36
- const child = React.Children.only(children);
37
- if (isClickableElement(child)) {
38
- return React.cloneElement(child, {
39
- onClick: handleClick,
40
- });
41
- } else {
42
- throw new Error("Change child must be a clickable element");
49
+ if (!showOn.includes(status)) {
50
+ return null;
43
51
  }
44
- }
45
52
 
46
- return (
47
- <button type="button" onClick={handleClick}>
48
- {children}
49
- </button>
50
- );
51
- };
53
+ return asChild ? (
54
+ <Slot ref={ref} onClick={handleClick} className={className} {...rest}>
55
+ {children}
56
+ </Slot>
57
+ ) : (
58
+ <button
59
+ ref={ref as React.RefObject<HTMLButtonElement>}
60
+ type="button"
61
+ className={className}
62
+ onClick={handleClick}
63
+ {...rest}
64
+ >
65
+ {children}
66
+ </button>
67
+ );
68
+ },
69
+ );
70
+
71
+ Reset.displayName = "ChessPuzzle.Reset";
@@ -64,3 +64,5 @@ export const Root: React.FC<React.PropsWithChildren<RootProps>> = ({
64
64
  </ChessGame.Root>
65
65
  );
66
66
  };
67
+
68
+ Root.displayName = "ChessPuzzle.Root";
@@ -0,0 +1,158 @@
1
+ import React from "react";
2
+ import { render, screen, fireEvent } from "@testing-library/react";
3
+ import "@testing-library/jest-dom";
4
+ import { ChessPuzzle } from "../..";
5
+ import { Hint } from "../Hint";
6
+ import { Puzzle } from "../../../../utils";
7
+
8
+ describe("ChessPuzzle.Hint", () => {
9
+ const mockPuzzle: Puzzle = {
10
+ fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
11
+ moves: ["e4", "e5"],
12
+ };
13
+
14
+ it("should have correct displayName", () => {
15
+ expect(Hint.displayName).toBe("ChessPuzzle.Hint");
16
+ });
17
+
18
+ it("should forward ref to button element", () => {
19
+ const ref = React.createRef<HTMLElement>();
20
+
21
+ render(
22
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
23
+ <Hint ref={ref} />
24
+ </ChessPuzzle.Root>,
25
+ );
26
+
27
+ expect(ref.current).toBeInstanceOf(HTMLButtonElement);
28
+ });
29
+
30
+ it("should forward ref when using asChild", () => {
31
+ const ref = React.createRef<HTMLElement>();
32
+
33
+ render(
34
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
35
+ <Hint ref={ref} asChild>
36
+ <button>Custom Hint</button>
37
+ </Hint>
38
+ </ChessPuzzle.Root>,
39
+ );
40
+
41
+ expect(ref.current).toBeInstanceOf(HTMLButtonElement);
42
+ });
43
+
44
+ it("should be visible when status matches showOn", () => {
45
+ render(
46
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
47
+ <Hint showOn={["not-started"]}>Hint</Hint>
48
+ </ChessPuzzle.Root>,
49
+ );
50
+
51
+ expect(screen.getByRole("button")).toBeInTheDocument();
52
+ });
53
+
54
+ it("should be hidden when status does not match showOn", () => {
55
+ render(
56
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
57
+ <Hint showOn={["solved"]}>Hint</Hint>
58
+ </ChessPuzzle.Root>,
59
+ );
60
+
61
+ expect(screen.queryByRole("button")).not.toBeInTheDocument();
62
+ });
63
+
64
+ it("should default to showing on not-started and in-progress statuses", () => {
65
+ render(
66
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
67
+ <Hint>Hint</Hint>
68
+ </ChessPuzzle.Root>,
69
+ );
70
+
71
+ expect(screen.getByRole("button")).toBeInTheDocument();
72
+ });
73
+
74
+ it("should accept multiple statuses in showOn", () => {
75
+ render(
76
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
77
+ <Hint showOn={["not-started", "in-progress", "failed", "solved"]}>
78
+ Hint
79
+ </Hint>
80
+ </ChessPuzzle.Root>,
81
+ );
82
+
83
+ expect(screen.getByRole("button")).toBeInTheDocument();
84
+ });
85
+
86
+ it("should render custom element when asChild is true", () => {
87
+ render(
88
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
89
+ <Hint asChild>
90
+ <button className="custom-button">Custom Hint</button>
91
+ </Hint>
92
+ </ChessPuzzle.Root>,
93
+ );
94
+
95
+ const button = screen.getByRole("button");
96
+ expect(button).toHaveTextContent("Custom Hint");
97
+ expect(button).toHaveClass("custom-button");
98
+ });
99
+
100
+ it("should compose onClick handlers with asChild", () => {
101
+ const childOnClick = jest.fn();
102
+
103
+ render(
104
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
105
+ <Hint asChild>
106
+ <button onClick={childOnClick}>Hint</button>
107
+ </Hint>
108
+ </ChessPuzzle.Root>,
109
+ );
110
+
111
+ fireEvent.click(screen.getByRole("button"));
112
+ expect(childOnClick).toHaveBeenCalledTimes(1);
113
+ });
114
+
115
+ it("should throw error when used outside ChessPuzzle.Root", () => {
116
+ const consoleError = jest
117
+ .spyOn(console, "error")
118
+ .mockImplementation(() => {});
119
+
120
+ expect(() => {
121
+ render(<Hint>Hint</Hint>);
122
+ }).toThrow();
123
+
124
+ consoleError.mockRestore();
125
+ });
126
+
127
+ it("should render text children", () => {
128
+ render(
129
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
130
+ <Hint>Show Hint</Hint>
131
+ </ChessPuzzle.Root>,
132
+ );
133
+
134
+ expect(screen.getByRole("button")).toHaveTextContent("Show Hint");
135
+ });
136
+
137
+ it("should render element children", () => {
138
+ render(
139
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
140
+ <Hint>
141
+ <span data-testid="child">Hint Icon</span>
142
+ </Hint>
143
+ </ChessPuzzle.Root>,
144
+ );
145
+
146
+ expect(screen.getByTestId("child")).toBeInTheDocument();
147
+ });
148
+
149
+ it("should render without children", () => {
150
+ render(
151
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
152
+ <Hint aria-label="Show hint" />
153
+ </ChessPuzzle.Root>,
154
+ );
155
+
156
+ expect(screen.getByRole("button")).toBeInTheDocument();
157
+ });
158
+ });
@@ -0,0 +1,140 @@
1
+ import React from "react";
2
+ import { render, screen } from "@testing-library/react";
3
+ import "@testing-library/jest-dom";
4
+ import { ChessPuzzle } from "../..";
5
+ import { PuzzleBoard } from "../PuzzleBoard";
6
+ import { Puzzle } from "../../../../utils";
7
+ import { ChessGame } from "@react-chess-tools/react-chess-game";
8
+
9
+ describe("ChessPuzzle.PuzzleBoard", () => {
10
+ const mockPuzzle: Puzzle = {
11
+ fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
12
+ moves: ["e4", "e5"],
13
+ };
14
+
15
+ it("should have correct displayName", () => {
16
+ expect(PuzzleBoard.displayName).toBe("ChessPuzzle.PuzzleBoard");
17
+ });
18
+
19
+ it("should forward ref to underlying Board component", () => {
20
+ const ref = React.createRef<HTMLDivElement>();
21
+
22
+ render(
23
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
24
+ <PuzzleBoard ref={ref} />
25
+ </ChessPuzzle.Root>,
26
+ );
27
+
28
+ expect(ref.current).toBeInstanceOf(HTMLDivElement);
29
+ });
30
+
31
+ it("should allow focusing via ref", () => {
32
+ const ref = React.createRef<HTMLDivElement>();
33
+
34
+ render(
35
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
36
+ <PuzzleBoard ref={ref} tabIndex={0} />
37
+ </ChessPuzzle.Root>,
38
+ );
39
+
40
+ expect(ref.current).toBeInstanceOf(HTMLDivElement);
41
+
42
+ // Note: focus() doesn't work in JSDOM, but we can verify the ref points to the element
43
+ // In a real browser, ref.current?.focus() would set document.activeElement to ref.current
44
+ });
45
+
46
+ it("should apply custom className", () => {
47
+ const { container } = render(
48
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
49
+ <PuzzleBoard className="custom-puzzle-board-class" />
50
+ </ChessPuzzle.Root>,
51
+ );
52
+
53
+ const board = container.querySelector('[style*="position: relative"]');
54
+ expect(board).toHaveClass("custom-puzzle-board-class");
55
+ });
56
+
57
+ it("should apply custom style", () => {
58
+ const customStyle = { border: "2px solid blue", margin: "15px" };
59
+
60
+ const { container } = render(
61
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
62
+ <PuzzleBoard style={customStyle} />
63
+ </ChessPuzzle.Root>,
64
+ );
65
+
66
+ const board = container.querySelector('[style*="position: relative"]');
67
+ expect(board).toHaveStyle({ border: "2px solid blue" });
68
+ expect(board).toHaveStyle({ margin: "15px" });
69
+ });
70
+
71
+ it("should apply custom id", () => {
72
+ const { container } = render(
73
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
74
+ <PuzzleBoard id="custom-puzzle-board-id" />
75
+ </ChessPuzzle.Root>,
76
+ );
77
+
78
+ const board = container.querySelector('[style*="position: relative"]');
79
+ expect(board).toHaveAttribute("id", "custom-puzzle-board-id");
80
+ });
81
+
82
+ it("should apply data-* attributes", () => {
83
+ render(
84
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
85
+ <PuzzleBoard data-testid="puzzle-board" data-custom="puzzle-value" />
86
+ </ChessPuzzle.Root>,
87
+ );
88
+
89
+ const board = screen.getByTestId("puzzle-board");
90
+ expect(board).toHaveAttribute("data-custom", "puzzle-value");
91
+ });
92
+
93
+ it("should apply aria-* attributes", () => {
94
+ const { container } = render(
95
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
96
+ <PuzzleBoard aria-label="Puzzle board" aria-describedby="puzzle-desc" />
97
+ </ChessPuzzle.Root>,
98
+ );
99
+
100
+ const board = container.querySelector('[style*="position: relative"]');
101
+ expect(board).toHaveAttribute("aria-label", "Puzzle board");
102
+ expect(board).toHaveAttribute("aria-describedby", "puzzle-desc");
103
+ });
104
+
105
+ it("should accept custom onClick handler", () => {
106
+ const handleClick = jest.fn();
107
+
108
+ const { container } = render(
109
+ <ChessPuzzle.Root puzzle={mockPuzzle}>
110
+ <PuzzleBoard onClick={handleClick} />
111
+ </ChessPuzzle.Root>,
112
+ );
113
+
114
+ const board = container.querySelector(
115
+ '[style*="position: relative"]',
116
+ ) as HTMLElement;
117
+ board?.click();
118
+
119
+ expect(handleClick).toHaveBeenCalledTimes(1);
120
+ });
121
+
122
+ it("should throw error when used outside ChessPuzzle.Root", () => {
123
+ // Suppress console.error for this test
124
+ const consoleError = jest
125
+ .spyOn(console, "error")
126
+ .mockImplementation(() => {});
127
+
128
+ expect(() => {
129
+ render(
130
+ <ChessGame.Root>
131
+ <PuzzleBoard />
132
+ </ChessGame.Root>,
133
+ );
134
+ }).toThrow(
135
+ "useChessPuzzleContext must be used within a ChessPuzzle component",
136
+ );
137
+
138
+ consoleError.mockRestore();
139
+ });
140
+ });