|
| 1 | +import { type Model } from "@typespec/compiler"; |
| 2 | +import { t } from "@typespec/compiler/testing"; |
| 3 | +import { describe, expect, it, beforeEach } from "vitest"; |
| 4 | +import { InputType } from "../../src/components/types/index.js"; |
| 5 | +import { Tester } from "../test-host.js"; |
| 6 | +import { renderComponentToSDL } from "./component-test-utils.js"; |
| 7 | + |
| 8 | +describe("InputType component", () => { |
| 9 | + let tester: Awaited<ReturnType<typeof Tester.createInstance>>; |
| 10 | + beforeEach(async () => { |
| 11 | + tester = await Tester.createInstance(); |
| 12 | + }); |
| 13 | + |
| 14 | + it("renders a basic input type with fields", async () => { |
| 15 | + const { CreateUser } = await tester.compile( |
| 16 | + t.code`model ${t.model("CreateUser")} { name: string; email: string; }`, |
| 17 | + ); |
| 18 | + |
| 19 | + const sdl = renderComponentToSDL(tester.program, <InputType type={CreateUser} />); |
| 20 | + |
| 21 | + expect(sdl).toContain("input CreateUser {"); |
| 22 | + expect(sdl).toContain("name: String!"); |
| 23 | + expect(sdl).toContain("email: String!"); |
| 24 | + }); |
| 25 | + |
| 26 | + it("renders with doc comment description", async () => { |
| 27 | + const { LoginInput } = await tester.compile( |
| 28 | + t.code` |
| 29 | + /** Credentials for login */ |
| 30 | + model ${t.model("LoginInput")} { username: string; password: string; } |
| 31 | + `, |
| 32 | + ); |
| 33 | + |
| 34 | + const sdl = renderComponentToSDL(tester.program, <InputType type={LoginInput} />); |
| 35 | + |
| 36 | + expect(sdl).toContain("Credentials for login"); |
| 37 | + expect(sdl).toContain("input LoginInput {"); |
| 38 | + }); |
| 39 | + |
| 40 | + it("renders optional fields as non-null (GraphQL input convention)", async () => { |
| 41 | + // In GraphQL, input fields are always non-null; optionality is expressed |
| 42 | + // via default values, not nullability. Only `| null` makes them nullable. |
| 43 | + const { UpdateUser } = await tester.compile( |
| 44 | + t.code`model ${t.model("UpdateUser")} { name?: string; bio?: string; }`, |
| 45 | + ); |
| 46 | + |
| 47 | + const sdl = renderComponentToSDL(tester.program, <InputType type={UpdateUser} />); |
| 48 | + |
| 49 | + expect(sdl).toContain("name: String!"); |
| 50 | + expect(sdl).toContain("bio: String!"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("appends Input suffix when model has an output variant", async () => { |
| 54 | + const { Pet } = await tester.compile( |
| 55 | + t.code`model ${t.model("Pet")} { name: string; }`, |
| 56 | + ); |
| 57 | + |
| 58 | + const sdl = renderComponentToSDL(tester.program, <InputType type={Pet} />, { |
| 59 | + modelVariants: { |
| 60 | + outputModels: new Map([["Pet", Pet]]), |
| 61 | + inputModels: new Map([["Pet", Pet]]), |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + expect(sdl).toContain("input PetInput {"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("uses original name when no output variant exists", async () => { |
| 69 | + const { CreatePet } = await tester.compile( |
| 70 | + t.code`model ${t.model("CreatePet")} { name: string; }`, |
| 71 | + ); |
| 72 | + |
| 73 | + const sdl = renderComponentToSDL(tester.program, <InputType type={CreatePet} />); |
| 74 | + |
| 75 | + expect(sdl).toContain("input CreatePet {"); |
| 76 | + expect(sdl).not.toContain("CreatePetInput"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("renders array fields as list types", async () => { |
| 80 | + const { TagInput } = await tester.compile( |
| 81 | + t.code`model ${t.model("TagInput")} { values: string[]; }`, |
| 82 | + ); |
| 83 | + |
| 84 | + const sdl = renderComponentToSDL(tester.program, <InputType type={TagInput} />); |
| 85 | + |
| 86 | + expect(sdl).toContain("values: [String!]!"); |
| 87 | + }); |
| 88 | +}); |
0 commit comments