|
| 1 | +import { afterAll, beforeAll, describe, it } from "vitest"; |
| 2 | +import createClient from "../../src/index.js"; |
| 3 | +import { server, baseUrl, useMockRequestHandler } from "../fixtures/mock-server.js"; |
| 4 | +import type { paths } from "../fixtures/api.js"; |
| 5 | + |
| 6 | +beforeAll(() => { |
| 7 | + server.listen({ |
| 8 | + onUnhandledRequest: (request) => { |
| 9 | + throw new Error(`No request handler found for ${request.method} ${request.url}`); |
| 10 | + }, |
| 11 | + }); |
| 12 | +}); |
| 13 | + |
| 14 | +afterEach(() => server.resetHandlers()); |
| 15 | + |
| 16 | +afterAll(() => server.close()); |
| 17 | + |
| 18 | +describe("client", () => { |
| 19 | + describe("TypeScript checks", () => { |
| 20 | + describe("params", () => { |
| 21 | + it("is optional if no parameters are defined", async () => { |
| 22 | + const client = createClient<paths>({ |
| 23 | + baseUrl, |
| 24 | + }); |
| 25 | + |
| 26 | + useMockRequestHandler({ |
| 27 | + baseUrl, |
| 28 | + method: "get", |
| 29 | + path: "/self", |
| 30 | + status: 200, |
| 31 | + body: { message: "OK" }, |
| 32 | + }); |
| 33 | + |
| 34 | + // assert no type error |
| 35 | + await client.GET("/self"); |
| 36 | + |
| 37 | + // assert no type error with empty params |
| 38 | + await client.GET("/self", { params: {} }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("checks types of optional params", async () => { |
| 42 | + const client = createClient<paths>({ |
| 43 | + baseUrl, |
| 44 | + }); |
| 45 | + |
| 46 | + useMockRequestHandler({ |
| 47 | + baseUrl, |
| 48 | + method: "get", |
| 49 | + path: "/self", |
| 50 | + status: 200, |
| 51 | + body: { message: "OK" }, |
| 52 | + }); |
| 53 | + |
| 54 | + // assert no type error with no params |
| 55 | + await client.GET("/blogposts"); |
| 56 | + |
| 57 | + // assert no type error with empty params |
| 58 | + await client.GET("/blogposts", { params: {} }); |
| 59 | + |
| 60 | + // expect error on incorrect param type |
| 61 | + // @ts-expect-error |
| 62 | + await client.GET("/blogposts", { params: { query: { published: "yes" } } }); |
| 63 | + |
| 64 | + // expect error on extra params |
| 65 | + // @ts-expect-error |
| 66 | + await client.GET("/blogposts", { params: { query: { fake: true } } }); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe("body", () => { |
| 71 | + it("requires necessary requestBodies", async () => { |
| 72 | + const client = createClient<paths>({ baseUrl }); |
| 73 | + |
| 74 | + useMockRequestHandler({ |
| 75 | + baseUrl, |
| 76 | + method: "put", |
| 77 | + path: "/blogposts", |
| 78 | + }); |
| 79 | + |
| 80 | + // expect error on missing `body` |
| 81 | + // @ts-expect-error |
| 82 | + await client.PUT("/blogposts"); |
| 83 | + |
| 84 | + // expect error on missing fields |
| 85 | + // @ts-expect-error |
| 86 | + await client.PUT("/blogposts", { body: { title: "Foo" } }); |
| 87 | + |
| 88 | + // (no error) |
| 89 | + await client.PUT("/blogposts", { |
| 90 | + body: { |
| 91 | + title: "Foo", |
| 92 | + body: "Bar", |
| 93 | + publish_date: new Date("2023-04-01T12:00:00Z").getTime(), |
| 94 | + }, |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it("requestBody with required: false", async () => { |
| 99 | + const client = createClient<paths>({ baseUrl }); |
| 100 | + |
| 101 | + useMockRequestHandler({ |
| 102 | + baseUrl, |
| 103 | + method: "put", |
| 104 | + path: "/blogposts-optional", |
| 105 | + status: 201, |
| 106 | + }); |
| 107 | + |
| 108 | + // assert missing `body` doesn’t raise a TS error |
| 109 | + await client.PUT("/blogposts-optional"); |
| 110 | + |
| 111 | + // assert error on type mismatch |
| 112 | + // @ts-expect-error |
| 113 | + await client.PUT("/blogposts-optional", { body: { error: true } }); |
| 114 | + |
| 115 | + // (no error) |
| 116 | + await client.PUT("/blogposts-optional", { |
| 117 | + body: { |
| 118 | + title: "", |
| 119 | + publish_date: 3, |
| 120 | + body: "", |
| 121 | + }, |
| 122 | + }); |
| 123 | + }); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments