|
| 1 | +import { t } from "@typespec/compiler/testing"; |
| 2 | +import { describe, expect, it, beforeEach } from "vitest"; |
| 3 | +import { EnumType } from "../../src/components/types/index.js"; |
| 4 | +import { createGraphQLMutationEngine } from "../../src/mutation-engine/index.js"; |
| 5 | +import { Tester } from "../test-host.js"; |
| 6 | +import { renderComponentToSDL } from "./component-test-utils.js"; |
| 7 | + |
| 8 | +describe("EnumType component", () => { |
| 9 | + let tester: Awaited<ReturnType<typeof Tester.createInstance>>; |
| 10 | + beforeEach(async () => { |
| 11 | + tester = await Tester.createInstance(); |
| 12 | + }); |
| 13 | + |
| 14 | + it("renders a basic enum", async () => { |
| 15 | + const { Color } = await tester.compile( |
| 16 | + t.code`enum ${t.enum("Color")} { Red, Green, Blue }`, |
| 17 | + ); |
| 18 | + |
| 19 | + const engine = createGraphQLMutationEngine(tester.program); |
| 20 | + const mutated = engine.mutateEnum(Color).mutatedType; |
| 21 | + |
| 22 | + const sdl = renderComponentToSDL(tester.program, <EnumType type={mutated} />); |
| 23 | + |
| 24 | + expect(sdl).toContain("enum Color {"); |
| 25 | + expect(sdl).toContain("Red"); |
| 26 | + expect(sdl).toContain("Green"); |
| 27 | + expect(sdl).toContain("Blue"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("renders enum with doc comment description", async () => { |
| 31 | + const { Role } = await tester.compile( |
| 32 | + t.code` |
| 33 | + /** The role a user can have */ |
| 34 | + enum ${t.enum("Role")} { Admin, User } |
| 35 | + `, |
| 36 | + ); |
| 37 | + |
| 38 | + const engine = createGraphQLMutationEngine(tester.program); |
| 39 | + const mutated = engine.mutateEnum(Role).mutatedType; |
| 40 | + |
| 41 | + const sdl = renderComponentToSDL(tester.program, <EnumType type={mutated} />); |
| 42 | + |
| 43 | + expect(sdl).toContain("The role a user can have"); |
| 44 | + expect(sdl).toContain("enum Role {"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("renders enum with member descriptions", async () => { |
| 48 | + const { Status } = await tester.compile( |
| 49 | + t.code` |
| 50 | + enum ${t.enum("Status")} { |
| 51 | + /** Currently active */ |
| 52 | + Active, |
| 53 | + /** No longer active */ |
| 54 | + Inactive, |
| 55 | + } |
| 56 | + `, |
| 57 | + ); |
| 58 | + |
| 59 | + const engine = createGraphQLMutationEngine(tester.program); |
| 60 | + const mutated = engine.mutateEnum(Status).mutatedType; |
| 61 | + |
| 62 | + const sdl = renderComponentToSDL(tester.program, <EnumType type={mutated} />); |
| 63 | + |
| 64 | + expect(sdl).toContain("Currently active"); |
| 65 | + expect(sdl).toContain("Active"); |
| 66 | + expect(sdl).toContain("No longer active"); |
| 67 | + expect(sdl).toContain("Inactive"); |
| 68 | + }); |
| 69 | + |
| 70 | + it("renders enum with deprecated members", async () => { |
| 71 | + const { Status } = await tester.compile( |
| 72 | + t.code` |
| 73 | + enum ${t.enum("Status")} { |
| 74 | + Active, |
| 75 | + #deprecated "use Active instead" |
| 76 | + Legacy, |
| 77 | + } |
| 78 | + `, |
| 79 | + ); |
| 80 | + |
| 81 | + const engine = createGraphQLMutationEngine(tester.program); |
| 82 | + const mutated = engine.mutateEnum(Status).mutatedType; |
| 83 | + |
| 84 | + const sdl = renderComponentToSDL(tester.program, <EnumType type={mutated} />); |
| 85 | + |
| 86 | + expect(sdl).toContain("Active"); |
| 87 | + expect(sdl).toContain("Legacy"); |
| 88 | + expect(sdl).toContain("@deprecated"); |
| 89 | + expect(sdl).toContain("use Active instead"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("renders enum with sanitized member names", async () => { |
| 93 | + const { E } = await tester.compile( |
| 94 | + t.code`enum ${t.enum("E")} { \`$val1$\`, \`val-2\` }`, |
| 95 | + ); |
| 96 | + |
| 97 | + const engine = createGraphQLMutationEngine(tester.program); |
| 98 | + const mutated = engine.mutateEnum(E).mutatedType; |
| 99 | + |
| 100 | + const sdl = renderComponentToSDL(tester.program, <EnumType type={mutated} />); |
| 101 | + |
| 102 | + expect(sdl).toContain("_val1_"); |
| 103 | + expect(sdl).toContain("val_2"); |
| 104 | + expect(sdl).not.toContain("$val1$"); |
| 105 | + expect(sdl).not.toContain("val-2"); |
| 106 | + }); |
| 107 | +}); |
0 commit comments