|
1 | | -import { describe, it, expect } from "bun:test"; |
| 1 | +import { describe, it, expect, mock } from "bun:test"; |
2 | 2 | import { createApp } from "../app"; |
3 | 3 | import { createTestAppClient } from "../testing"; |
4 | 4 | import type { App } from "../types"; |
5 | 5 | import type { GetClientRoutes } from "../client"; |
6 | 6 | import { expectTypeOf } from "expect-type"; |
7 | 7 | import { z } from "zod/v4"; |
| 8 | +import { HttpStatus } from "../status"; |
| 9 | +import { ErrorResponse, NoResponse } from "../custom-responses"; |
| 10 | + |
| 11 | +// Silence console.error logs |
| 12 | +globalThis.console.error = mock(); |
8 | 13 |
|
9 | 14 | describe("Client", () => { |
10 | 15 | describe("inputs", () => { |
@@ -89,6 +94,50 @@ describe("Client", () => { |
89 | 94 | }); |
90 | 95 | }); |
91 | 96 |
|
| 97 | + describe("response types", () => { |
| 98 | + describe("single response", () => { |
| 99 | + it("should return the responses type", async () => { |
| 100 | + const app = createApp().get( |
| 101 | + "/health", |
| 102 | + { responses: z.object({ status: z.string() }) }, |
| 103 | + () => ({ status: "ok" }), |
| 104 | + ); |
| 105 | + const client = createTestAppClient(app); |
| 106 | + |
| 107 | + const response = await client.fetch("GET", "/health", {}); |
| 108 | + expectTypeOf(response).toEqualTypeOf<{ status: string }>(); |
| 109 | + expect(response).toEqual({ status: "ok" }); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe("multiple responses", () => { |
| 114 | + it("should return a union of the 2XX responses", async () => { |
| 115 | + const app = createApp().put( |
| 116 | + "/users/:id", |
| 117 | + { |
| 118 | + params: z.object({ id: z.string() }), |
| 119 | + responses: { |
| 120 | + [200]: z.object({ id: z.string(), name: z.string() }), |
| 121 | + [HttpStatus.NoContent]: NoResponse, |
| 122 | + [HttpStatus.BadRequest]: ErrorResponse, |
| 123 | + }, |
| 124 | + }, |
| 125 | + ({ status }) => status(HttpStatus.NoContent, undefined), |
| 126 | + ); |
| 127 | + const client = createTestAppClient(app); |
| 128 | + |
| 129 | + const response = await client.fetch("PUT", "/users/:id", { |
| 130 | + params: { id: "123" }, |
| 131 | + }); |
| 132 | + expectTypeOf(response).toEqualTypeOf<void | { |
| 133 | + id: string; |
| 134 | + name: string; |
| 135 | + }>(); |
| 136 | + expect(response).toEqual(undefined); |
| 137 | + }); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
92 | 141 | describe("GetClientRoutes", () => { |
93 | 142 | it("should convert app data with prefix to absolute routes", () => { |
94 | 143 | type MyApp = App<{ |
|
0 commit comments