Files
jobtrackingapp/job-tracker-ui/src/application-calendar.test.tsx
T

46 lines
2.7 KiB
TypeScript

import "@testing-library/jest-dom";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { api } from "./api";
import ApplicationCalendarActions from "./components/ApplicationCalendarActions";
import { I18nProvider } from "./i18n/I18nProvider";
jest.mock("./api", () => ({
api: { get: jest.fn(), post: jest.fn() },
getApiErrorMessage: (_: unknown, fallback: string) => fallback,
}));
const mockedApi = api as jest.Mocked<typeof api>;
beforeEach(() => {
window.localStorage.clear();
mockedApi.get.mockResolvedValue({ data: { google: { connected: true, writable: true }, microsoft: { connected: false, writable: false } } } as any);
mockedApi.post.mockResolvedValue({ data: { provider: "google", id: "event-1", webUrl: "https://calendar.google.test/event-1" } } as any);
});
afterEach(() => jest.clearAllMocks());
test("adds the selected application date to a writable connected calendar", async () => {
render(<I18nProvider><ApplicationCalendarActions jobId={42} followUpAt="2026-09-02T10:30:00Z" deadline="2026-09-15T00:00:00Z" /></I18nProvider>);
const googleButtons = await screen.findAllByRole("button", { name: "Google Calendar" });
fireEvent.click(googleButtons[0]);
await waitFor(() => expect(mockedApi.post).toHaveBeenCalledWith("/calendar/jobs/42/events", { provider: "google", kind: "follow-up" }));
expect(await screen.findByText("The event was added to your calendar.")).toBeInTheDocument();
expect(screen.getByRole("link", { name: "Open event" })).toHaveAttribute("href", "https://calendar.google.test/event-1");
});
test("keeps portable calendar download available without a provider connection", async () => {
mockedApi.get.mockResolvedValue({ data: { google: { connected: false, writable: false }, microsoft: { connected: false, writable: false } } } as any);
render(<I18nProvider><ApplicationCalendarActions jobId={42} followUpAt={null} deadline="2026-09-15T00:00:00Z" /></I18nProvider>);
expect(await screen.findByRole("button", { name: "Download .ics" })).toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Google Calendar" })).not.toBeInTheDocument();
});
test("explains when an existing connection needs renewed calendar consent", async () => {
mockedApi.get.mockResolvedValue({ data: { google: { connected: true, writable: false }, microsoft: { connected: false, writable: false } } } as any);
render(<I18nProvider><ApplicationCalendarActions jobId={42} followUpAt="2026-09-02T10:30:00Z" /></I18nProvider>);
expect(await screen.findByText("Reconnect your calendar account to approve calendar access.")).toBeInTheDocument();
expect(screen.getByRole("link", { name: "Reconnect" })).toHaveAttribute("href", "/settings");
});