Implement catalog API integration and tests: commerce/src/api/catalog.test.ts
davoodmehraban89 committed
Oct 13, 2026 at 23:12 UTC
134b819d10e4abc6f59a61f9d9d3ccadabe6b5d1
1 file changed
+28
commerce/src/api/catalog.test.ts
new
+28
@@ -0,0 +1,28 @@
1
+import { describe, expect, it, vi } from "vitest"
2
+import { CatalogError, fetchCatalog } from "./catalog"
3
+
4
+const options = { backendUrl: "http://localhost:9000", publishableKey: "pk_test_example" }
5
+
6
+describe("Medusa Store API catalog", () => {
7
+ it("sends publishable API key and parses products", async () => {
8
+ const fetcher = vi.fn(async () => new Response(JSON.stringify({ products: [
9
+ { id: "prod_1", title: "Sample", handle: "sample", thumbnail: "https://example.com/a.jpg" },
10
+ ] }), { status: 200 }))
11
+ const products = await fetchCatalog("IR", { ...options, fetcher: fetcher as typeof fetch })
12
+ expect(products).toHaveLength(1)
13
+ expect(products[0].handle).toBe("sample")
14
+ expect(fetcher.mock.calls[0][0]).toContain("/store/products?limit=24")
15
+ expect((fetcher.mock.calls[0][1] as RequestInit).headers).toMatchObject({ "x-publishable-api-key": "pk_test_example" })
16
+ })
17
+ it("rejects missing API key", async () => {
18
+ await expect(fetchCatalog("AE", { ...options, publishableKey: "" })).rejects.toThrow("publishable key")
19
+ })
20
+ it("surfaces HTTP errors", async () => {
21
+ const fetcher = vi.fn(async () => new Response("Unauthorized", { status: 401 }))
22
+ await expect(fetchCatalog("QA", { ...options, fetcher: fetcher as typeof fetch })).rejects.toMatchObject({ status: 401 })
23
+ })
24
+ it("rejects malformed response", async () => {
25
+ const fetcher = vi.fn(async () => new Response(JSON.stringify({ items: [] }), { status: 200 }))
26
+ await expect(fetchCatalog("OM", { ...options, fetcher: fetcher as typeof fetch })).rejects.toBeInstanceOf(CatalogError)
27
+ })
28
+})