test(context): /api/v1/order-items/[id]/context route tests
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
parent
7ad7375dd2
commit
5291fd2fd1
1 changed files with 51 additions and 0 deletions
51
src/app/api/v1/order-items/[id]/context/route.test.ts
Normal file
51
src/app/api/v1/order-items/[id]/context/route.test.ts
Normal file
|
|
@ -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");
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue