Skip to content

Commit

Permalink
Merge pull request #92 from pmcelhaney/test-for-schema-coder
Browse files Browse the repository at this point in the history
unit test for JsonSchemaCoder
  • Loading branch information
pmcelhaney committed Jul 18, 2022
2 parents fca37e9 + 66874e7 commit 40788fd
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/typescript-generator/schema-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ export class SchemaCoder extends Coder {
return this.arraySchema(script);
}

if (["integer", "number"].includes(type)) {
return JSON.stringify(
scrubSchema({ ...this.requirement.data, type: "number" })
);
}

return JSON.stringify(scrubSchema(this.requirement.data));
}
}
124 changes: 124 additions & 0 deletions test/typescript-generator/schema-coder.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import prettier from "prettier";

import { SchemaCoder } from "../../src/typescript-generator/schema-coder.js";
import { Requirement } from "../../src/typescript-generator/requirement.js";

function format(code) {
return prettier.format(code, { parser: "typescript" });
}

describe("a SchemaCoder", () => {
it("generates a list of potential names", () => {
const coder = new SchemaCoder(
new Requirement({ $ref: "/components/schemas/Example" })
);

const [one, two, three] = coder.names();

expect([one, two, three]).toStrictEqual([
"ExampleSchema",
"ExampleSchema2",
"ExampleSchema3",
]);
});

it("when given a $ref, creates an import", () => {
const coder = new SchemaCoder(
new Requirement({ $ref: "/components/schemas/Example" })
);

const script = {
import(c) {
this.importedCoder = c;

return "aVariable";
},
};

const result = coder.write(script);

expect(script.importedCoder).toBe(coder);
expect(result).toBe("aVariable");
});

it.each`
type | output
${"string"} | ${'{"type":"string"}'}
${"number"} | ${'{"type":"number"}'}
${"integer"} | ${'{"type":"number"}'}
`("generates a type declaration for $type", ({ type, output }) => {
const coder = new SchemaCoder(
new Requirement({ type, xml: "should be ignored" })
);
const result = coder.write({});

expect(result).toBe(output);
});

it("generates a type declaration for an object", () => {
const coder = new SchemaCoder(
new Requirement({
type: "object",

properties: {
name: { type: "string" },
age: { type: "integer" },
},

xml: "should be ignored",
})
);

const expected = format(`const x = {
type: "object",
required: [],
properties: { "name": {"type":"string"}, "age": {"type":"number"} }
}`);

expect(format(`const x = ${coder.write()}`)).toStrictEqual(expected);
});

it("generates a type declaration for an array", () => {
const coder = new SchemaCoder(
new Requirement({
type: "array",
items: { type: "string" },
xml: "should be ignored",
})
);

const expected = format(
`const x = {
type: "array",
items: { type: "string" }
};`
);

expect(format(`const x = ${coder.write()}`)).toStrictEqual(expected);
});

it("has type JSONSchema6", () => {
const coder = new SchemaCoder(
new Requirement({
type: "array",
items: { type: "string" },
xml: "should be ignored",
})
);

const script = {
importExternalType(name, path) {
this.imported = { name, path };

return name;
},
};

expect(coder.typeDeclaration(undefined, script)).toBe("JSONSchema6");

expect(script.imported).toStrictEqual({
name: "JSONSchema6",
path: "json-schema",
});
});
});

0 comments on commit 40788fd

Please sign in to comment.