-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathopenapi.test.ts
More file actions
189 lines (162 loc) · 5.05 KB
/
openapi.test.ts
File metadata and controls
189 lines (162 loc) · 5.05 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* ============================================================================
* Copyright (c) Palo Alto Networks
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* ========================================================================== */
import path from "path";
// eslint-disable-next-line import/no-extraneous-dependencies
import { posixPath } from "@docusaurus/utils";
import { readOpenapiFiles } from ".";
import { processOpenapiFile } from "./openapi";
import type { APIOptions, SidebarOptions } from "../types";
// npx jest packages/docusaurus-plugin-openapi/src/openapi/openapi.test.ts --watch
describe("openapi", () => {
describe("readOpenapiFiles", () => {
it("readOpenapiFiles", async () => {
const results = await readOpenapiFiles(
posixPath(path.join(__dirname, "__fixtures__/examples"))
);
const categoryMeta = results.find((x) =>
x.source.endsWith("_category_.json")
);
expect(categoryMeta).toBeFalsy();
// console.log(results);
const yaml = results.find((x) => x.source.endsWith("openapi.yaml"));
expect(yaml).toBeTruthy();
expect(yaml?.sourceDirName).toBe(".");
expect(yaml?.data.tags).toBeDefined();
expect(yaml?.data["x-tagGroups"]).toBeDefined();
expect(
yaml?.data.components?.schemas?.HelloString["x-tags"]
).toBeDefined();
});
});
describe("schemasOnly", () => {
it("includes schema metadata when showSchemas is disabled", async () => {
const openapiData = {
openapi: "3.0.0",
info: {
title: "Schema Only",
version: "1.0.0",
},
paths: {
"/ping": {
get: {
summary: "Ping",
responses: {
"200": {
description: "OK",
},
},
},
},
},
components: {
schemas: {
WithoutTags: {
title: "Without Tags",
type: "object",
properties: {
value: {
type: "string",
},
},
},
},
},
};
const options: APIOptions = {
specPath: "dummy", // required by the type but unused in this context
outputDir: "build",
showSchemas: false,
schemasOnly: true,
};
const sidebarOptions = {} as SidebarOptions;
const [items] = await processOpenapiFile(
openapiData as any,
options,
sidebarOptions
);
const schemaItems = items.filter((item) => item.type === "schema");
expect(schemaItems).toHaveLength(1);
expect(schemaItems[0].id).toBe("without-tags");
});
});
describe("specInfoPagePath", () => {
it("uses default kebab-cased info.title when specInfoPagePath is not provided", async () => {
const openapiData = {
openapi: "3.0.0",
info: {
title: "Company Foo",
version: "1.0.0",
description: "Test API",
},
paths: {},
};
const options: APIOptions = {
specPath: "dummy",
outputDir: "build",
};
const sidebarOptions = {} as SidebarOptions;
const [items] = await processOpenapiFile(
openapiData as any,
options,
sidebarOptions
);
const infoItem = items.find((item) => item.type === "info");
expect(infoItem).toBeDefined();
expect(infoItem?.id).toBe("company-foo");
});
it("uses custom specInfoPagePath when provided", async () => {
const openapiData = {
openapi: "3.0.0",
info: {
title: "Company Foo",
version: "1.0.0",
description: "Test API",
},
paths: {},
};
const options: APIOptions = {
specPath: "dummy",
outputDir: "build",
specInfoPagePath: "custom-api-intro",
};
const sidebarOptions = {} as SidebarOptions;
const [items] = await processOpenapiFile(
openapiData as any,
options,
sidebarOptions
);
const infoItem = items.find((item) => item.type === "info");
expect(infoItem).toBeDefined();
expect(infoItem?.id).toBe("custom-api-intro");
});
it("kebab-cases custom specInfoPagePath", async () => {
const openapiData = {
openapi: "3.0.0",
info: {
title: "Company Foo",
version: "1.0.0",
description: "Test API",
},
paths: {},
};
const options: APIOptions = {
specPath: "dummy",
outputDir: "build",
specInfoPagePath: "Custom API Introduction",
};
const sidebarOptions = {} as SidebarOptions;
const [items] = await processOpenapiFile(
openapiData as any,
options,
sidebarOptions
);
const infoItem = items.find((item) => item.type === "info");
expect(infoItem).toBeDefined();
expect(infoItem?.id).toBe("custom-api-introduction");
});
});
});