Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

8.5 KB
Raw Blame
// @vitest-environment happy-dom

import type { Application } from "../types";
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { beforeAll, beforeEach, expect, it, vi } from "vite-plus/test";
import { i18n } from "@lingui/core";
import { I18nProvider } from "@lingui/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";

vi.mock("@tanstack/react-router", () => ({
	useRouteContext: () => ({ session: { user: { timezone: mocks.timeZone } } }),
}));

const mocks = vi.hoisted(() => ({
	timeZone: "UTC",
	upload: vi.fn(),
	removeFile: vi.fn(),
	create: vi.fn(),
	update: vi.fn(),
	autofill: vi.fn(),
	documents: [] as Array<{
		id: string;
		name: string;
		type: "letter";
		isLocked: boolean;
		application: { id: string } | null;
	}>,
}));

vi.mock("@/libs/orpc/client", () => ({
	orpc: {
		resume: { list: { queryOptions: () => ({ queryKey: ["resume", "list"], queryFn: async () => [] }) } },
		applications: {
			lanes: {
				get: {
					queryOptions: () => ({ queryKey: ["lanes"], queryFn: () => new Promise(() => {}) }),
					queryKey: () => ["lanes"],
				},
			},
			priorNoResponse: { queryOptions: () => ({ queryKey: ["prior"], queryFn: async () => [] }) },
			listSummary: { queryKey: () => ["applications", "summary"] },
			tags: {
				queryOptions: () => ({ queryKey: ["applications", "tags"], queryFn: async () => [] }),
				queryKey: () => ["applications", "tags"],
			},
			stats: { queryKey: () => ["applications", "stats"] },
			getById: { queryKey: () => ["applications", "getById"] },
			create: { mutationOptions: (options: object) => ({ ...options, mutationFn: mocks.create }) },
			update: { mutationOptions: (options: object) => ({ ...options, mutationFn: mocks.update }) },
			ai: { autofill: { mutationOptions: (options: object) => ({ ...options, mutationFn: mocks.autofill }) } },
		},
		documents: {
			list: { queryOptions: () => ({ queryKey: ["documents", "list"], queryFn: async () => mocks.documents }) },
		},
		aiProviders: { list: { queryOptions: () => ({ queryKey: ["aiProviders"], queryFn: async () => [] }) } },
		storage: {
			uploadFile: { mutationOptions: (options: object) => ({ ...options, mutationFn: mocks.upload }) },
			deleteFile: { mutationOptions: (options: object) => ({ ...options, mutationFn: mocks.removeFile }) },
		},
	},
}));

const { ApplicationFormSheet } = await import("./application-form-sheet");

vi.mock("@reactive-resume/ui/components/toast", () => ({ toast: { add: vi.fn(), close: vi.fn() } }));

const OLD_URL = "https://example.com/api/uploads/user-1/pictures/old.pdf";

const NEW_KEY = "uploads/user-1/pictures/new.pdf";

const application: Application = {
	id: "app-1",
	company: "Example",
	role: "Engineer",
	location: null,
	salary: null,
	status: "saved",
	closedReason: null,
	postingSource: null,
	coverLetterId: null,
	sentResumeVersionId: null,
	sentCoverLetterVersionId: null,
	sentCheckScore: null,
	requirements: [],
	resumeId: null,
	source: null,
	sourceUrl: null,
	jobDescription: null,
	matchScore: null,
	aiMetadata: null,
	notes: null,
	resumeFileUrl: OLD_URL,
	resumeFileName: "old.pdf",
	coverLetterUrl: null,
	coverLetterName: null,
	followUpAt: null,
	followUpNote: null,
	tags: [],
	contacts: [],
	activity: [],
	appliedAt: new Date(),
	createdAt: new Date(),
	updatedAt: new Date(),
};

beforeAll(() => i18n.loadAndActivate({ locale: "en", messages: {} }));
it("describes a fit check instead of promising a match score", () => {
	renderSheet();
	expect(screen.getByText(/a fit check against each requirement and tailoring/)).toBeTruthy();
	expect(screen.queryByText(/match scoring/i)).toBeNull();
});

beforeEach(() => {
	vi.resetAllMocks();
	mocks.timeZone = "UTC";
	mocks.documents = [];
	mocks.upload.mockResolvedValue({
		url: "https://example.com/api/uploads/user-1/pictures/new.pdf",
		path: NEW_KEY,
		contentType: "application/pdf",
	});
	mocks.update.mockResolvedValue(application);
	mocks.create.mockResolvedValue(application);
	mocks.removeFile.mockResolvedValue(undefined);
});

function renderSheet(app: Application | null = application, onOpenChange = vi.fn()) {
	render(
		<QueryClientProvider client={new QueryClient({ defaultOptions: { mutations: { retry: false } } })}>
			<I18nProvider i18n={i18n}>
				<ApplicationFormSheet open application={app} onOpenChange={onOpenChange} />
			</I18nProvider>
		</QueryClientProvider>,
	);
	return { onOpenChange };
}

