diff --git a/packages/chess/src/ui/ParamPiecePicker.test.tsx b/packages/chess/src/ui/ParamPiecePicker.test.tsx new file mode 100644 index 0000000..2606f4e --- /dev/null +++ b/packages/chess/src/ui/ParamPiecePicker.test.tsx @@ -0,0 +1,39 @@ +import React from 'react'; +import { renderToStaticMarkup } from 'react-dom/server'; +import { describe, it, expect, vi } from 'vitest'; +import { ParamPiecePicker } from './ParamPiecePicker'; + +describe('ParamPiecePicker', () => { + const pieces = [ + { id: 1, type: 'queen', color: 'white' as const, square: 28 }, // e4 + { id: 2, type: 'knight', color: 'black' as const, square: 62 }, // g8 + ]; + + it('renders dropdown listing pieces with color, type and square name', () => { + const html = renderToStaticMarkup( + {}} + pieces={pieces} + /> + ); + + expect(html).toContain('white queen at e4'); + expect(html).toContain('black knight at g8'); + expect(html).toContain('value="1"'); + expect(html).toContain('value="2"'); + }); + + it('calls onChange with entityId when selecting an option', () => { + // Testing React component events without testing-library: + // We instantiate the component directly and call its props.onChange to verify the type + const onChange = vi.fn(); + const component = ParamPiecePicker({ value: 1, onChange, pieces }); + + // Trigger the onChange handler passed to the select element + component.props.onChange({ target: { value: '2' } } as unknown as React.ChangeEvent); + + expect(onChange).toHaveBeenCalledTimes(1); + expect(onChange).toHaveBeenCalledWith(2); + }); +}); diff --git a/packages/chess/src/ui/ParamPiecePicker.tsx b/packages/chess/src/ui/ParamPiecePicker.tsx new file mode 100644 index 0000000..6dee1b8 --- /dev/null +++ b/packages/chess/src/ui/ParamPiecePicker.tsx @@ -0,0 +1,35 @@ +import React from 'react'; +import { squareToAlgebraic } from '../coord'; + +export interface ParamPiecePickerProps { + value: number | undefined; + onChange: (entityId: number) => void; + pieces: readonly { + id: number; + type: string; + color: "white" | "black"; + square: number; + }[]; +} + +export function ParamPiecePicker({ value, onChange, pieces }: ParamPiecePickerProps) { + return ( + + ); +}