mercury-web/src/test/handlers.ts

43 lines
1.9 KiB
TypeScript

import { http, HttpResponse } from "msw";
import * as fixtures from "./fixtures";
/**
* MSW (v2) request handlers for every `/api/v1/*` read endpoint the app
* calls (see src/api/hooks/reads.ts), returning the fixtures in ./fixtures.ts.
* Reusable by any feature test that needs the whole read surface mocked —
* `setupServer(...handlers)` in ./server.ts, or override per-test with
* `server.use(...)` for error/loading-state cases.
*/
export const handlers = [
http.get("/api/v1/networth", () => HttpResponse.json(fixtures.netWorth)),
// Same endpoint backs both the month view (no query) and the "today" view
// (?from=&to=); distinguish on the query string, matching useAnalyzeMonth
// vs useAnalyzeToday in src/api/hooks/reads.ts.
http.get("/api/v1/analyze", ({ request }) => {
const url = new URL(request.url);
if (url.searchParams.get("period") === "all") return HttpResponse.json(fixtures.analyzeAll);
const isToday = url.searchParams.has("from") || url.searchParams.has("to");
return HttpResponse.json(isToday ? fixtures.analyzeToday : fixtures.analyzeMonth);
}),
http.get("/api/v1/budget", () => HttpResponse.json(fixtures.budget)),
http.get("/api/v1/transactions", () => HttpResponse.json({ transactions: [fixtures.txn] })),
http.get("/api/v1/categories", () => HttpResponse.json({ categories: fixtures.categories })),
http.get("/api/v1/subscriptions", () =>
HttpResponse.json({ subscriptions: fixtures.subscriptions, bills: fixtures.bills }),
),
http.get("/api/v1/manual-assets", () => HttpResponse.json({ manualAssets: fixtures.manualAssets })),
http.get("/api/v1/lending", () => HttpResponse.json({ entries: [fixtures.lending] })),
http.get("/api/v1/settings", () => HttpResponse.json(fixtures.settings)),
http.get("/api/v1/connections", () => HttpResponse.json({ connections: fixtures.connections })),
http.get("/api/v1/me", () => HttpResponse.json({ user: fixtures.user })),
];