From 5291fd2fd1e61d9c5e3cc39ddac710ce933b01f2 Mon Sep 17 00:00:00 2001 From: SJCoetz Date: Fri, 7 Aug 2026 02:50:44 +0200 Subject: [PATCH] test(context): /api/v1/order-items/[id]/context route tests Co-authored-by: Orca --- .../v1/order-items/[id]/context/route.test.ts | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/app/api/v1/order-items/[id]/context/route.test.ts diff --git a/src/app/api/v1/order-items/[id]/context/route.test.ts b/src/app/api/v1/order-items/[id]/context/route.test.ts new file mode 100644 index 0000000..2830b69 --- /dev/null +++ b/src/app/api/v1/order-items/[id]/context/route.test.ts @@ -0,0 +1,51 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; +import { NextRequest } from "next/server"; + +const getOrderItemContext = vi.fn(); +vi.mock("@/lib/server/order-item-context-service", () => ({ + getOrderItemContext: (...args: unknown[]) => getOrderItemContext(...args), +})); + +import { GET } from "./route"; + +function get(id: string) { + return new NextRequest(`http://localhost/api/v1/order-items/${id}/context`, { + method: "GET", + }); +} + +describe("GET /api/v1/order-items/[id]/context", () => { + beforeEach(() => { + getOrderItemContext.mockReset(); + }); + + it("400s on missing id", async () => { + const res = await GET(get(""), { params: Promise.resolve({ id: "" }) }); + expect(res.status).toBe(400); + }); + + it("200s with the service result", async () => { + getOrderItemContext.mockResolvedValue({ + generated_at: "2026-08-07T10:00:00Z", + match: { sku: "DRL-6MM", match_kind: "sku_exact", match_confidence: 1 }, + last_quote: null, + last_order: null, + last_invoiced: null, + stock: null, + suppliers: [], + cheapest: null, + }); + const res = await GET(get("abc"), { params: Promise.resolve({ id: "abc" }) }); + expect(res.status).toBe(200); + const body = await res.json(); + expect(body.match.sku).toBe("DRL-6MM"); + }); + + it("500s when the service throws", async () => { + getOrderItemContext.mockRejectedValue(new Error("PB exploded")); + const res = await GET(get("abc"), { params: Promise.resolve({ id: "abc" }) }); + expect(res.status).toBe(500); + const body = await res.json(); + expect(body.error).toBe("PB exploded"); + }); +});