Refactor EventCard tests to verify hook arguments

This commit is contained in:
2026-05-10 19:11:04 +02:00
parent de9c16430b
commit 330cbb6b37
+69 -5
View File
@@ -1,6 +1,11 @@
import { describe, it, expect, vi } from "vitest"; import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen } from "@testing-library/react"; import { render, screen } from "@testing-library/react";
import EventCard from "../EventCard"; import EventCard from "../EventCard";
import { useDestinationStation } from "@/hooks/useDestinationStation";
import { useJourneys } from "@/hooks/useJourneys";
// vi.mock hoists, so these imports are the mocked versions
// We can inspect their call arguments directly
// Mock all hooks that EventCard depends on // Mock all hooks that EventCard depends on
vi.mock("@/hooks/useGeolocation", () => ({ vi.mock("@/hooks/useGeolocation", () => ({
@@ -12,11 +17,11 @@ vi.mock("@/hooks/useGeolocation", () => ({
})); }));
vi.mock("@/hooks/useDestinationStation", () => ({ vi.mock("@/hooks/useDestinationStation", () => ({
useDestinationStation: () => ({ useDestinationStation: vi.fn(() => ({
station: { name: "Wien Hbf", extId: "0WB0F0001500" }, station: { name: "Wien Hbf", extId: "0WB0F0001500" },
loading: false, loading: false,
error: null, error: null,
}), })),
})); }));
vi.mock("@/hooks/useGeocode", () => ({ vi.mock("@/hooks/useGeocode", () => ({
@@ -28,11 +33,11 @@ vi.mock("@/hooks/useGeocode", () => ({
})); }));
vi.mock("@/hooks/useJourneys", () => ({ vi.mock("@/hooks/useJourneys", () => ({
useJourneys: () => ({ useJourneys: vi.fn(() => ({
journeys: [], journeys: [],
loading: false, loading: false,
error: null, error: null,
}), })),
})); }));
vi.mock("@/hooks/useBikeRoute", () => ({ vi.mock("@/hooks/useBikeRoute", () => ({
@@ -43,6 +48,13 @@ vi.mock("@/hooks/useBikeRoute", () => ({
}), }),
})); }));
vi.mock("@/hooks/useClock", () => ({
useClock: () => ({
countdown: { label: "No deadline set", color: "text-gray-400", urgent: false },
status: "upcoming",
}),
}));
vi.mock("@/lib/countdown-utils", () => ({ vi.mock("@/lib/countdown-utils", () => ({
calculateCountdown: () => ({ calculateCountdown: () => ({
label: "No deadline set", label: "No deadline set",
@@ -52,6 +64,22 @@ vi.mock("@/lib/countdown-utils", () => ({
})); }));
describe("EventCard", () => { describe("EventCard", () => {
beforeEach(() => {
(useDestinationStation as ReturnType<typeof vi.fn>).mockReset();
(useDestinationStation as ReturnType<typeof vi.fn>).mockImplementation(() => ({
station: { name: "Wien Hbf", extId: "0WB0F0001500" },
loading: false,
error: null,
}));
(useJourneys as ReturnType<typeof vi.fn>).mockReset();
(useJourneys as ReturnType<typeof vi.fn>).mockImplementation(() => ({
journeys: [],
loading: false,
error: null,
}));
});
it("renders event title and destination", () => { it("renders event title and destination", () => {
const mockEvent = { const mockEvent = {
id: "test-1", id: "test-1",
@@ -67,4 +95,40 @@ describe("EventCard", () => {
expect(screen.getByText("Team Meeting")).toBeInTheDocument(); expect(screen.getByText("Team Meeting")).toBeInTheDocument();
expect(screen.getByText("Wien Hbf")).toBeInTheDocument(); expect(screen.getByText("Wien Hbf")).toBeInTheDocument();
}); });
it("passes destStation.station.extId to useJourneys as the destination argument", () => {
const mockEvent = {
id: "test-2",
title: "Lunch at Hofburg",
destination: "Hofburg",
eventTime: new Date("2025-12-01T12:00:00"),
source: "manual" as const,
};
const mockStation = { name: "Graz Hbf", extId: "0WB0F0000600" };
render(<EventCard event={mockEvent} originStation={mockStation} />);
// useDestinationStation should be called with the event's destination string
expect(useDestinationStation).toHaveBeenCalledWith("Hofburg");
// useJourneys should be called with origin extId as first arg
// and the destination station's extId as second arg (NOT null)
expect(useJourneys).toHaveBeenCalledWith("0WB0F0000600", "0WB0F0001500", new Date("2025-12-01T12:00:00"), 0);
});
it("passes null as originStation extId to useJourneys when originStation is null", () => {
const mockEvent = {
id: "test-3",
title: "Meeting without origin",
destination: "Parapluie",
eventTime: new Date("2025-12-01T10:00:00"),
source: "manual" as const,
};
render(<EventCard event={mockEvent} originStation={null} />);
// When originStation is null, the first arg to useJourneys should be null
expect(useJourneys).toHaveBeenCalledWith(null, "0WB0F0001500", new Date("2025-12-01T10:00:00"), 0);
});
}); });