test: add unit and component tests across the monorepo

Adds ~1000 tests to bring the previously-untested packages and apps
under coverage:

- packages/utils — string, color, date, file, level, locale, sanitize,
  field, html, network-icons, rate-limit, url, url-security, monorepo,
  resume/patch, resume/docx/link-utils, style helpers (~97% on the
  testable utility files)
- packages/ui — 28 component test files plus the use-controlled-state,
  use-mobile, use-confirm, use-prompt hooks (95% statements)
- packages/pdf — shared template helpers (filtering, columns, picture,
  metrics, section-links, page-size, rich-text-html, section-title)
- packages/schema — resumeDataSchema, page, templates, default
- packages/fonts — expanded coverage on font helpers
- packages/ai — resume sanitize and extraction template
- packages/api — resume-access-policy
- apps/web — error-message, locale, theme, pwa, dialogs/store, and
  the resume/move-item / section-actions / make-section-item helpers

Adds jsdom polyfills (ResizeObserver, IntersectionObserver,
scrollIntoView, matchMedia) and an explicit React Testing Library
cleanup hook to vitest.setup.ts so portal- and overlay-based components
work without per-test setup.
This commit is contained in:
Amruth Pillai
2026-05-10 20:00:07 +02:00
parent 33103536ae
commit 6a01207b6b
83 changed files with 8099 additions and 6 deletions
+124
View File
@@ -0,0 +1,124 @@
/**
* @vitest-environment jsdom
*/
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { useDialogStore } from "./store";
describe("useDialogStore", () => {
beforeEach(() => {
vi.useFakeTimers();
// Reset store state between tests
useDialogStore.setState({ open: false, activeDialog: null, onBeforeClose: null });
});
afterEach(() => {
vi.useRealTimers();
});
describe("openDialog", () => {
it("opens a dialog and sets activeDialog", () => {
useDialogStore.getState().openDialog("api-key.create", undefined);
const state = useDialogStore.getState();
expect(state.open).toBe(true);
expect(state.activeDialog?.type).toBe("api-key.create");
});
it("clears any existing onBeforeClose handler", () => {
useDialogStore.setState({ onBeforeClose: () => true });
useDialogStore.getState().openDialog("resume.create", undefined);
expect(useDialogStore.getState().onBeforeClose).toBeNull();
});
it("preserves dialog data for typed payloads", () => {
const data = { id: "r1", name: "My Resume", slug: "my-resume", tags: [] };
useDialogStore.getState().openDialog("resume.update", data);
const active = useDialogStore.getState().activeDialog;
expect(active?.type).toBe("resume.update");
if (active?.type === "resume.update") {
expect(active.data).toEqual(data);
}
});
});
describe("closeDialog", () => {
it("immediately sets open to false", () => {
useDialogStore.setState({
open: true,
activeDialog: { type: "api-key.create", data: undefined },
});
useDialogStore.getState().closeDialog();
expect(useDialogStore.getState().open).toBe(false);
});
it("clears activeDialog after 300ms (animation finished)", () => {
useDialogStore.setState({
open: true,
activeDialog: { type: "api-key.create", data: undefined },
});
useDialogStore.getState().closeDialog();
expect(useDialogStore.getState().activeDialog).not.toBeNull();
vi.advanceTimersByTime(300);
expect(useDialogStore.getState().activeDialog).toBeNull();
});
});
describe("onOpenChange", () => {
it("opens via set without invoking onBeforeClose", () => {
const onBefore = vi.fn();
useDialogStore.setState({ onBeforeClose: onBefore });
useDialogStore.getState().onOpenChange(true);
expect(useDialogStore.getState().open).toBe(true);
expect(onBefore).not.toHaveBeenCalled();
});
it("closes immediately when no onBeforeClose handler", () => {
useDialogStore.setState({ open: true, activeDialog: { type: "api-key.create", data: undefined } });
useDialogStore.getState().onOpenChange(false);
expect(useDialogStore.getState().open).toBe(false);
});
it("invokes onBeforeClose when closing", async () => {
const handler = vi.fn().mockResolvedValue(true);
useDialogStore.setState({
open: true,
activeDialog: { type: "api-key.create", data: undefined },
onBeforeClose: handler,
});
useDialogStore.getState().onOpenChange(false);
await vi.waitFor(() => expect(handler).toHaveBeenCalled());
});
it("calls cancel when onBeforeClose returns false", async () => {
const handler = vi.fn().mockResolvedValue(false);
const cancel = vi.fn();
useDialogStore.setState({
open: true,
activeDialog: { type: "api-key.create", data: undefined },
onBeforeClose: handler,
});
useDialogStore.getState().onOpenChange(false, { cancel });
await vi.waitFor(() => expect(cancel).toHaveBeenCalled());
});
});
describe("setOnBeforeClose", () => {
it("sets and clears the handler", () => {
const handler = () => true;
useDialogStore.getState().setOnBeforeClose(handler);
expect(useDialogStore.getState().onBeforeClose).toBe(handler);
useDialogStore.getState().setOnBeforeClose(null);
expect(useDialogStore.getState().onBeforeClose).toBeNull();
});
});
});