@rosen-bridge/config 0.3.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,955 +0,0 @@
1
- import { configDir } from './configEnvSetup';
2
- import config from 'config';
3
- import fs from 'fs';
4
- import path from 'path';
5
- import { afterAll, beforeEach, describe, expect, it } from 'vitest';
6
- import { ConfigValidator } from '../lib';
7
- import { ConfigSchema } from '../lib/schema/types/fields';
8
- import * as testData from './configTestData';
9
-
10
- afterAll(() => {
11
- fs.rmSync(configDir, { force: true, recursive: true });
12
- });
13
-
14
- beforeEach(() => {
15
- fs.readdirSync(configDir).forEach((file) => {
16
- fs.unlinkSync(path.join(configDir, file));
17
- });
18
- fs.cpSync(path.join(__dirname, 'configTestFiles'), configDir, {
19
- recursive: true,
20
- });
21
- });
22
-
23
- describe('ConfigValidator', () => {
24
- describe('generateDefault', () => {
25
- /**
26
- * @target generateDefault should return default values object for the
27
- * passed schema
28
- * @dependencies
29
- * @scenario
30
- * - call generateDefault
31
- * - check if correct default value object is returned
32
- * @expected
33
- * - correct default value object should have been returned
34
- */
35
- it(`should return default values object for the passed schema`, async () => {
36
- const config = new ConfigValidator(
37
- <ConfigSchema>testData.apiSchemaDefaultValuePairSample.schema
38
- );
39
- expect(config.generateDefault()).toEqual(
40
- testData.apiSchemaDefaultValuePairSample.defaultVal
41
- );
42
- });
43
- });
44
-
45
- describe('validateSchema', () => {
46
- /**
47
- * @target validateSchema should not throw any exceptions when a correct
48
- * schema is passed
49
- * @dependencies
50
- * @scenario
51
- * - create a new instance of Config which calls Config.validateSchema
52
- * - check if any exception is thrown
53
- * @expected
54
- * - no errors should be thrown
55
- */
56
- it(`should not throw any exceptions when a correct schema is passed`, async () => {
57
- new ConfigValidator(<ConfigSchema>testData.correctApiSchema);
58
- });
59
-
60
- /**
61
- * @target validateSchema should throw exception when a schema with
62
- * incorrect default value type is passed
63
- * @dependencies
64
- * @scenario
65
- * - create a new instance of Config which calls Config.validateSchema
66
- * - check if any exception is thrown
67
- * @expected
68
- * - exception should be thrown
69
- */
70
- it(`should throw exception when a schema with incorrect default value type
71
- is passed`, async () => {
72
- expect(
73
- () =>
74
- new ConfigValidator(
75
- <ConfigSchema>testData.schemaWithIncorrectPortDefaultValueTypeSample
76
- )
77
- ).toThrow();
78
- });
79
-
80
- /**
81
- * @target validateSchema should throw exception when array type doesn't
82
- * have an item property
83
- * @dependencies
84
- * @scenario
85
- * - create a new instance of Config which calls Config.validateSchema
86
- * - check if any exception is thrown
87
- * @expected
88
- * - exception should be thrown
89
- */
90
- it(`should throw exception when array type doesn't have an item property`, async () => {
91
- expect(
92
- () =>
93
- new ConfigValidator(
94
- <ConfigSchema>testData.arrayTypeSchemaWithoutItems
95
- )
96
- ).toThrow();
97
- });
98
-
99
- /**
100
- * @target validateSchema should throw exception when object type doesn't
101
- * have a children property
102
- * @dependencies
103
- * @scenario
104
- * - create a new instance of Config which calls Config.validateSchema
105
- * - check if any exception is thrown
106
- * @expected
107
- * - exception should be thrown
108
- */
109
- it(`should throw exception when object type doesn't have a children property`, async () => {
110
- expect(
111
- () =>
112
- new ConfigValidator(
113
- <ConfigSchema>testData.objectTypeSchemaWithoutChildren
114
- )
115
- ).toThrow();
116
- });
117
- });
118
-
119
- describe('validateConfig', () => {
120
- /**
121
- * @target validateConfig should not throw any exceptions when a correct
122
- * config is passed
123
- * @dependencies
124
- * @scenario
125
- * - call validateConfig with the config
126
- * - check if any exception is thrown
127
- * @expected
128
- * - no errors should be thrown
129
- */
130
- it(`should not throw any exceptions when a correct config is passed`, async () => {
131
- const confValidator = new ConfigValidator(
132
- <ConfigSchema>testData.apiSchemaConfigPair.schema
133
- );
134
- confValidator.validateConfig(testData.apiSchemaConfigPair.config);
135
- });
136
-
137
- /**
138
- * @target validateConfig should throw exception when a config violating
139
- * choices constraint is passed
140
- * @dependencies
141
- * @scenario
142
- * - call validateConfig with the config
143
- * - check if any exception is thrown
144
- * @expected
145
- * - exception should be thrown
146
- */
147
- it(`should throw exception when a config violating choices constraint is
148
- passed`, async () => {
149
- const confValidator = new ConfigValidator(
150
- <ConfigSchema>testData.apiSchemaConfigPairWrongChoice.schema
151
- );
152
-
153
- expect(() =>
154
- confValidator.validateConfig(
155
- testData.apiSchemaConfigPairWrongChoice.config
156
- )
157
- ).toThrow();
158
- });
159
-
160
- /**
161
- * @target validateSchema should throw exception when a config violating
162
- * regex constraint is passed
163
- * @dependencies
164
- * @scenario
165
- * - call validateConfig with the config
166
- * - check if any exception is thrown
167
- * @expected
168
- * - exception should be thrown
169
- */
170
- it(`should throw exception when a config violating regex constraint is
171
- passed`, async () => {
172
- const confValidator = new ConfigValidator(
173
- <ConfigSchema>testData.apiSchemaConfigPairWrongRegex.schema
174
- );
175
-
176
- expect(() =>
177
- confValidator.validateConfig(
178
- testData.apiSchemaConfigPairWrongRegex.config
179
- )
180
- ).toThrow();
181
- });
182
-
183
- /**
184
- * @target validateSchema should throw exception when a config violating the
185
- * "required" constraint is passed
186
- * @dependencies
187
- * @scenario
188
- * - call validateConfig with the config
189
- * - check if any exception is thrown
190
- * @expected
191
- * - exception should be thrown
192
- */
193
- it(`should throw exception when a config violating the "required" constraint
194
- is passed`, async () => {
195
- const confValidator = new ConfigValidator(
196
- <ConfigSchema>testData.apiSchemaConfigPairWrongRequired.schema
197
- );
198
-
199
- expect(() =>
200
- confValidator.validateConfig(
201
- testData.apiSchemaConfigPairWrongRequired.config
202
- )
203
- ).toThrow();
204
- });
205
-
206
- /**
207
- * @target validateSchema should throw exception when a config violating the
208
- * value type is passed
209
- * @dependencies
210
- * @scenario
211
- * - call validateConfig with the config
212
- * - check if any exception is thrown
213
- * @expected
214
- * - exception should be thrown
215
- */
216
- it(`should throw exception when a config violating the value type is passed`, async () => {
217
- const confValidator = new ConfigValidator(
218
- <ConfigSchema>testData.apiSchemaConfigPairWrongPortType.schema
219
- );
220
-
221
- expect(() =>
222
- confValidator.validateConfig(
223
- testData.apiSchemaConfigPairWrongPortType.config
224
- )
225
- ).toThrow();
226
- });
227
-
228
- /**
229
- * @target validateSchema should throw exception when a config violating the
230
- * "greater than" constraint, is passed
231
- * @dependencies
232
- * @scenario
233
- * - call validateConfig with the config
234
- * - check if any exception is thrown
235
- * @expected
236
- * - exception should be thrown
237
- */
238
- it(`should throw exception when a config violating the "greater than"
239
- constraint, is passed`, async () => {
240
- const confValidator = new ConfigValidator(
241
- <ConfigSchema>testData.apiSchemaConfigPairWrongGreater.schema
242
- );
243
-
244
- expect(() =>
245
- confValidator.validateConfig(
246
- testData.apiSchemaConfigPairWrongGreater.config
247
- )
248
- ).toThrow();
249
- });
250
-
251
- /**
252
- * @target validateSchema should throw exception when a config violating the
253
- * "greater than or equal" constraint, is passed
254
- * @dependencies
255
- * @scenario
256
- * - call validateConfig with the config
257
- * - check if any exception is thrown
258
- * @expected
259
- * - exception should be thrown
260
- */
261
- it(`should throw exception when a config violating the
262
- "greater than or equal" constraint, is passed`, async () => {
263
- const confValidator = new ConfigValidator(
264
- <ConfigSchema>testData.apiSchemaConfigPairWrongGreaterEqual.schema
265
- );
266
-
267
- expect(() =>
268
- confValidator.validateConfig(
269
- testData.apiSchemaConfigPairWrongGreaterEqual.config
270
- )
271
- ).toThrow();
272
- });
273
-
274
- /**
275
- * @target validateSchema should throw exception when a config violating the
276
- * "less than" constraint, is passed
277
- * @dependencies
278
- * @scenario
279
- * - call validateConfig with the config
280
- * - check if any exception is thrown
281
- * @expected
282
- * - exception should be thrown
283
- */
284
- it(`should throw exception when a config violating the "less than"
285
- constraint, is passed`, async () => {
286
- const confValidator = new ConfigValidator(
287
- <ConfigSchema>testData.apiSchemaConfigPairWrongLess.schema
288
- );
289
-
290
- expect(() =>
291
- confValidator.validateConfig(
292
- testData.apiSchemaConfigPairWrongLess.config
293
- )
294
- ).toThrow();
295
- });
296
-
297
- /**
298
- * @target validateSchema should throw exception when a config violating the
299
- * "less than or equal" constraint, is passed
300
- * @dependencies
301
- * @scenario
302
- * - call validateConfig with the config
303
- * - check if any exception is thrown
304
- * @expected
305
- * - exception should be thrown
306
- */
307
- it(`should throw exception when a config violating the "less than or equal"
308
- constraint, is passed`, async () => {
309
- const confValidator = new ConfigValidator(
310
- <ConfigSchema>testData.apiSchemaConfigPairWrongLessEqual.schema
311
- );
312
-
313
- expect(() =>
314
- confValidator.validateConfig(
315
- testData.apiSchemaConfigPairWrongLessEqual.config
316
- )
317
- ).toThrow();
318
- });
319
-
320
- /**
321
- * @target validateSchema should throw exception when a config violating the
322
- * "greater than for bigint" constraint, is passed
323
- * @dependencies
324
- * @scenario
325
- * - call validateConfig with the config
326
- * - check if any exception is thrown
327
- * @expected
328
- * - exception should be thrown
329
- */
330
- it(`should throw exception when a config violating the
331
- "greater than for bigint" constraint, is passed`, async () => {
332
- const confValidator = new ConfigValidator(
333
- <ConfigSchema>testData.apiSchemaConfigPairWrongGreaterBigInt.schema
334
- );
335
-
336
- expect(() =>
337
- confValidator.validateConfig(
338
- testData.apiSchemaConfigPairWrongGreaterBigInt.config
339
- )
340
- ).toThrow();
341
- });
342
-
343
- /**
344
- * @target validateSchema should throw exception when a config violating the
345
- * "greater than or equal for bigint" constraint, is passed
346
- * @dependencies
347
- * @scenario
348
- * - call validateConfig with the config
349
- * - check if any exception is thrown
350
- * @expected
351
- * - exception should be thrown
352
- */
353
- it(`should throw exception when a config violating the
354
- "greater than or equal for bigint" constraint, is passed`, async () => {
355
- const confValidator = new ConfigValidator(
356
- <ConfigSchema>testData.apiSchemaConfigPairWrongGreaterEqualBigInt.schema
357
- );
358
-
359
- expect(() =>
360
- confValidator.validateConfig(
361
- testData.apiSchemaConfigPairWrongGreaterEqualBigInt.config
362
- )
363
- ).toThrow();
364
- });
365
-
366
- /**
367
- * @target validateSchema should throw exception when a config violating the
368
- * "less than for bigint" constraint, is passed
369
- * @dependencies
370
- * @scenario
371
- * - call validateConfig with the config
372
- * - check if any exception is thrown
373
- * @expected
374
- * - exception should be thrown
375
- */
376
- it(`should throw exception when a config violating the
377
- "less than for bigint" constraint, is passed`, async () => {
378
- const confValidator = new ConfigValidator(
379
- <ConfigSchema>testData.apiSchemaConfigPairWrongLessBigInt.schema
380
- );
381
-
382
- expect(() =>
383
- confValidator.validateConfig(
384
- testData.apiSchemaConfigPairWrongLessBigInt.config
385
- )
386
- ).toThrow();
387
- });
388
-
389
- /**
390
- * @target validateSchema should throw exception when a config violating the
391
- * "less than or equal for bigint" constraint, is passed
392
- * @dependencies
393
- * @scenario
394
- * - call validateConfig with the config
395
- * - check if any exception is thrown
396
- * @expected
397
- * - exception should be thrown
398
- */
399
- it(`should throw exception when a config violating the
400
- "less than or equal for bigint" constraint, is passed`, async () => {
401
- const confValidator = new ConfigValidator(
402
- <ConfigSchema>testData.apiSchemaConfigPairWrongLessEqualBigInt.schema
403
- );
404
-
405
- expect(() =>
406
- confValidator.validateConfig(
407
- testData.apiSchemaConfigPairWrongLessEqualBigInt.config
408
- )
409
- ).toThrow();
410
- });
411
-
412
- /**
413
- * @target validateSchema should not throw exception when config violates
414
- * "required" validation but the "when" clause is false
415
- * @dependencies
416
- * @scenario
417
- * - call validateConfig with the config
418
- * - check if any exception is thrown
419
- * @expected
420
- * - exception should be thrown
421
- */
422
- it(`should not throw exception when config violates "required" validation
423
- but the "when" clause is false`, async () => {
424
- const confValidator = new ConfigValidator(
425
- <ConfigSchema>testData.apiSchemaConfigPairWrongRequiredFalseWhen.schema
426
- );
427
-
428
- confValidator.validateConfig(
429
- testData.apiSchemaConfigPairWrongRequiredFalseWhen.config
430
- );
431
- });
432
-
433
- /**
434
- * @target validateSchema should not throw exception when config violates
435
- * "regex" validation but the "when" clause is false
436
- * @dependencies
437
- * @scenario
438
- * - call validateConfig with the config
439
- * - check if any exception is thrown
440
- * @expected
441
- * - exception should be thrown
442
- */
443
- it(`should not throw exception when config violates "regex" validation but
444
- the "when" clause is false`, async () => {
445
- const confValidator = new ConfigValidator(
446
- <ConfigSchema>testData.apiSchemaConfigPairWrongRegexFalseWhen.schema
447
- );
448
-
449
- confValidator.validateConfig(
450
- testData.apiSchemaConfigPairWrongRegexFalseWhen.config
451
- );
452
- });
453
-
454
- /**
455
- * @target validateSchema should not throw exception when config violates
456
- * "choices" validation but the "when" clause is false
457
- * @dependencies
458
- * @scenario
459
- * - call validateConfig with the config
460
- * - check if any exception is thrown
461
- * @expected
462
- * - exception should be thrown
463
- */
464
- it(`should not throw exception when config violates "choices" validation but
465
- the "when" clause is false`, async () => {
466
- const confValidator = new ConfigValidator(
467
- <ConfigSchema>testData.apiSchemaConfigPairWrongChoiceFalseWhen.schema
468
- );
469
-
470
- confValidator.validateConfig(
471
- testData.apiSchemaConfigPairWrongChoiceFalseWhen.config
472
- );
473
- });
474
-
475
- /**
476
- * @target validateSchema should not throw exception when config violates
477
- * "gt" validation but the "when" clause is false
478
- * @dependencies
479
- * @scenario
480
- * - call validateConfig with the config
481
- * - check if any exception is thrown
482
- * @expected
483
- * - exception should be thrown
484
- */
485
- it(`should not throw exception when config violates "gt" validation but the
486
- "when" clause is false`, async () => {
487
- const confValidator = new ConfigValidator(
488
- <ConfigSchema>testData.apiSchemaConfigPairWrongGreaterFalseWhen.schema
489
- );
490
-
491
- confValidator.validateConfig(
492
- testData.apiSchemaConfigPairWrongGreaterFalseWhen.config
493
- );
494
- });
495
-
496
- /**
497
- * @target validateSchema should not throw exception when config violates
498
- * "gte" validation but the "when" clause is false
499
- * @dependencies
500
- * @scenario
501
- * - call validateConfig with the config
502
- * - check if any exception is thrown
503
- * @expected
504
- * - exception should be thrown
505
- */
506
- it(`should not throw exception when config violates "gte" validation but
507
- the "when" clause is false`, async () => {
508
- const confValidator = new ConfigValidator(
509
- <ConfigSchema>(
510
- testData.apiSchemaConfigPairWrongGreaterEqualFalseWhen.schema
511
- )
512
- );
513
-
514
- confValidator.validateConfig(
515
- testData.apiSchemaConfigPairWrongGreaterEqualFalseWhen.config
516
- );
517
- });
518
-
519
- /**
520
- * @target validateSchema should not throw exception when config violates
521
- * "lt" validation but the "when" clause is false
522
- * @dependencies
523
- * @scenario
524
- * - call validateConfig with the config
525
- * - check if any exception is thrown
526
- * @expected
527
- * - exception should be thrown
528
- */
529
- it(`should not throw exception when config violates "lt" validation but the
530
- "when" clause is false`, async () => {
531
- const confValidator = new ConfigValidator(
532
- <ConfigSchema>testData.apiSchemaConfigPairWrongLessFalseWhen.schema
533
- );
534
-
535
- confValidator.validateConfig(
536
- testData.apiSchemaConfigPairWrongLessFalseWhen.config
537
- );
538
- });
539
-
540
- /**
541
- * @target validateSchema should not throw exception when config violates
542
- * "lte" validation but the "when" clause is false
543
- * @dependencies
544
- * @scenario
545
- * - call validateConfig with the config
546
- * - check if any exception is thrown
547
- * @expected
548
- * - exception should be thrown
549
- */
550
- it(`should not throw exception when config violates "lte" validation but the
551
- "when" clause is false`, async () => {
552
- const confValidator = new ConfigValidator(
553
- <ConfigSchema>testData.apiSchemaConfigPairWrongLessEqualFalseWhen.schema
554
- );
555
-
556
- confValidator.validateConfig(
557
- testData.apiSchemaConfigPairWrongLessEqualFalseWhen.config
558
- );
559
- });
560
-
561
- /**
562
- * @target validateSchema should not throw exception when config violates
563
- * "bigint gt" validation but the "when" clause is false
564
- * @dependencies
565
- * @scenario
566
- * - call validateConfig with the config
567
- * - check if any exception is thrown
568
- * @expected
569
- * - exception should be thrown
570
- */
571
- it(`should not throw exception when config violates "bigint gt" validation
572
- but the "when" clause is false`, async () => {
573
- const confValidator = new ConfigValidator(
574
- <ConfigSchema>(
575
- testData.apiSchemaConfigPairWrongBigIntGreaterFalseWhen.schema
576
- )
577
- );
578
-
579
- confValidator.validateConfig(
580
- testData.apiSchemaConfigPairWrongBigIntGreaterFalseWhen.config
581
- );
582
- });
583
-
584
- /**
585
- * @target validateSchema should not throw exception when config violates
586
- * "bigint gte" validation but the "when" clause is false
587
- * @dependencies
588
- * @scenario
589
- * - call validateConfig with the config
590
- * - check if any exception is thrown
591
- * @expected
592
- * - exception should be thrown
593
- */
594
- it(`should not throw exception when config violates "bigint gte" validation
595
- but the "when" clause is false`, async () => {
596
- const confValidator = new ConfigValidator(
597
- <ConfigSchema>(
598
- testData.apiSchemaConfigPairWrongBigIntGreaterEqualFalseWhen.schema
599
- )
600
- );
601
-
602
- confValidator.validateConfig(
603
- testData.apiSchemaConfigPairWrongBigIntGreaterEqualFalseWhen.config
604
- );
605
- });
606
-
607
- /**
608
- * @target validateSchema should not throw exception when config violates
609
- * "bigint lt" validation but the "when" clause is false
610
- * @dependencies
611
- * @scenario
612
- * - call validateConfig with the config
613
- * - check if any exception is thrown
614
- * @expected
615
- * - exception should be thrown
616
- */
617
- it(`should not throw exception when config violates "bigint lt" validation
618
- but the "when" clause is false`, async () => {
619
- const confValidator = new ConfigValidator(
620
- <ConfigSchema>(
621
- testData.apiSchemaConfigPairWrongBigIntLessFalseWhen.schema
622
- )
623
- );
624
-
625
- confValidator.validateConfig(
626
- testData.apiSchemaConfigPairWrongBigIntLessFalseWhen.config
627
- );
628
- });
629
-
630
- /**
631
- * @target validateSchema should not throw exception when config violates
632
- * "bigint lte" validation but the "when" clause is false
633
- * @dependencies
634
- * @scenario
635
- * - call validateConfig with the config
636
- * - check if any exception is thrown
637
- * @expected
638
- * - exception should be thrown
639
- */
640
- it(`should not throw exception when config violates "bigint lte" validation
641
- but the "when" clause is false`, async () => {
642
- const confValidator = new ConfigValidator(
643
- <ConfigSchema>(
644
- testData.apiSchemaConfigPairWrongBigIntLessEqualFalseWhen.schema
645
- )
646
- );
647
-
648
- confValidator.validateConfig(
649
- testData.apiSchemaConfigPairWrongBigIntLessEqualFalseWhen.config
650
- );
651
- });
652
-
653
- /**
654
- * @target validateSchema should throw exception using the custom message
655
- * when the validation has error property set
656
- * @dependencies
657
- * @scenario
658
- * - call validateConfig with the config
659
- * - check if any exception is thrown with the right message
660
- * @expected
661
- * - exception should be thrown with the right message
662
- */
663
- it(`should throw exception using the custom message when the validation has
664
- error property set`, async () => {
665
- const confValidator = new ConfigValidator(
666
- <ConfigSchema>testData.apiSchemaConfigPairWrongChoice.schema
667
- );
668
-
669
- expect(() =>
670
- confValidator.validateConfig(
671
- testData.apiSchemaConfigPairWrongChoice.config
672
- )
673
- ).toThrow(
674
- testData.apiSchemaConfigPairWrongChoice.schema.apiType.validations[1]
675
- .error
676
- );
677
- });
678
-
679
- /**
680
- * @target validateSchema should not throw exception when "bigint" field is
681
- * passed in string format
682
- * @dependencies
683
- * @scenario
684
- * - call validateConfig with the config
685
- * - check if any exception is thrown
686
- * @expected
687
- * - exception should not be thrown
688
- */
689
- it(`should not throw exception when "bigint" field is passed in string
690
- format`, async () => {
691
- const confValidator = new ConfigValidator(
692
- <ConfigSchema>testData.apiSchemaConfigPairWithStringBigInt.schema
693
- );
694
-
695
- confValidator.validateConfig(
696
- testData.apiSchemaConfigPairWithStringBigInt.config
697
- );
698
- });
699
-
700
- /**
701
- * @target validateSchema should not throw exception when "number" field is
702
- * passed in string format
703
- * @dependencies
704
- * @scenario
705
- * - call validateConfig with the config
706
- * - check if any exception is thrown
707
- * @expected
708
- * - exception should not be thrown
709
- */
710
- it(`should not throw exception when "number" field is passed in string
711
- format`, async () => {
712
- const confValidator = new ConfigValidator(
713
- <ConfigSchema>testData.apiSchemaConfigPairWithStringNumber.schema
714
- );
715
-
716
- confValidator.validateConfig(
717
- testData.apiSchemaConfigPairWithStringNumber.config
718
- );
719
- });
720
-
721
- /**
722
- * @target validateConfig should throw exception when value doesn't match
723
- * the schema array type
724
- * @dependencies
725
- * @scenario
726
- * - call validateConfig with the config
727
- * - check if any exception is thrown
728
- * @expected
729
- * - exception should be thrown
730
- */
731
- it(`should throw exception when value doesn't match the schema array type`, async () => {
732
- const confValidator = new ConfigValidator(
733
- <ConfigSchema>testData.arraySchemaConfigPairWrongValueType.schema
734
- );
735
-
736
- expect(() =>
737
- confValidator.validateConfig(
738
- testData.arraySchemaConfigPairWrongValueType.config
739
- )
740
- ).toThrow();
741
- });
742
- });
743
-
744
- describe('valueAt', () => {
745
- /**
746
- * @target valueAt should return the value at specified path in config
747
- * object
748
- * @dependencies
749
- * @scenario
750
- * - call valueAt with the config and path
751
- * - check if correct value is returned
752
- * @expected
753
- * - correct value should be returned
754
- */
755
- it(`should return the value at specified path in config object`, async () => {
756
- const configObject = {
757
- apiType: 'explorer',
758
- servers: {
759
- url: 'node256.mydomain.net',
760
- },
761
- apis: {
762
- explorer: {
763
- url: 'example.com',
764
- port: 600,
765
- },
766
- },
767
- };
768
-
769
- const value = ConfigValidator.valueAt(configObject, [
770
- 'apis',
771
- 'explorer',
772
- 'url',
773
- ]);
774
-
775
- expect(value).toEqual('example.com');
776
- });
777
-
778
- /**
779
- * @target valueAt should return undefined when invalid path is passed
780
- * @dependencies
781
- * @scenario
782
- * - call valueAt with the config and path
783
- * - check if undefined is returned
784
- * @expected
785
- * - undefined should be returned
786
- */
787
- it(`should return undefined when invalid path is passed`, async () => {
788
- const configObject = {
789
- apiType: 'explorer',
790
- servers: {
791
- url: 'node256.mydomain.net',
792
- },
793
- apis: {
794
- explorer: {
795
- url: 'example.com',
796
- port: 600,
797
- },
798
- },
799
- };
800
-
801
- const value = ConfigValidator.valueAt(configObject, [
802
- 'apis',
803
- 'node',
804
- 'url',
805
- ]);
806
-
807
- expect(value).toEqual(undefined);
808
- });
809
- });
810
-
811
- describe('generateTSTypes', () => {
812
- /**
813
- * @target generateTSTypes should return TypeScript interfaces for
814
- * this.schema
815
- * @dependencies
816
- * @scenario
817
- * - call generateTSTypes
818
- * - check if correct types string is returned
819
- * @expected
820
- * - correct types string should be returned
821
- */
822
- it(`should return TypeScript interfaces for this.schema`, async () => {
823
- const confValidator = new ConfigValidator(
824
- <ConfigSchema>testData.schemaTypeScriptTypesPair.schema
825
- );
826
- const types = confValidator.generateTSTypes('Infrastructure');
827
- expect(types).toEqual(testData.schemaTypeScriptTypesPair.types);
828
- });
829
- });
830
-
831
- describe('getConfigForLevel', () => {
832
- /**
833
- * @target getConfigForLevel should return the correct characteristic object
834
- * for passed level of node config package
835
- * @dependencies
836
- * @scenario
837
- * - call getConfigForLevel
838
- * - check if correct characteristic object is returned
839
- * @expected
840
- * - correct characteristic object should be returned
841
- */
842
- it(`should return the correct characteristic object for passed level of node
843
- config package`, async () => {
844
- const confValidator = new ConfigValidator(
845
- <ConfigSchema>testData.schemaConfigCharPair.schema
846
- );
847
- const configCharacteristic = confValidator.getConfigForLevel(
848
- config,
849
- 'local'
850
- );
851
-
852
- expect(configCharacteristic).toEqual(
853
- testData.schemaConfigCharPair.characteristic
854
- );
855
- });
856
- });
857
-
858
- describe('validateAndWriteConfig', () => {
859
- /**
860
- * @target validateAndWriteConfig should validate config when merged with
861
- * passed object and write it to the appropriate config file
862
- * @dependencies
863
- * @scenario
864
- * - call validateAndWriteConfig
865
- * - check validateAndWriteConfig to throw no exception
866
- * - check the config file to be correctly saved
867
- * @expected
868
- * - validateAndWriteConfig should throw no exception
869
- * - the config file should be correctly saved
870
- */
871
- it(`should validate config when merged with passed object and write it to
872
- the appropriate config file`, async () => {
873
- const confValidator = new ConfigValidator(
874
- <ConfigSchema>testData.schemaConfigCharPair.schema
875
- );
876
- const obj = { apiType: 'node' };
877
- confValidator.validateAndWriteConfig(obj, config, 'default', 'json');
878
- const savedObj = JSON.parse(
879
- fs.readFileSync(path.join(configDir, 'default.json'), 'utf-8')
880
- );
881
-
882
- expect(savedObj).toEqual(obj);
883
- });
884
-
885
- /**
886
- * @target validateAndWriteConfig should throw exception when config after
887
- * being merged with passed object is not valid and preserve the original
888
- * config file
889
- * @dependencies
890
- * @scenario
891
- * - call validateAndWriteConfig
892
- * - check validateAndWriteConfig to throw exception
893
- * - check the config file to be unchanged
894
- * @expected
895
- * - validateAndWriteConfig should throw exception
896
- * - config file should be unchanged
897
- */
898
- it(`should throw exception when config after being merged with passed object
899
- is not valid and preserve the original config file`, async () => {
900
- const confValidator = new ConfigValidator(
901
- <ConfigSchema>testData.schemaConfigCharPair.schema
902
- );
903
- const originalObj = JSON.parse(
904
- fs.readFileSync(path.join(configDir, 'local.json'), 'utf-8')
905
- );
906
-
907
- const obj = { apiType: 'wrong-value' };
908
-
909
- expect(() =>
910
- confValidator.validateAndWriteConfig(obj, config, 'local', 'json')
911
- ).toThrow();
912
-
913
- const savedObj = JSON.parse(
914
- fs.readFileSync(path.join(configDir, 'local.json'), 'utf-8')
915
- );
916
- expect(savedObj).toEqual(originalObj);
917
- });
918
-
919
- /**
920
- * @target validateAndWriteConfig should not throw exception when config
921
- * after being merged with passed object is valid even if the passed object
922
- * itself is not valid
923
- * @dependencies
924
- * @scenario
925
- * - call validateAndWriteConfig
926
- * - check validateAndWriteConfig not to throw exception
927
- * - check the config file to be correctly saved
928
- * @expected
929
- * - validateAndWriteConfig should throw exception
930
- * - config file should be correctly saved
931
- */
932
- it(`should throw exception when config after being merged with passed object
933
- is not valid and preserve the original config file`, async () => {
934
- const confValidator = new ConfigValidator(
935
- <ConfigSchema>testData.schemaConfigCharPair.schema
936
- );
937
- const originalObj = JSON.parse(
938
- fs.readFileSync(path.join(configDir, 'local.json'), 'utf-8')
939
- );
940
-
941
- const obj = {
942
- servers: {
943
- url: 555,
944
- },
945
- };
946
-
947
- confValidator.validateAndWriteConfig(obj, config, 'local', 'json');
948
-
949
- const savedObj = JSON.parse(
950
- fs.readFileSync(path.join(configDir, 'local.json'), 'utf-8')
951
- );
952
- expect(savedObj).toEqual(obj);
953
- });
954
- });
955
- });