|
| 1 | +import { describe, expect, test } from "vitest"; |
| 2 | + |
| 3 | +import { |
| 4 | + InputData, |
| 5 | + JSONSchemaInput, |
| 6 | + quicktype, |
| 7 | +} from "../../packages/quicktype-core/src/index.js"; |
| 8 | + |
| 9 | +describe("TypeScript readonly", () => { |
| 10 | + test("applies to index signatures", async () => { |
| 11 | + const input = new JSONSchemaInput(undefined); |
| 12 | + await input.addSource({ |
| 13 | + name: "TopLevel", |
| 14 | + schema: JSON.stringify({ |
| 15 | + type: "object", |
| 16 | + properties: { fixed: { type: "string" } }, |
| 17 | + required: ["fixed"], |
| 18 | + additionalProperties: { type: "string" }, |
| 19 | + }), |
| 20 | + }); |
| 21 | + const inputData = new InputData(); |
| 22 | + inputData.addInput(input); |
| 23 | + |
| 24 | + const result = await quicktype({ |
| 25 | + inputData, |
| 26 | + lang: "typescript", |
| 27 | + rendererOptions: { readonly: "true" }, |
| 28 | + }); |
| 29 | + |
| 30 | + expect(result.lines.join("\n")).toContain( |
| 31 | + "readonly [property: string]: string;", |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + test("applies to map types", async () => { |
| 36 | + const input = new JSONSchemaInput(undefined); |
| 37 | + await input.addSource({ |
| 38 | + name: "TopLevel", |
| 39 | + schema: JSON.stringify({ |
| 40 | + type: "object", |
| 41 | + additionalProperties: { type: "string" }, |
| 42 | + }), |
| 43 | + }); |
| 44 | + const inputData = new InputData(); |
| 45 | + inputData.addInput(input); |
| 46 | + |
| 47 | + const result = await quicktype({ |
| 48 | + inputData, |
| 49 | + lang: "typescript", |
| 50 | + rendererOptions: { readonly: "true" }, |
| 51 | + }); |
| 52 | + |
| 53 | + expect(result.lines.join("\n")).toContain( |
| 54 | + "{ readonly [key: string]: string }", |
| 55 | + ); |
| 56 | + }); |
| 57 | +}); |
0 commit comments