|
6 | 6 | deepEquals,
|
7 | 7 | getDefaultFormState,
|
8 | 8 | isFilesArray,
|
| 9 | + isConstant, |
| 10 | + toConstant, |
9 | 11 | isMultiSelect,
|
10 | 12 | mergeObjects,
|
11 | 13 | pad,
|
@@ -269,24 +271,101 @@ describe("utils", () => {
|
269 | 271 | });
|
270 | 272 | });
|
271 | 273 |
|
272 |
| - describe("isMultiSelect()", () => { |
273 |
| - it("should be true if schema items enum is an array and uniqueItems is true", () => { |
274 |
| - let schema = { items: { enum: ["foo", "bar"] }, uniqueItems: true }; |
275 |
| - expect(isMultiSelect(schema)).to.be.true; |
| 274 | + describe("isConstant", () => { |
| 275 | + it("should return false when neither enum nor const is defined", () => { |
| 276 | + const schema = {}; |
| 277 | + expect(isConstant(schema)).to.be.false; |
276 | 278 | });
|
277 | 279 |
|
278 |
| - it("should be false if items is undefined", () => { |
279 |
| - const schema = {}; |
280 |
| - expect(isMultiSelect(schema)).to.be.false; |
| 280 | + it("should return true when schema enum is an array of one item", () => { |
| 281 | + const schema = { enum: ["foo"] }; |
| 282 | + expect(isConstant(schema)).to.be.true; |
281 | 283 | });
|
282 | 284 |
|
283 |
| - it("should be false if uniqueItems is false", () => { |
284 |
| - const schema = { items: { enum: ["foo", "bar"] }, uniqueItems: false }; |
285 |
| - expect(isMultiSelect(schema)).to.be.false; |
| 285 | + it("should return false when schema enum contains several items", () => { |
| 286 | + const schema = { enum: ["foo", "bar", "baz"] }; |
| 287 | + expect(isConstant(schema)).to.be.false; |
| 288 | + }); |
| 289 | + |
| 290 | + it("should return true when schema const is defined", () => { |
| 291 | + const schema = { const: "foo" }; |
| 292 | + expect(isConstant(schema)).to.be.true; |
| 293 | + }); |
| 294 | + }); |
| 295 | + |
| 296 | + describe("toConstant()", () => { |
| 297 | + describe("schema contains an enum array", () => { |
| 298 | + it("should return its first value when it contains a unique element", () => { |
| 299 | + const schema = { enum: ["foo"] }; |
| 300 | + expect(toConstant(schema)).eql("foo"); |
| 301 | + }); |
| 302 | + |
| 303 | + it("should return schema const value when it exists", () => { |
| 304 | + const schema = { const: "bar" }; |
| 305 | + expect(toConstant(schema)).eql("bar"); |
| 306 | + }); |
| 307 | + |
| 308 | + it("should throw when it contains more than one element", () => { |
| 309 | + const schema = { enum: ["foo", "bar"] }; |
| 310 | + expect(() => { |
| 311 | + toConstant(schema); |
| 312 | + }).to.Throw(Error, "cannot be inferred"); |
| 313 | + }); |
| 314 | + }); |
| 315 | + }); |
| 316 | + |
| 317 | + describe("isMultiSelect()", () => { |
| 318 | + describe("uniqueItems is true", () => { |
| 319 | + describe("schema items enum is an array", () => { |
| 320 | + it("should be true", () => { |
| 321 | + let schema = { items: { enum: ["foo", "bar"] }, uniqueItems: true }; |
| 322 | + expect(isMultiSelect(schema)).to.be.true; |
| 323 | + }); |
| 324 | + }); |
| 325 | + |
| 326 | + it("should be false if items is undefined", () => { |
| 327 | + const schema = {}; |
| 328 | + expect(isMultiSelect(schema)).to.be.false; |
| 329 | + }); |
| 330 | + |
| 331 | + describe("schema items enum is not an array", () => { |
| 332 | + const constantSchema = { type: "string", enum: ["Foo"] }; |
| 333 | + const notConstantSchema = { type: "string" }; |
| 334 | + |
| 335 | + it("should be false if oneOf/anyOf is not in items schema", () => { |
| 336 | + const schema = { items: {}, uniqueItems: true }; |
| 337 | + expect(isMultiSelect(schema)).to.be.false; |
| 338 | + }); |
| 339 | + |
| 340 | + it("should be false if oneOf/anyOf schemas are not all constants", () => { |
| 341 | + const schema = { |
| 342 | + items: { oneOf: [constantSchema, notConstantSchema] }, |
| 343 | + uniqueItems: true, |
| 344 | + }; |
| 345 | + expect(isMultiSelect(schema)).to.be.false; |
| 346 | + }); |
| 347 | + |
| 348 | + it("should be true if oneOf/anyOf schemas are all constants", () => { |
| 349 | + const schema = { |
| 350 | + items: { oneOf: [constantSchema, constantSchema] }, |
| 351 | + uniqueItems: true, |
| 352 | + }; |
| 353 | + expect(isMultiSelect(schema)).to.be.true; |
| 354 | + }); |
| 355 | + }); |
| 356 | + |
| 357 | + it("should retrieve reference schema definitions", () => { |
| 358 | + const schema = { |
| 359 | + items: { $ref: "#/definitions/FooItem" }, |
| 360 | + uniqueItems: true, |
| 361 | + }; |
| 362 | + const definitions = { FooItem: { type: "string", enum: ["foo"] } }; |
| 363 | + expect(isMultiSelect(schema, definitions)).to.be.true; |
| 364 | + }); |
286 | 365 | });
|
287 | 366 |
|
288 |
| - it("should be false if schema items enum is not an array", () => { |
289 |
| - const schema = { items: {}, uniqueItems: true }; |
| 367 | + it("should be false if uniqueItems is false", () => { |
| 368 | + const schema = { items: { enum: ["foo", "bar"] }, uniqueItems: false }; |
290 | 369 | expect(isMultiSelect(schema)).to.be.false;
|
291 | 370 | });
|
292 | 371 | });
|
@@ -605,7 +684,7 @@ describe("utils", () => {
|
605 | 684 | });
|
606 | 685 | });
|
607 | 686 |
|
608 |
| - it("should retrieve reference schema definitions", () => { |
| 687 | + it("should retrieve referenced schema definitions", () => { |
609 | 688 | const schema = {
|
610 | 689 | definitions: {
|
611 | 690 | testdef: {
|
|
0 commit comments