|
| 1 | +import { describe, expect, it } from "@jest/globals"; |
| 2 | +import injectSchemas from "./injectSchemas"; |
| 3 | + |
| 4 | +describe("injectSchemas", () => { |
| 5 | + it("should inject the schema name into the code", () => { |
| 6 | + const code = ` |
| 7 | + const { GET } = defineRoute({ |
| 8 | + requestBody: User, |
| 9 | + }); |
| 10 | + `; |
| 11 | + const output = injectSchemas(code, "User"); |
| 12 | + expect(output).toBe(` |
| 13 | + const { GET } = defineRoute({ |
| 14 | + requestBody: "User", |
| 15 | + }); |
| 16 | + `); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should handle zod helper functions", () => { |
| 20 | + const code = ` |
| 21 | + const { GET } = defineRoute({ |
| 22 | + requestBody: User.pick({ id: true }), |
| 23 | + }); |
| 24 | + `; |
| 25 | + const output = injectSchemas(code, "User"); |
| 26 | + expect(output).toBe(` |
| 27 | + const { GET } = defineRoute({ |
| 28 | + requestBody: global.schemas["User"].pick({ id: true }), |
| 29 | + }); |
| 30 | + `); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should leave the strings alone", () => { |
| 34 | + const code = ` |
| 35 | + const { GET } = defineRoute({ |
| 36 | + requestBody: User.pick({ id: true }), |
| 37 | + responses: { |
| 38 | + 200: { description: "User. found", content: User }, |
| 39 | + 400: { description: 'Bad Request' }, |
| 40 | + 404: { description: \`User not found\` } |
| 41 | + }, |
| 42 | + }); |
| 43 | + `; |
| 44 | + const output = injectSchemas(code, "User"); |
| 45 | + expect(output).toBe(` |
| 46 | + const { GET } = defineRoute({ |
| 47 | + requestBody: global.schemas["User"].pick({ id: true }), |
| 48 | + responses: { |
| 49 | + 200: { description: "User. found", content: "User" }, |
| 50 | + 400: { description: 'Bad Request' }, |
| 51 | + 404: { description: \`User not found\` } |
| 52 | + }, |
| 53 | + }); |
| 54 | + `); |
| 55 | + }); |
| 56 | +}); |
0 commit comments