-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
generate.test.ts
148 lines (120 loc) · 4.68 KB
/
generate.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import ApiGenerator, { getOperationName, getFormatter } from "./generate";
import { printAst } from "./index";
import SwaggerParser from "@apidevtools/swagger-parser";
import { OpenAPIV3 } from "openapi-types";
describe("getOperationName", () => {
it("should use the id", () => {
expect(getOperationName("GET", "/pets", "list pets")).toEqual("listPets");
});
it("should use the verb and path", () => {
expect(getOperationName("GET", "/pets/{color}/{status}")).toEqual(
"getPetsByColorAndStatus"
);
});
it("should not use ids with special chars", () => {
expect(
getOperationName("GET", "/pets", "API\\PetController::listPetAction")
).toEqual("getPets");
});
});
describe("generate", () => {
let artefact: string;
let spec: { petstore: OpenAPIV3.Document; allOf: OpenAPIV3.Document };
beforeAll(async () => {
spec = {
petstore: (await SwaggerParser.bundle(
__dirname + "/../../demo/petstore.json"
)) as any,
allOf: (await SwaggerParser.bundle(
__dirname + "/../../demo/allOf.json"
)) as any,
};
});
it("should generate an api", async () => {
artefact = printAst(new ApiGenerator(spec.petstore).generateApi());
});
/* https://github.com/cotype/build-client/issues/5 */
it("should generate same api a second time", async () => {
expect(printAst(new ApiGenerator(spec.petstore).generateApi())).toBe(
artefact
);
});
it("should handle properties both inside and outside of allOf", async () => {
const api = printAst(new ApiGenerator(spec.allOf).generateApi());
const oneLine = api.replace(/\s+/g, " ");
const circleTypeDefinition =
"export type Circle = Shape & { radius?: number; } & { circumference?: number; };";
expect(oneLine).toContain(circleTypeDefinition);
});
it("should handle enums as union types", async () => {
const api = printAst(new ApiGenerator(spec.petstore).generateApi());
const oneLine = api.replace(/\s+/g, " ");
const enumTypeDefinition = `export type Option = ("one" | "two" | "three")[];`;
expect(oneLine).toContain(enumTypeDefinition);
});
it("should create real enums", async () => {
const api = printAst(
new ApiGenerator(spec.petstore, { useEnumType: true }).generateApi()
);
const oneLine = api.replace(/\s+/g, " ");
// Enum type string
const enumDefinition = `export enum Status { Available = "Available", Pending = "Pending", Sold = "Sold" }`;
expect(oneLine).toContain(enumDefinition);
// Enum type number
const enumNumberDefinition = `export enum Size { P = 0, M = 1, G = 2 }`;
expect(oneLine).toContain(enumNumberDefinition);
// Enums with different values but same name should result in a different instance name
const sameNameDefinition = `export enum Status2 { Placed = "Placed", Approved = "Approved", Delivered = "Delivered" }`;
expect(oneLine).toContain(sameNameDefinition);
});
});
describe("generate with application/geo+json", () => {
let spec: OpenAPIV3.Document;
beforeAll(async () => {
spec = (await SwaggerParser.bundle(
__dirname + "/../../demo/geojson.json"
)) as any;
});
it("should generate an api", async () => {
const artefact = printAst(new ApiGenerator(spec).generateApi());
const oneLine = artefact.replace(/\s+/g, " ");
expect(oneLine).toContain(
'return oazapfts.fetchJson<{ status: 200; data: FeatureCollection; }>("/geojson", { ...opts });'
);
});
});
describe("generate with blob download", () => {
let spec: OpenAPIV3.Document;
beforeAll(async () => {
spec = (await SwaggerParser.bundle(
__dirname + "/../../demo/binary.json"
)) as any;
});
it("should generate an api using fetchBlob", async () => {
const artefact = printAst(new ApiGenerator(spec).generateApi());
const oneLine = artefact.replace(/\s+/g, " ");
expect(oneLine).toContain(
"return oazapfts.fetchBlob<{ status: 200; data: Blob; }>(`/file/${encodeURIComponent(fileId)}/download`, { ...opts });"
);
});
});
describe("generate with const", () => {
let spec: OpenAPIV3.Document;
beforeAll(async () => {
spec = (await SwaggerParser.bundle(
__dirname + "/../../demo/const.json"
)) as any;
});
it("should generate an api with literal type set to const value", async () => {
const artefact = printAst(new ApiGenerator(spec).generateApi());
const oneLine = artefact.replace(/\s+/g, " ");
const constTypeDefinition = `export type Shape = "circle";`;
expect(oneLine).toContain(constTypeDefinition);
});
});
// // https://spec.openapis.org/oas/v3.0.3#style-examples
// describe('getFormatter', () => {
// it('formats correctly', () => {
// const formatter = getFormatter()
// })
// })