it("shows the recorded letter even when locked, without offering other locked letters", async () => {
	mocks.documents = [
		{ id: "letter-1", name: "Recorded letter", type: "letter", isLocked: true, application: { id: application.id } },
		{ id: "letter-2", name: "Other locked letter", type: "letter", isLocked: true, application: null },
	];
	renderSheet({ ...application, coverLetterId: "letter-1", sentCoverLetterVersionId: "sent-1" });
	const select = screen.getByRole("combobox", { name: "Link an existing letter" });
	await waitFor(() => expect(select).toHaveValue("letter-1"));
	expect(select).toBeDisabled();
	expect(screen.queryByRole("option", { name: "Other locked letter" })).toBeNull();
});

async function pickResumeFile(file: File) {
	const input = document.querySelector<HTMLInputElement>('input[type="file"]');
	if (!input) throw new Error("Missing attachment picker");
	await act(async () => fireEvent.change(input, { target: { files: [file] } }));
}

async function clickRemove() {
	await act(async () => fireEvent.click(screen.getByTitle("Remove file")));
}

const pdfFile = (name = "new.pdf") => new File(["%PDF-1.4"], name, { type: "application/pdf" });

function deferred<T>() {
	let resolve!: (value: T) => void;
	let reject!: (reason?: unknown) => void;
	const promise = new Promise<T>((res, rej) => {
		resolve = res;
		reject = rej;
	});
	return { promise, resolve, reject };
}

it.each(["remove", "pick"])("cancelling %s leaves storage and saved application untouched", async (action) => {
	const { onOpenChange } = renderSheet();
	if (action === "remove") await clickRemove();
	else await pickResumeFile(pdfFile());
	await act(async () => fireEvent.click(screen.getByRole("button", { name: "Cancel" })));
	expect(onOpenChange).toHaveBeenCalledWith(false);
	expect(mocks.upload).not.toHaveBeenCalled();
	expect(mocks.removeFile).not.toHaveBeenCalled();
	expect(mocks.update).not.toHaveBeenCalled();
	expect(application.resumeFileUrl).toBe(OLD_URL);
});

it("saves picked files with the application in one request and blocks dismissal until it finishes", async () => {
	const write = deferred<Application>();
	mocks.update.mockReturnValue(write.promise);
	const { onOpenChange } = renderSheet();
	const file = pdfFile();
	await pickResumeFile(file);
	await act(async () => fireEvent.click(screen.getByRole("button", { name: "Save changes" })));
	await waitFor(() =>
		expect(mocks.update).toHaveBeenCalledWith(expect.objectContaining({ resumeFile: file }), expect.anything()),
	);
	await act(async () => fireEvent.keyDown(screen.getByRole("dialog"), { key: "Escape" }));
	expect(onOpenChange).not.toHaveBeenCalled();
	await act(async () => write.resolve(application));
	await waitFor(() => expect(onOpenChange).toHaveBeenCalledWith(false));
	expect(mocks.upload).not.toHaveBeenCalled();
	expect(mocks.removeFile).not.toHaveBeenCalled();
});

it("keeps a failed save editable and cancellation performs no client storage cleanup", async () => {
	mocks.update.mockRejectedValue(new Error("save failed"));
	const { onOpenChange } = renderSheet();
	await pickResumeFile(pdfFile());
	await act(async () => fireEvent.click(screen.getByRole("button", { name: "Save changes" })));
	await waitFor(() => expect(mocks.update).toHaveBeenCalled());
	expect(onOpenChange).not.toHaveBeenCalled();
	await act(async () => fireEvent.click(screen.getByRole("button", { name: "Cancel" })));
	expect(onOpenChange).toHaveBeenCalledWith(false);
	expect(mocks.upload).not.toHaveBeenCalled();
	expect(mocks.removeFile).not.toHaveBeenCalled();
});

it("lets a changed follow-up day correct an inherited time skipped by daylight saving", async () => {
	mocks.timeZone = "Europe/Berlin";
	renderSheet({ ...application, followUpAt: new Date("2027-03-27T01:30:00Z"), followUpDateOnly: false });
	expect(screen.getByLabelText("Time")).toHaveValue("02:30");
	fireEvent.change(screen.getByLabelText("Follow-up date"), { target: { value: "2027-03-28" } });
	fireEvent.change(screen.getByLabelText("Time"), { target: { value: "03:30" } });
	fireEvent.click(screen.getByRole("button", { name: "Save changes" }));
	await waitFor(() =>
		expect(mocks.update).toHaveBeenCalledWith(
			expect.objectContaining({ followUpAt: new Date("2027-03-28T01:30:00Z"), followUpDateOnly: false }),
			expect.anything(),
		),
	);
});