refactor: simplify DigitalClock and TimeConverter components

This commit is contained in:
Loic Coenen
2026-06-25 10:43:36 +02:00
committed by Loic Coenen (aider)
parent 3fed6dc168
commit 0b8d76a057
5 changed files with 43 additions and 41 deletions

View File

@@ -36,15 +36,16 @@ describe("DigitalClock", () => {
expect(input).toBeDisabled();
});
it("renders the digital time and input when enabled", () => {
it("renders the input with the correct initial value when enabled", () => {
render(
<DigitalClock
currentTime={new Date(2025, 0, 1, 14, 30, 15)}
onTimeChange={onTimeChangeMock}
/>,
);
expect(screen.getByTestId("digital-clock")).toHaveTextContent("14:30:15");
expect(screen.getByTestId("time-input")).toBeInTheDocument();
const input = screen.getByTestId("time-input") as HTMLInputElement;
expect(input).toBeInTheDocument();
expect(input.value).toBe("14:30:15");
});
it("calls onTimeChange when a valid time is entered", () => {

View File

@@ -10,33 +10,26 @@ vi.mock("../containers/BerlinClock", () => ({
}));
vi.mock("../containers/DigitalClock", () => ({
DigitalClock: ({
enabled,
currentTime,
onTimeChange,
disabled = false,
}: {
enabled: boolean;
currentTime: Date;
onTimeChange: (d: Date) => void;
disabled?: boolean;
}) => (
<div data-testid="digital-clock-wrapper">
<span data-testid="digital-clock-time">{currentTime.toISOString()}</span>
<input
data-testid="time-input"
disabled={disabled}
value=""
onChange={() => {}}
/>
<button
data-testid="set-time"
onClick={() => onTimeChange(new Date(2025, 0, 1, 12, 0, 0))}
>
Set time
</button>
{!enabled && <span data-testid="disabled-indicator">disabled</span>}
</div>
),
}) => {
const digitalTime = `${String(currentTime.getHours()).padStart(2, "0")}:${String(currentTime.getMinutes()).padStart(2, "0")}:${String(currentTime.getSeconds()).padStart(2, "0")}`;
return (
<div data-testid="digital-clock-wrapper">
<input
data-testid="time-input"
disabled={disabled}
value={digitalTime}
onChange={() => {}}
/>
</div>
);
},
}));
const mockedUseCurrentTime = vi.mocked(useCurrentTime);
@@ -72,12 +65,10 @@ describe("TimeConverter", () => {
expect(screen.getByTestId("berlin-clock")).toHaveTextContent("enabled");
});
it("calls setCurrentTime when DigitalClock triggers onTimeChange", () => {
it("renders the digital clock input with the correct initial value", () => {
render(<TimeConverter />);
fireEvent.click(screen.getByTestId("set-time"));
expect(setCurrentTimeMock).toHaveBeenCalledWith(
new Date(2025, 0, 1, 12, 0, 0),
);
const input = screen.getByTestId("time-input") as HTMLInputElement;
expect(input.value).toBe("14:30:15");
});
// ──────────────────────────────────────────────────────────
@@ -101,22 +92,18 @@ describe("TimeConverter", () => {
render(<TimeConverter />);
// Initially the digital clock shows the manual time 12:00:00
expect(screen.getByTestId("digital-clock-time")).toHaveTextContent(
new Date(2025, 0, 1, 12, 0, 0).toISOString(),
);
// Initially the digital clock input shows the manual time 12:00:00
const input = screen.getByTestId("time-input") as HTMLInputElement;
expect(input.value).toBe("12:00:00");
// Click the "Use system time" checkbox
fireEvent.click(screen.getByTestId("use-system-time"));
// The component SHOULD have called setCurrentDate with the system time
// (currently it does NOT, so this assertion fails)
// The component should have called setCurrentDate with the system time
expect(setCurrentDate).toHaveBeenCalledWith(new Date(2025, 0, 1, 15, 0, 0));
// And the digital clock should now show the system time 15:00:00
expect(screen.getByTestId("digital-clock-time")).toHaveTextContent(
new Date(2025, 0, 1, 15, 0, 0).toISOString(),
);
// And the input should now show the system time 15:00:00
expect(input.value).toBe("15:00:00");
vi.useRealTimers();
});