From ffb878b60436f449b5a0ac4478298162d0db4ac9 Mon Sep 17 00:00:00 2001 From: scarf Date: Sun, 2 Jul 2023 16:14:28 +0900 Subject: [PATCH] test: multiline description --- lib/tests/description-in-zod.test.ts | 124 +++++++++++++++++++-------- 1 file changed, 89 insertions(+), 35 deletions(-) diff --git a/lib/tests/description-in-zod.test.ts b/lib/tests/description-in-zod.test.ts index 56344d0..d9301b3 100644 --- a/lib/tests/description-in-zod.test.ts +++ b/lib/tests/description-in-zod.test.ts @@ -2,46 +2,49 @@ import type { OpenAPIObject } from "openapi3-ts"; import { expect, test } from "vitest"; import { generateZodClientFromOpenAPI } from "../src"; -test("description-in-zod", async () => { - const openApiDoc: OpenAPIObject = { - openapi: "3.0.0", - info: { - version: "1.0.0", - title: "Numerical enums", - }, - paths: { - "/sample": { - get: { - parameters: [ - { - in: "query", - name: "foo", - schema: { - type: "integer", - enum: [1, -2, 3], - }, - description: "foo description", - }, - { - in: "query", - name: "bar", - schema: { - type: "number", - enum: [1.2, 34, -56.789], - }, - description: "bar description", +const openApiDoc: OpenAPIObject = { + openapi: "3.0.0", + info: { + version: "1.0.0", + title: "Numerical enums", + }, + paths: { + "/sample": { + get: { + parameters: [ + { + in: "query", + name: "foo", + schema: { + type: "integer", + enum: [1, -2, 3], }, - ], - responses: { - "200": { - description: "resoponse", + description: "foo description", + }, + { + in: "query", + name: "bar", + schema: { + type: "number", + enum: [1.2, 34, -56.789], }, + description: `multi + line + bar + description`, + }, + ], + responses: { + "200": { + description: "resoponse", }, }, }, }, - }; + }, +}; +test("description-in-zod", async () => { const output = await generateZodClientFromOpenAPI({ disableWriteToFile: true, openApiDoc, @@ -62,7 +65,7 @@ test("description-in-zod", async () => { type: "Query", schema: z .union([z.literal(1), z.literal(-2), z.literal(3)]) - .describe("foo description") + .describe(\`foo description\`) .optional(), }, { @@ -70,7 +73,7 @@ test("description-in-zod", async () => { type: "Query", schema: z .union([z.literal(1.2), z.literal(34), z.literal(-56.789)]) - .describe("bar description") + .describe(\`multi line bar description\`) .optional(), }, ], @@ -86,3 +89,54 @@ test("description-in-zod", async () => { " `); }); + +test("description-in-zod-multiline", async () => { + const output = await generateZodClientFromOpenAPI({ + disableWriteToFile: true, + openApiDoc, + options: { withDescription: "multiline" }, + }); + expect(output).toMatchInlineSnapshot(` + "import { makeApi, Zodios, type ZodiosOptions } from "@zodios/core"; + import { z } from "zod"; + + const endpoints = makeApi([ + { + method: "get", + path: "/sample", + requestFormat: "json", + parameters: [ + { + name: "foo", + type: "Query", + schema: z + .union([z.literal(1), z.literal(-2), z.literal(3)]) + .describe(\`foo description\`) + .optional(), + }, + { + name: "bar", + type: "Query", + schema: z + .union([z.literal(1.2), z.literal(34), z.literal(-56.789)]) + .describe( + \`multi + line + bar + description\` + ) + .optional(), + }, + ], + response: z.void(), + }, + ]); + + export const api = new Zodios(endpoints); + + export function createApiClient(baseUrl: string, options?: ZodiosOptions) { + return new Zodios(baseUrl, endpoints, options); + } + " + `) +})