massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / @humanwhocodes / object-schema / tests / object-schema.js
1 /**
2  * @filedescription Object Schema Tests
3  */
4 /* global it, describe, beforeEach */
5
6 "use strict";
7
8 //-----------------------------------------------------------------------------
9 // Requirements
10 //-----------------------------------------------------------------------------
11
12 const assert = require("chai").assert;
13 const { ObjectSchema } = require("../src/");
14
15 //-----------------------------------------------------------------------------
16 // Class
17 //-----------------------------------------------------------------------------
18
19 describe("ObjectSchema", () => {
20
21     let schema;
22
23     describe("new ObjectSchema()", () => {
24
25         it("should add a new key when a strategy is passed", () => {
26             schema = new ObjectSchema({
27                 foo: {
28                     merge() {},
29                     validate() {}
30                 }
31             });
32
33             assert.isTrue(schema.hasKey("foo"));
34         });
35
36         it("should throw an error when a strategy is missing a merge() method", () => {
37             assert.throws(() => {
38                 schema = new ObjectSchema({
39                     foo: {
40                         validate() { }
41                     }
42                 });
43             }, /Definition for key "foo" must have a merge property/);
44         });
45
46         it("should throw an error when a strategy is missing a merge() method", () => {
47             assert.throws(() => {
48                 schema = new ObjectSchema();
49             }, /Schema definitions missing/);
50         });
51
52         it("should throw an error when a strategy is missing a validate() method", () => {
53             assert.throws(() => {
54                 schema = new ObjectSchema({
55                     foo: {
56                         merge() { },
57                     }
58                 });
59             }, /Definition for key "foo" must have a validate\(\) method/);
60         });
61
62         it("should throw an error when merge is an invalid string", () => {
63             assert.throws(() => {
64                 new ObjectSchema({
65                     foo: {
66                         merge: "bar",
67                         validate() { }
68                     }
69                 });
70             }, /key "foo" missing valid merge strategy/);
71         });
72
73         it("should throw an error when validate is an invalid string", () => {
74             assert.throws(() => {
75                 new ObjectSchema({
76                     foo: {
77                         merge: "assign",
78                         validate: "s"
79                     }
80                 });
81             }, /key "foo" missing valid validation strategy/);
82         });
83
84     });
85
86
87     describe("merge()", () => {
88
89         it("should throw an error when an unexpected key is found", () => {
90             let schema = new ObjectSchema({});
91
92             assert.throws(() => {
93                 schema.merge({ foo: true }, { foo: true });
94             }, /Unexpected key "foo"/);
95         });
96
97         it("should throw an error when merge() throws an error", () => {
98             let schema = new ObjectSchema({
99                 foo: {
100                     merge() {
101                         throw new Error("Boom!");
102                     },
103                     validate() {}
104                 }
105             });
106
107             assert.throws(() => {
108                 schema.merge({ foo: true }, { foo: true });
109             }, /Key "foo": Boom!/);
110         
111         });
112
113         it("should call the merge() strategy for one key when called", () => {
114             
115             schema = new ObjectSchema({
116                 foo: {
117                     merge() {
118                         return "bar";
119                     },
120                     validate() {}
121                 }
122             });
123
124             const result = schema.merge({ foo: true }, { foo: false });
125             assert.propertyVal(result, "foo", "bar");
126         });
127
128         it("should not call the merge() strategy when both objects don't contain the key", () => {
129             
130             let called = false;
131
132             schema = new ObjectSchema({
133                 foo: {
134                     merge() {
135                         called = true;
136                     },
137                     validate() {}
138                 }
139             });
140
141             schema.merge({}, {});
142             assert.isFalse(called, "The merge() strategy should not have been called.");
143         });
144
145         it("should omit returning the key when the merge() strategy returns undefined", () => {
146             schema = new ObjectSchema({
147                 foo: {
148                     merge() {
149                         return undefined;
150                     },
151                     validate() { }
152                 }
153             });
154             
155             const result = schema.merge({ foo: true }, { foo: false });
156             assert.notProperty(result, "foo");
157         });
158
159         it("should call the merge() strategy for two keys when called", () => {
160             schema = new ObjectSchema({
161                 foo: {
162                     merge() {
163                         return "bar";
164                     },
165                     validate() { }
166                 },
167                 bar: {
168                     merge() {
169                         return "baz";
170                     },
171                     validate() {}
172                 }
173             });
174             
175             const result = schema.merge({ foo: true, bar: 1 }, { foo: true, bar: 2 });
176             assert.propertyVal(result, "foo", "bar");
177             assert.propertyVal(result, "bar", "baz");
178         });
179
180         it("should call the merge() strategy for two keys when called on three objects", () => {
181             schema = new ObjectSchema({
182                 foo: {
183                     merge() {
184                         return "bar";
185                     },
186                     validate() { }
187                 },
188                 bar: {
189                     merge() {
190                         return "baz";
191                     },
192                     validate() { }
193                 }
194             });
195             
196             const result = schema.merge(
197                 { foo: true, bar: 1 },
198                 { foo: true, bar: 3 },
199                 { foo: false, bar: 2 }
200             );
201             assert.propertyVal(result, "foo", "bar");
202             assert.propertyVal(result, "bar", "baz");
203         });
204
205         it("should call the merge() strategy when defined as 'overwrite'", () => {
206             schema = new ObjectSchema({
207                 foo: {
208                     merge: "overwrite",
209                     validate() { }
210                 }
211             });
212             
213             const result = schema.merge(
214                 { foo: true },
215                 { foo: false }
216             );
217             assert.propertyVal(result, "foo", false);
218         });
219
220         it("should call the merge() strategy when defined as 'assign'", () => {
221             schema = new ObjectSchema({
222                 foo: {
223                     merge: "assign",
224                     validate() { }
225                 }
226             });
227             
228             const result = schema.merge(
229                 { foo: { bar: true } },
230                 { foo: { baz: false } }
231             );
232
233             assert.strictEqual(result.foo.bar, true);
234             assert.strictEqual(result.foo.baz, false);
235         });
236
237         it("should call the merge strategy when there's a subschema", () => {
238
239             schema = new ObjectSchema({
240                 name: {
241                     schema: {
242                         first: {
243                             merge: "replace",
244                             validate: "string"
245                         },
246                         last: {
247                             merge: "replace",
248                             validate: "string"
249                         }
250                     }
251                 }
252             });
253
254             const result = schema.merge({
255                 name: {
256                     first: "n",
257                     last: "z"
258                 }
259             }, {
260                 name: {
261                     first: "g"
262                 }
263             });
264
265             assert.strictEqual(result.name.first, "g");
266             assert.strictEqual(result.name.last, "z");
267         });
268
269         it("should return separate objects when using subschema", () => {
270
271             schema = new ObjectSchema({
272                 age: {
273                     merge: "replace",
274                     validate: "number"
275                 },
276                 address: {
277                     schema: {
278                         street: {
279                             schema: {
280                                 number: {
281                                     merge: "replace",
282                                     validate: "number"
283                                 },
284                                 streetName: {
285                                     merge: "replace",
286                                     validate: "string"
287                                 }
288                             }
289                         },
290                         state: {
291                             merge: "replace",
292                             validate: "string"
293                         }
294                     }
295                 }
296             });
297
298             const baseObject = {
299                 address: {
300                     street: {
301                         number: 100,
302                         streetName: "Foo St"
303                     },
304                     state: "HA"
305                 }
306             };
307
308             const result = schema.merge(baseObject, {
309                 age: 29
310             });
311
312             assert.notStrictEqual(result.address.street, baseObject.address.street);
313             assert.deepStrictEqual(result.address, baseObject.address);
314         });
315
316         it("should not error when calling the merge strategy when there's a subschema and no matching key in second object", () => {
317
318             schema = new ObjectSchema({
319                 name: {
320                     schema: {
321                         first: {
322                             merge: "replace",
323                             validate: "string"
324                         },
325                         last: {
326                             merge: "replace",
327                             validate: "string"
328                         }
329                     }
330                 }
331             });
332
333             const result = schema.merge({
334                 name: {
335                     first: "n",
336                     last: "z"
337                 }
338             }, {
339             });
340
341             assert.strictEqual(result.name.first, "n");
342             assert.strictEqual(result.name.last, "z");
343         });
344
345         it("should not error when calling the merge strategy when there's multiple subschemas and no matching key in second object", () => {
346
347             schema = new ObjectSchema({
348                 user: {
349                     schema: {
350                         name: {
351                             schema: {
352                                 first: {
353                                     merge: "replace",
354                                     validate: "string"
355                                 },
356                                 last: {
357                                     merge: "replace",
358                                     validate: "string"
359                                 }
360                             }
361                         }
362
363                     }
364                 }
365             });
366
367             const result = schema.merge({
368                 user: {
369                     name: {
370                         first: "n",
371                         last: "z"
372                     }
373                 }
374             }, {
375             });
376
377             assert.strictEqual(result.user.name.first, "n");
378             assert.strictEqual(result.user.name.last, "z");
379         });
380
381
382     });
383
384     describe("validate()", () => {
385
386         it("should throw an error when an unexpected key is found", () => {
387             let schema = new ObjectSchema({});
388             assert.throws(() => {
389                 schema.validate({ foo: true });
390             }, /Unexpected key "foo"/);
391         });
392
393         it("should not throw an error when an expected key is found", () => {
394             schema = new ObjectSchema({
395                 foo: {
396                     merge() {
397                         return "bar";
398                     },
399                     validate() {}
400                 }
401             });
402             
403             schema.validate({ foo: true });
404         });
405
406         it("should pass the property value into validate() when key is found", () => {
407             schema = new ObjectSchema({
408                 foo: {
409                     merge() {
410                         return "bar";
411                     },
412                     validate(value) {
413                         assert.isTrue(value);
414                     }
415                 }
416             });
417             
418             schema.validate({ foo: true });
419         });
420
421         it("should not throw an error when expected keys are found", () => {
422             schema = new ObjectSchema({
423                 foo: {
424                     merge() {
425                         return "bar";
426                     },
427                     validate() {}
428                 },
429                 bar: {
430                     merge() {
431                         return "baz";
432                     },
433                     validate() {}
434                 }
435             });
436             
437             schema.validate({ foo: true, bar: true });
438         });
439
440         it("should not throw an error when expected keys are found with required keys", () => {
441             schema = new ObjectSchema({
442                 foo: {
443                     merge() {
444                         return "bar";
445                     },
446                     validate() { }
447                 },
448                 bar: {
449                     requires: ["foo"],
450                     merge() {
451                         return "baz";
452                     },
453                     validate() { }
454                 }
455             });
456             
457             schema.validate({ foo: true, bar: true });
458         });
459
460         it("should throw an error when expected keys are found without required keys", () => {
461             schema = new ObjectSchema({
462                 foo: {
463                     merge() {
464                         return "bar";
465                     },
466                     validate() { }
467                 },
468                 baz: {
469                     merge() {
470                         return "baz";
471                     },
472                     validate() { }
473                 },
474                 bar: {
475                     name: "bar",
476                     requires: ["foo", "baz"],
477                     merge() { },
478                     validate() { }
479                 }
480             });
481
482             assert.throws(() => {
483                 schema.validate({ bar: true });
484             }, /Key "bar" requires keys "foo", "baz"./);
485         });
486
487
488         it("should throw an error when an expected key is found but is invalid", () => {
489
490             schema = new ObjectSchema({
491                 foo: {
492                     merge() {
493                         return "bar";
494                     },
495                     validate() {
496                         throw new Error("Invalid key.");
497                     }
498                 }
499             });
500
501             assert.throws(() => {
502                 schema.validate({ foo: true });
503             }, /Key "foo": Invalid key/);
504         });
505
506         it("should throw an error when an expected key is found but is invalid with a string validator", () => {
507
508             schema = new ObjectSchema({
509                 foo: {
510                     merge() {
511                         return "bar";
512                     },
513                     validate: "string"
514                 }
515             });
516
517             assert.throws(() => {
518                 schema.validate({ foo: true });
519             }, /Key "foo": Expected a string/);
520         });
521
522         it("should throw an error when an expected key is found but is invalid with a number validator", () => {
523
524             schema = new ObjectSchema({
525                 foo: {
526                     merge() {
527                         return "bar";
528                     },
529                     validate: "number"
530                 }
531             });
532
533             assert.throws(() => {
534                 schema.validate({ foo: true });
535             }, /Key "foo": Expected a number/);
536         });
537
538         it("should throw an error when a required key is missing", () => {
539
540             schema = new ObjectSchema({
541                 foo: {
542                     required: true,
543                     merge() {
544                         return "bar";
545                     },
546                     validate() {}
547                 }
548             });
549
550             assert.throws(() => {
551                 schema.validate({});
552             }, /Missing required key "foo"/);
553         });
554
555         it("should throw an error when a subschema is provided and the value doesn't validate", () => {
556
557             schema = new ObjectSchema({
558                 name: {
559                     schema: {
560                         first: {
561                             merge: "replace",
562                             validate: "string"
563                         },
564                         last: {
565                             merge: "replace",
566                             validate: "string"
567                         }
568                     }
569                 }
570             });
571
572             assert.throws(() => {
573                 schema.validate({
574                     name: {
575                         first: 123,
576                         last: "z"
577                     }
578                 });
579                         
580             }, /Key "name": Key "first": Expected a string/);
581         });
582
583         it("should not throw an error when a subschema is provided and the value validates", () => {
584
585             schema = new ObjectSchema({
586                 name: {
587                     schema: {
588                         first: {
589                             merge: "replace",
590                             validate: "string"
591                         },
592                         last: {
593                             merge: "replace",
594                             validate: "string"
595                         }
596                     }
597                 }
598             });
599
600             schema.validate({
601                 name: {
602                     first: "n",
603                     last: "z"
604                 }
605             });
606                     
607         });
608
609     });
610
611 });