Skip to content

Commit cc2f657

Browse files
tests
1 parent 9946d4b commit cc2f657

28 files changed

+5394
-0
lines changed

test/specs/anchor.spec.js

+223
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
"use strict";
2+
3+
const { JsonSchema, Anchor } = require("../../");
4+
const assert = require("../utils/assert");
5+
6+
describe("Anchor class", () => {
7+
8+
it("should create an Anchor", () => {
9+
let schema = new JsonSchema({ url: "https://example.com/foo/bar/schema.json" });
10+
let anchor = new Anchor({
11+
resource: schema.rootResource,
12+
locationInFile: ["$defs", "person"],
13+
name: "person",
14+
data: {
15+
$anchor: "person",
16+
title: "Person",
17+
properties: {
18+
name: { type: "string" },
19+
},
20+
}
21+
});
22+
23+
assert.anchor(anchor, {
24+
file: schema.rootFile,
25+
resource: schema.rootResource,
26+
name: "person",
27+
uri: new URL("https://example.com/foo/bar/schema.json#person"),
28+
locationInFile: {
29+
tokens: ["$defs", "person"],
30+
path: "/$defs/person",
31+
hash: "#/$defs/person",
32+
},
33+
data: {
34+
$anchor: "person",
35+
title: "Person",
36+
properties: {
37+
name: { type: "string" },
38+
},
39+
}
40+
});
41+
});
42+
43+
it("cannot be called without any arguments", () => {
44+
try {
45+
new Anchor();
46+
assert.failed();
47+
}
48+
catch (error) {
49+
assert.error(error, {
50+
name: "TypeError",
51+
message: /undefined/,
52+
});
53+
}
54+
});
55+
56+
it("should throw an error if no resource is specified", () => {
57+
try {
58+
new Anchor({
59+
locationInFile: [],
60+
name: "my-anchor"
61+
});
62+
assert.failed();
63+
}
64+
catch (error) {
65+
assert.error(error, {
66+
name: "TypeError",
67+
message: /undefined/,
68+
});
69+
}
70+
});
71+
72+
it("should throw an error if no location is specified", () => {
73+
try {
74+
new Anchor({
75+
resource: new JsonSchema({ path: "schema.json" }).rootResource,
76+
name: "my-anchor"
77+
});
78+
assert.failed();
79+
}
80+
catch (error) {
81+
assert.error(error, {
82+
name: "TypeError",
83+
message: /undefined/,
84+
});
85+
}
86+
});
87+
88+
it("should throw an error if no value is specified", () => {
89+
try {
90+
new Anchor({
91+
resource: new JsonSchema({ path: "schema.json" }).rootResource,
92+
locationInFile: [],
93+
});
94+
assert.failed();
95+
}
96+
catch (error) {
97+
assert.error(error, {
98+
name: "TypeError",
99+
message: "$anchor must be a string, not undefined.",
100+
code: "ERR_INVALID_ANCHOR",
101+
type: "undefined",
102+
input: undefined,
103+
});
104+
}
105+
});
106+
107+
it("should throw an error if the $anchor is null", () => {
108+
try {
109+
new Anchor({
110+
resource: new JsonSchema({ path: "schema.json" }).rootResource,
111+
locationInFile: [],
112+
name: null,
113+
});
114+
assert.failed();
115+
}
116+
catch (error) {
117+
assert.error(error, {
118+
name: "TypeError",
119+
message: "$anchor must be a string, not object.",
120+
code: "ERR_INVALID_ANCHOR",
121+
type: "object",
122+
input: null,
123+
});
124+
}
125+
});
126+
127+
it("should throw an error if the $anchor is not a string", () => {
128+
try {
129+
new Anchor({
130+
resource: new JsonSchema({ path: "schema.json" }).rootResource,
131+
locationInFile: [],
132+
name: 12345,
133+
});
134+
assert.failed();
135+
}
136+
catch (error) {
137+
assert.error(error, {
138+
name: "TypeError",
139+
message: "$anchor must be a string, not number.",
140+
code: "ERR_INVALID_ANCHOR",
141+
type: "number",
142+
input: 12345,
143+
});
144+
}
145+
});
146+
147+
it("should throw an error if the $anchor is empty", () => {
148+
try {
149+
new Anchor({
150+
resource: new JsonSchema({ path: "schema.json" }).rootResource,
151+
locationInFile: [],
152+
name: "",
153+
});
154+
assert.failed();
155+
}
156+
catch (error) {
157+
assert.error(error, {
158+
name: "SyntaxError",
159+
message: "$anchor cannot be empty.",
160+
code: "ERR_INVALID_ANCHOR",
161+
input: "",
162+
});
163+
}
164+
});
165+
166+
it("should throw an error if the $anchor starts with a hash", () => {
167+
try {
168+
new Anchor({
169+
resource: new JsonSchema({ path: "schema.json" }).rootResource,
170+
locationInFile: [],
171+
name: "#my-anchor",
172+
});
173+
assert.failed();
174+
}
175+
catch (error) {
176+
assert.error(error, {
177+
name: "SyntaxError",
178+
message: "$anchor cannot start with a \"#\" character.",
179+
code: "ERR_INVALID_ANCHOR",
180+
input: "#my-anchor",
181+
});
182+
}
183+
});
184+
185+
it("should throw an error if the $anchor does not start with a letter", () => {
186+
try {
187+
new Anchor({
188+
resource: new JsonSchema({ path: "schema.json" }).rootResource,
189+
locationInFile: [],
190+
name: "-my-anchor",
191+
});
192+
assert.failed();
193+
}
194+
catch (error) {
195+
assert.error(error, {
196+
name: "SyntaxError",
197+
message: "$anchor must start with a letter.",
198+
code: "ERR_INVALID_ANCHOR",
199+
input: "-my-anchor",
200+
});
201+
}
202+
});
203+
204+
it("should throw an error if the $anchor contains invalid characters", () => {
205+
try {
206+
new Anchor({
207+
resource: new JsonSchema({ path: "schema.json" }).rootResource,
208+
locationInFile: [],
209+
name: "my-awesome!-anchor",
210+
});
211+
assert.failed();
212+
}
213+
catch (error) {
214+
assert.error(error, {
215+
name: "SyntaxError",
216+
message: "$anchor contains illegal characters.",
217+
code: "ERR_INVALID_ANCHOR",
218+
input: "my-awesome!-anchor",
219+
});
220+
}
221+
});
222+
223+
});

