|
| 1 | +// Integration test for generated query client using MSW |
| 2 | +// This test ensures the generated client (TS types only, no schema validation) has no runtime errors |
| 3 | + |
| 4 | +import { http, HttpResponse } from "msw"; |
| 5 | +import { setupServer } from "msw/node"; |
| 6 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 7 | +import { api } from "./api-client.example.js"; |
| 8 | +import { createApiClient } from "../tmp/generated-client.ts"; |
| 9 | + |
| 10 | +// Mock handler for a real endpoint from petstore.yaml |
| 11 | +const mockPets = [ |
| 12 | + { |
| 13 | + id: 1, |
| 14 | + name: "Fluffy", |
| 15 | + photoUrls: [], |
| 16 | + status: "available", |
| 17 | + }, |
| 18 | +]; |
| 19 | +const server = setupServer( |
| 20 | + // GET with query |
| 21 | + http.get("http://localhost/pet/findByStatus", () => { |
| 22 | + return HttpResponse.json(mockPets); |
| 23 | + }), |
| 24 | + // GET with path |
| 25 | + http.get("http://localhost/pet/42", () => { |
| 26 | + return HttpResponse.json({ id: 42, name: "Spot", photoUrls: [], status: "sold" }); |
| 27 | + }), |
| 28 | + // POST with body |
| 29 | + http.post("http://localhost/pet", async ({ request }) => { |
| 30 | + let body: any = await request.json(); |
| 31 | + if (body == null || typeof body !== "object") body = {}; |
| 32 | + return HttpResponse.json({ ...body, id: 99 }); |
| 33 | + }), |
| 34 | + // POST with path and query (no body expected) |
| 35 | + http.post("http://localhost/pet/42", ({ request }) => { |
| 36 | + const url = new URL(request.url); |
| 37 | + const name = url.searchParams.get("name"); |
| 38 | + const status = url.searchParams.get("status"); |
| 39 | + return HttpResponse.json({ name, status }); |
| 40 | + }), |
| 41 | + // DELETE with header |
| 42 | + http.delete("http://localhost/pet/42", ({ request }) => { |
| 43 | + if (request.headers.get("api_key") === "secret") { |
| 44 | + return HttpResponse.text("deleted"); |
| 45 | + } |
| 46 | + return HttpResponse.text("forbidden", { status: 403 }); |
| 47 | + }), |
| 48 | +); |
| 49 | + |
| 50 | +beforeAll(() => server.listen()); |
| 51 | +afterAll(() => server.close()); |
| 52 | + |
| 53 | +describe("minimalist test", () => { |
| 54 | + it("should fetch /pet/findByStatus and receive mocked pets", async () => { |
| 55 | + const fetcher = (method: string, url: string) => fetch(url, { method }); |
| 56 | + const mini = createApiClient(fetcher, "http://localhost"); |
| 57 | + const result = await mini.get("/pet/findByStatus", { query: {} }); |
| 58 | + expect(result).toEqual(mockPets); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("Generated Query Client (runtime)", () => { |
| 63 | + beforeAll(() => { |
| 64 | + api.baseUrl = "http://localhost"; |
| 65 | + }); |
| 66 | + |
| 67 | + it("should fetch /pet/findByStatus and receive mocked pets", async () => { |
| 68 | + const result = await api.get("/pet/findByStatus", { query: {} }); |
| 69 | + expect(result).toEqual(mockPets); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should fetch /pet/:petId with path param", async () => { |
| 73 | + const result = await api.get("/pet/{petId}", { path: { petId: 42 } }); |
| 74 | + expect(result).toEqual({ id: 42, name: "Spot", photoUrls: [], status: "sold" }); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should post /pet with body param", async () => { |
| 78 | + const newPet = { name: "Rex", photoUrls: ["img.jpg"] }; |
| 79 | + const result = await api.post("/pet", { body: newPet }); |
| 80 | + expect(result).toMatchObject(newPet); |
| 81 | + expect(result.id).toBe(99); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should post /pet/:petId with path and query params", async () => { |
| 85 | + const result = await api.post("/pet/{petId}", { |
| 86 | + path: { petId: 42 }, |
| 87 | + query: { name: "Buddy", status: "pending" }, |
| 88 | + }); |
| 89 | + expect(result).toEqual({ name: "Buddy", status: "pending" }); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should delete /pet/:petId with header param", async () => { |
| 93 | + const result = await api.delete("/pet/{petId}", { |
| 94 | + path: { petId: 42 }, |
| 95 | + header: { api_key: "secret" }, |
| 96 | + }); |
| 97 | + expect(result).toBe("deleted"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should use .request to get a Response object", async () => { |
| 101 | + const res = await api.request("get", "/pet/findByStatus", { query: {} }); |
| 102 | + expect(res.status).toBe(200); |
| 103 | + const data = await res.json(); |
| 104 | + expect(data).toEqual(mockPets); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should use .request to post and get a Response object", async () => { |
| 108 | + const newPet = { name: "Tiger", photoUrls: [] }; |
| 109 | + const res = await api.request("post", "/pet", { body: newPet }); |
| 110 | + expect(res.status).toBe(200); |
| 111 | + const data = await res.json(); |
| 112 | + expect(data).toMatchObject(newPet); |
| 113 | + expect(data.id).toBe(99); |
| 114 | + }); |
| 115 | + |
| 116 | + it("should throw and handle error for forbidden delete", async () => { |
| 117 | + api.baseUrl = "http://localhost"; |
| 118 | + await expect( |
| 119 | + api.delete("/pet/{petId}", { |
| 120 | + path: { petId: 42 }, |
| 121 | + header: { api_key: "wrong" }, |
| 122 | + }), |
| 123 | + ).rejects.toMatchObject({ |
| 124 | + message: expect.stringContaining("403"), |
| 125 | + status: 403, |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments