Skip to content

Commit 4a7d7f9

Browse files
authored
test: add some missing unit tests (#387)
adds a couple of missing unit tests, split from #386
1 parent 70d0f9a commit 4a7d7f9

File tree

12 files changed

+91
-20
lines changed

12 files changed

+91
-20
lines changed

packages/openapi-code-generator/src/core/dependency-graph.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import {describe, expect, it} from "@jest/globals"
22
import {testVersions, unitTestInput} from "../test/input.test-utils"
33
import {buildDependencyGraph} from "./dependency-graph"
4-
import {getSchemaNameFromRef} from "./openapi-utils"
4+
import {getNameFromRef} from "./openapi-utils"
55

66
describe.each(testVersions)("%s - core/dependency-graph", (version) => {
77
it("works", async () => {
88
const {input} = await unitTestInput(version)
99

10-
const graph = buildDependencyGraph(input, getSchemaNameFromRef)
10+
const graph = buildDependencyGraph(input, (it) => getNameFromRef(it, "s_"))
1111

1212
expect(graph.order.indexOf("s_Ordering")).toBeGreaterThan(
1313
graph.order.indexOf("s_AOrdering"),

packages/openapi-code-generator/src/core/loaders/tsconfig.loader.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {describe, expect, it} from "@jest/globals"
1+
import {describe, expect, it, jest} from "@jest/globals"
22
import {WebFsAdaptor} from "../file-system/web-fs-adaptor"
33
import {loadTsConfigCompilerOptions} from "./tsconfig.loader"
44

@@ -98,4 +98,30 @@ describe("core/loaders/tsconfig.loader", () => {
9898
rewriteRelativeImportExtensions: true,
9999
})
100100
})
101+
102+
it("falls back to defaults if an exception occurs", async () => {
103+
const fs = fsAdaptor({
104+
"/virtual/ws/tsconfig.json": JSON.stringify({
105+
compilerOptions: {
106+
exactOptionalPropertyTypes: true,
107+
},
108+
}),
109+
})
110+
111+
const spy = jest
112+
.spyOn(fs, "readFile")
113+
.mockRejectedValue(new Error("EACCES: permission denied"))
114+
115+
const actual = await loadTsConfigCompilerOptions(
116+
"/virtual/ws/packages/pkg",
117+
fs,
118+
)
119+
120+
expect(actual).toEqual({
121+
exactOptionalPropertyTypes: false,
122+
rewriteRelativeImportExtensions: false,
123+
})
124+
125+
expect(spy).toHaveBeenCalled()
126+
})
101127
})

packages/openapi-code-generator/src/core/openapi-loader.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {describe, expect, it} from "@jest/globals"
22
import {normalizeRef, pathFromRef} from "./openapi-loader"
33

44
describe("core/openapi-loader", () => {
5-
describe("normalizeRef", () => {
5+
describe("#normalizeRef", () => {
66
it("rejects invalid $refs", () => {
77
expect(() => normalizeRef("not a ref", "/some/path")).toThrow(
88
/invalid \$ref '.+'/,

packages/openapi-code-generator/src/core/openapi-utils.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {describe, expect, it} from "@jest/globals"
2-
import {getNameFromRef, isRef} from "./openapi-utils"
2+
import {extractPlaceholders, getNameFromRef, isRef} from "./openapi-utils"
33

44
describe("core/openapi-utils", () => {
55
describe("#isRef", () => {
@@ -10,6 +10,11 @@ describe("core/openapi-utils", () => {
1010
it("returns false if $ref is not defined", () => {
1111
expect(isRef({type: "number"})).toBe(false)
1212
})
13+
14+
it("returns false if not passed an object", () => {
15+
expect(isRef(null)).toBe(false)
16+
expect(isRef(123)).toBe(false)
17+
})
1318
})
1419

1520
describe("#getNameFromRef", () => {
@@ -30,5 +35,29 @@ describe("core/openapi-utils", () => {
3035
"t_Foo_Bar",
3136
)
3237
})
38+
39+
it("throws on an invalid $ref", () => {
40+
expect(() => getNameFromRef({$ref: "#/"}, "t_")).toThrow(
41+
"no name found in $ref: '#/'",
42+
)
43+
})
44+
})
45+
46+
describe("#extractPlaceholders", () => {
47+
it("returns an empty array if no placeholders in input", () => {
48+
expect(extractPlaceholders("/foo/bar")).toStrictEqual([])
49+
})
50+
51+
it("extracts valid placeholders", () => {
52+
expect(extractPlaceholders("/{foo/{id}/bar}/{type}")).toStrictEqual([
53+
{
54+
placeholder: "id",
55+
wholeString: "{id}",
56+
},
57+
{placeholder: "type", wholeString: "{type}"},
58+
])
59+
})
60+
61+
// todo: expand tests for special characters / escaping
3362
})
3463
})

packages/openapi-code-generator/src/core/openapi-utils.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,3 @@ export function extractPlaceholders(
3131
placeholder: match[1],
3232
}))
3333
}
34-
35-
export function getSchemaNameFromRef(reference: Reference) {
36-
return getNameFromRef(reference, "s_")
37-
}

packages/openapi-code-generator/src/core/openapi-validator.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe("core/openapi-validator", () => {
5454
})
5555
})
5656

57-
describe.skip("openapi 3.1", () => {
57+
describe("openapi 3.1", () => {
5858
it("should accept a valid specification", async () => {
5959
const validator = await OpenapiValidator.create()
6060
await expect(
@@ -89,7 +89,7 @@ describe("core/openapi-validator", () => {
8989
).resolves.toBeUndefined()
9090
})
9191

92-
it("should reject an invalid specification", async () => {
92+
it.skip("should reject an invalid specification", async () => {
9393
const validator = await OpenapiValidator.create()
9494
await expect(
9595
validator.validate(

packages/openapi-code-generator/src/core/openapi-validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export class OpenapiValidator {
2222
return this.validate3_1
2323
}
2424

25+
// todo: openapi 3.2: add validator
26+
2527
throw new Error(`unsupported openapi version '${version}'`)
2628
}
2729

packages/openapi-code-generator/src/core/utils.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {describe, expect, it} from "@jest/globals"
22
import {
33
camelCase,
44
coalesce,
5+
deepEqual,
56
hasSingleElement,
67
isDefined,
78
isHttpMethod,
@@ -46,6 +47,21 @@ describe("core/utils", () => {
4647
})
4748
})
4849

50+
describe("#deepEqual", () => {
51+
it("works for primitives", () => {
52+
expect(deepEqual(2, 2)).toBe(true)
53+
expect(deepEqual(1, 2)).toBe(false)
54+
})
55+
it("works for array", () => {
56+
expect(deepEqual([1], [1])).toBe(true)
57+
expect(deepEqual([1], [2])).toBe(false)
58+
})
59+
it("works for object", () => {
60+
expect(deepEqual({foo: "bar"}, {foo: "bar"})).toBe(true)
61+
expect(deepEqual({foo: "bar"}, {foo: "baz"})).toBe(false)
62+
})
63+
})
64+
4965
describe("#coalesce", () => {
5066
it("returns the first defined parameter", () => {
5167
expect(coalesce(null, undefined, 1, 2)).toBe(1)

packages/openapi-code-generator/src/typescript/common/schema-builders/abstract-schema-builder.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
IRParameter,
1616
MaybeIRModel,
1717
} from "../../../core/openapi-types-normalized"
18-
import {getSchemaNameFromRef, isRef} from "../../../core/openapi-utils"
18+
import {getNameFromRef, isRef} from "../../../core/openapi-utils"
1919
import {hasSingleElement} from "../../../core/utils"
2020
import {CompilationUnit, type ICompilable} from "../compilation-units"
2121
import type {ImportBuilder} from "../import-builder"
@@ -51,7 +51,8 @@ export abstract class AbstractSchemaBuilder<
5151
private readonly parent?: SubClass,
5252
) {
5353
this.graph =
54-
parent?.graph ?? buildDependencyGraph(this.input, getSchemaNameFromRef)
54+
parent?.graph ??
55+
buildDependencyGraph(this.input, (it) => this.getSchemaNameFromRef(it))
5556
this.importHelpers(this.schemaBuilderImports)
5657
this.typeBuilder = typeBuilder.withImports(this.schemaBuilderImports)
5758
}
@@ -61,7 +62,7 @@ export abstract class AbstractSchemaBuilder<
6162
private add(reference: Reference): string {
6263
this.parent?.add(reference)
6364

64-
const name = getSchemaNameFromRef(reference)
65+
const name = this.getSchemaNameFromRef(reference)
6566
this.referenced[name] = reference
6667

6768
if (this.imports) {
@@ -148,6 +149,10 @@ export abstract class AbstractSchemaBuilder<
148149

149150
protected abstract importHelpers(importBuilder: ImportBuilder): void
150151

152+
public getSchemaNameFromRef(reference: Reference) {
153+
return getNameFromRef(reference, "s_")
154+
}
155+
151156
public abstract schemaTypeForType(type: string): string
152157

153158
protected abstract schemaFromRef(reference: Reference): ExportDefinition

packages/openapi-code-generator/src/typescript/common/schema-builders/joi-schema-builder.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type {
88
IRModelString,
99
MaybeIRModel,
1010
} from "../../../core/openapi-types-normalized"
11-
import {getSchemaNameFromRef} from "../../../core/openapi-utils"
1211
import {hasSingleElement, isDefined} from "../../../core/utils"
1312
import type {ImportBuilder} from "../import-builder"
1413
import type {TypeBuilder} from "../type-builder"
@@ -75,7 +74,7 @@ export class JoiBuilder extends AbstractSchemaBuilder<
7574
}
7675

7776
protected schemaFromRef(reference: Reference): ExportDefinition {
78-
const name = getSchemaNameFromRef(reference)
77+
const name = this.getSchemaNameFromRef(reference)
7978
const schemaObject = this.input.schema(reference)
8079

8180
const value = this.fromModel(schemaObject, true)

0 commit comments

Comments
 (0)