test/specs/exports.spec.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"use strict";
2+
3+
const { expect } = require("chai");
4+
const commonJSExport = require("../../");
5+
const { default: defaultExport } = require("../../");
6+
const {
7+
JsonSchema, File, Resource, Anchor, Reference, Pointer, Resolution, SchemaError, createErrorHandler,
8+
jsonSchema, jsonSchema2019_09, jsonSchemaDraft4, jsonSchemaDraft8 // eslint-disable-line camelcase
9+
} = require("../../");
10+
11+
describe("json-schema package exports", () => {
12+
13+
it("should not have a default ESM export", () => {
14+
expect(defaultExport).to.equal(undefined);
15+
});
16+
17+
it("should export the JsonSchema class", () => {
18+
expect(JsonSchema).to.be.a("function");
19+
expect(JsonSchema.name).to.equal("JsonSchema");
20+
});
21+
22+
it("should export the File class", () => {
23+
expect(File).to.be.a("function");
24+
expect(File.name).to.equal("File");
25+
});
26+
27+
it("should export the Resource class", () => {
28+
expect(Resource).to.be.a("function");
29+
expect(Resource.name).to.equal("Resource");
30+
});
31+
32+
it("should export the Anchor class", () => {
33+
expect(Anchor).to.be.a("function");
34+
expect(Anchor.name).to.equal("Anchor");
35+
});
36+
37+
it("should export the Reference class", () => {
38+
expect(Reference).to.be.a("function");
39+
expect(Reference.name).to.equal("Reference");
40+
});
41+
42+
it("should export the Pointer class", () => {
43+
expect(Pointer).to.be.a("function");
44+
expect(Pointer.name).to.equal("Pointer");
45+
});
46+
47+
it("should export the Resolution class", () => {
48+
expect(Resolution).to.be.a("function");
49+
expect(Resolution.name).to.equal("Resolution");
50+
});
51+
52+
it("should export the SchemaError class", () => {
53+
expect(SchemaError).to.be.a("function");
54+
expect(SchemaError.name).to.equal("SchemaError");
55+
});
56+
57+
it("should export the createErrorHandler function", () => {
58+
expect(createErrorHandler).to.be.a("function");
59+
expect(createErrorHandler.name).to.equal("createErrorHandler");
60+
});
61+
62+
it("should export the jsonSchema object", () => {
63+
expect(jsonSchema).to.be.an("object");
64+
expect(jsonSchema["draft-04"]).to.equal(jsonSchemaDraft4);
65+
expect(jsonSchema["draft-08"]).to.equal(jsonSchemaDraft8);
66+
expect(jsonSchema["2019-09"]).to.equal(jsonSchema2019_09);
67+
});
68+
69+
it("should export each JSON Schema version implementation as a named export", () => {
70+
assertJsonSchemaVersion(jsonSchemaDraft4);
71+
assertJsonSchemaVersion(jsonSchemaDraft8);
72+
assertJsonSchemaVersion(jsonSchema2019_09);
73+
74+
function assertJsonSchemaVersion (version) {
75+
expect(version).to.be.an("object").with.keys(["indexFile", "resolveRef"]);
76+
expect(version.indexFile).to.be.a("function");
77+
expect(version.resolveRef).to.be.a("function");
78+
}
79+
});
80+
81+
it("should not export anything else", () => {
82+
expect(commonJSExport).to.have.same.keys(
83+
"JsonSchema",
84+
"File",
85+
"Resource",
86+
"Anchor",
87+
"Reference",
88+
"Pointer",
89+
"Resolution",
90+
"SchemaError",
91+
"createErrorHandler",
92+
"jsonSchema",
93+
"jsonSchemaDraft4",
94+
"jsonSchemaDraft8",
95+
"jsonSchema2019_09",
96+
);
97+
});
98+
99+
});

0 commit comments

Comments
 (0)