|
| 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 | +}); |
0 commit comments