webext-schema 6.0.0 → 7.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.
@@ -0,0 +1,1280 @@
1
+ /* api */
2
+ import { strict as assert } from 'node:assert';
3
+ import { describe, it } from 'mocha';
4
+
5
+ /* test */
6
+ import { Schema } from '../modules/schema.js';
7
+
8
+ describe('Schema', () => {
9
+ describe('construct', () => {
10
+ it('should be instance of Schema', () => {
11
+ const schema = new Schema();
12
+ assert.strictEqual(schema instanceof Schema, true, 'instance');
13
+ assert.strictEqual(schema.channel, 'beta', 'channel');
14
+ assert.strictEqual(typeof schema._sandbox, 'object', 'sandbox');
15
+ schema._sandbox.restore();
16
+ });
17
+
18
+ it('should be instance of Schema', () => {
19
+ const schema = new Schema('central');
20
+ assert.strictEqual(schema instanceof Schema, true, 'instance');
21
+ assert.strictEqual(schema.channel, 'central', 'channel');
22
+ assert.strictEqual(typeof schema._sandbox, 'object', 'sandbox');
23
+ schema._sandbox.restore();
24
+ });
25
+
26
+ it('should be instance of Schema', () => {
27
+ const config = {
28
+ injectInto: null,
29
+ properties: ['spy', 'stub', 'mock'],
30
+ useFakeTimers: true,
31
+ useFakeServer: true
32
+ };
33
+ const schema = new Schema(config);
34
+ assert.strictEqual(schema instanceof Schema, true, 'instance');
35
+ assert.strictEqual(schema.channel, 'beta', 'channel');
36
+ assert.strictEqual(typeof schema._sandbox, 'object', 'sandbox');
37
+ schema._sandbox.restore();
38
+ });
39
+
40
+ it('should be instance of Schema', () => {
41
+ const config = {
42
+ injectInto: null,
43
+ properties: ['spy', 'stub', 'mock'],
44
+ useFakeTimers: true,
45
+ useFakeServer: true
46
+ };
47
+ const schema = new Schema('central', config);
48
+ assert.strictEqual(schema instanceof Schema, true, 'instance');
49
+ assert.strictEqual(schema.channel, 'central', 'channel');
50
+ assert.strictEqual(typeof schema._sandbox, 'object', 'sandbox');
51
+ schema._sandbox.restore();
52
+ });
53
+ });
54
+
55
+ describe('getter', () => {
56
+ it('should get default value', () => {
57
+ const schema = new Schema();
58
+ assert.strictEqual(schema.channel, 'beta');
59
+ });
60
+
61
+ it('should get default value', () => {
62
+ const schema = new Schema('foo');
63
+ assert.strictEqual(schema.channel, 'beta');
64
+ });
65
+
66
+ it('should get value', () => {
67
+ const schema = new Schema('beta');
68
+ assert.strictEqual(schema.channel, 'beta');
69
+ });
70
+
71
+ it('should get value', () => {
72
+ const schema = new Schema('central');
73
+ assert.strictEqual(schema.channel, 'central');
74
+ });
75
+
76
+ it('should get value', () => {
77
+ const schema = new Schema('mail');
78
+ assert.strictEqual(schema.channel, 'mail');
79
+ });
80
+ });
81
+
82
+ describe('setter', () => {
83
+ it('should set default value', () => {
84
+ const schema = new Schema();
85
+ schema.channel = 1;
86
+ assert.strictEqual(schema.channel, 'beta');
87
+ });
88
+
89
+ it('should set default value', () => {
90
+ const schema = new Schema();
91
+ schema.channel = 'foo';
92
+ assert.strictEqual(schema.channel, 'beta');
93
+ });
94
+
95
+ it('should set value', () => {
96
+ const schema = new Schema();
97
+ schema.channel = 'central';
98
+ assert.strictEqual(schema.channel, 'central');
99
+ });
100
+
101
+ it('should set value', () => {
102
+ const schema = new Schema();
103
+ schema.channel = 'mail';
104
+ assert.strictEqual(schema.channel, 'mail');
105
+ });
106
+
107
+ it('should set value', () => {
108
+ const schema = new Schema('central');
109
+ schema.channel = 'beta';
110
+ assert.strictEqual(schema.channel, 'beta');
111
+ });
112
+ });
113
+
114
+ describe('get target from namespace', () => {
115
+ it('should get null', () => {
116
+ const schema = new Schema();
117
+ schema._browser = {};
118
+ const res = schema._getTargetFromNamespace();
119
+ assert.deepEqual(res, null, 'result');
120
+ });
121
+
122
+ it('should get null', () => {
123
+ const schema = new Schema();
124
+ schema._browser = {};
125
+ const res = schema._getTargetFromNamespace('');
126
+ assert.deepEqual(res, null, 'result');
127
+ });
128
+
129
+ it('should get null', () => {
130
+ const schema = new Schema();
131
+ schema._browser = {};
132
+ const res = schema._getTargetFromNamespace('foo');
133
+ assert.deepEqual(res, null, 'result');
134
+ });
135
+
136
+ it('should get target object', () => {
137
+ const schema = new Schema();
138
+ schema._browser = {
139
+ foo: {}
140
+ };
141
+ const res = schema._getTargetFromNamespace('foo');
142
+ assert.deepEqual(res, schema._browser.foo, 'result');
143
+ });
144
+
145
+ it('should get null', () => {
146
+ const schema = new Schema();
147
+ schema._browser = {
148
+ foo: {
149
+ bar: {}
150
+ }
151
+ };
152
+ const res = schema._getTargetFromNamespace('foo.baz');
153
+ assert.deepEqual(res, null, 'result');
154
+ });
155
+
156
+ it('should get target object', () => {
157
+ const schema = new Schema();
158
+ schema._browser = {
159
+ foo: {
160
+ bar: {}
161
+ }
162
+ };
163
+ const res = schema._getTargetFromNamespace('foo.bar');
164
+ assert.deepEqual(res, schema._browser.foo.bar, 'result');
165
+ });
166
+
167
+ it('should get target object', () => {
168
+ const schema = new Schema();
169
+ schema._browser = {
170
+ foo: {
171
+ bar: {
172
+ baz: {}
173
+ }
174
+ }
175
+ };
176
+ const res = schema._getTargetFromNamespace('foo.bar.baz');
177
+ assert.deepEqual(res, schema._browser.foo.bar.baz, 'result');
178
+ });
179
+ });
180
+
181
+ describe('assign import map', () => {
182
+ it('should not assign', () => {
183
+ const schema = new Schema();
184
+ schema._browser = {};
185
+ schema._assignImportMap();
186
+ assert.deepEqual(schema._browser, {}, 'browser');
187
+ assert.strictEqual(schema._importMap.size, 0, 'size');
188
+ });
189
+
190
+ it('should not assign', () => {
191
+ const schema = new Schema();
192
+ schema._browser = {
193
+ foo: {
194
+ baz: {}
195
+ },
196
+ bar: {}
197
+ };
198
+ schema._importMap.set('qux', {
199
+ $import: 'baz',
200
+ namespace: 'foo'
201
+ });
202
+ schema._assignImportMap();
203
+ assert.deepEqual(schema._browser, {
204
+ foo: {
205
+ baz: {}
206
+ },
207
+ bar: {
208
+ }
209
+ }, 'browser');
210
+ assert.strictEqual(schema._importMap.size, 0, 'size');
211
+ });
212
+
213
+ it('should assign', () => {
214
+ const schema = new Schema();
215
+ schema._browser = {
216
+ foo: {
217
+ baz: {}
218
+ },
219
+ bar: {}
220
+ };
221
+ schema._importMap.set('bar', {
222
+ $import: 'foo',
223
+ namespace: 'bar'
224
+ });
225
+ schema._assignImportMap();
226
+ assert.deepEqual(schema._browser, {
227
+ foo: {
228
+ baz: {}
229
+ },
230
+ bar: {
231
+ baz: {}
232
+ }
233
+ }, 'browser');
234
+ assert.strictEqual(schema._importMap.size, 0, 'size');
235
+ });
236
+
237
+ it('should assign', () => {
238
+ const schema = new Schema();
239
+ schema._browser = {
240
+ foo: {
241
+ baz: {
242
+ qux: {}
243
+ }
244
+ },
245
+ bar: {}
246
+ };
247
+ schema._importMap.set('bar', {
248
+ $import: 'foo.baz',
249
+ namespace: 'bar'
250
+ });
251
+ schema._assignImportMap();
252
+ assert.deepEqual(schema._browser, {
253
+ foo: {
254
+ baz: {
255
+ qux: {}
256
+ }
257
+ },
258
+ bar: {
259
+ qux: {}
260
+ }
261
+ }, 'browser');
262
+ assert.strictEqual(schema._importMap.size, 0, 'size');
263
+ });
264
+
265
+ it('should assign', () => {
266
+ const schema = new Schema();
267
+ schema._browser = {
268
+ foo: {
269
+ baz: {
270
+ qux: {}
271
+ }
272
+ },
273
+ bar: {
274
+ quux: {}
275
+ }
276
+ };
277
+ schema._importMap.set('bar.quux', {
278
+ $import: 'foo.baz',
279
+ namespace: 'bar'
280
+ });
281
+ schema._assignImportMap();
282
+ assert.deepEqual(schema._browser, {
283
+ foo: {
284
+ baz: {
285
+ qux: {}
286
+ }
287
+ },
288
+ bar: {
289
+ quux: {
290
+ qux: {}
291
+ }
292
+ }
293
+ }, 'browser');
294
+ assert.strictEqual(schema._importMap.size, 0, 'size');
295
+ });
296
+
297
+ it('should assign', () => {
298
+ const schema = new Schema();
299
+ schema._browser = {
300
+ foo: {
301
+ bar: {},
302
+ baz: {
303
+ qux: {}
304
+ }
305
+ }
306
+ };
307
+ schema._importMap.set('foo.bar', {
308
+ $import: 'baz',
309
+ namespace: 'foo'
310
+ });
311
+ schema._assignImportMap();
312
+ assert.deepEqual(schema._browser, {
313
+ foo: {
314
+ bar: {
315
+ qux: {}
316
+ },
317
+ baz: {
318
+ qux: {}
319
+ }
320
+ }
321
+ }, 'browser');
322
+ assert.strictEqual(schema._importMap.size, 0, 'size');
323
+ });
324
+ });
325
+
326
+ describe('assign referred map', () => {
327
+ it('should not assign', () => {
328
+ const schema = new Schema();
329
+ schema._browser = {};
330
+ schema._assignRefMap();
331
+ assert.deepEqual(schema._browser, {}, 'browser');
332
+ assert.strictEqual(schema._refMap.size, 0, 'size');
333
+ });
334
+
335
+ it('should not assign', () => {
336
+ const schema = new Schema();
337
+ schema._browser = {
338
+ foo: {
339
+ baz: {}
340
+ },
341
+ bar: {}
342
+ };
343
+ schema._refMap.set('qux', {
344
+ $ref: 'baz',
345
+ namespace: 'foo'
346
+ });
347
+ schema._assignRefMap();
348
+ assert.deepEqual(schema._browser, {
349
+ foo: {
350
+ baz: {}
351
+ },
352
+ bar: {
353
+ }
354
+ }, 'browser');
355
+ assert.strictEqual(schema._refMap.size, 0, 'size');
356
+ });
357
+
358
+ it('should assign', () => {
359
+ const schema = new Schema();
360
+ schema._browser = {
361
+ foo: {
362
+ baz: {}
363
+ },
364
+ bar: {}
365
+ };
366
+ schema._refMap.set('bar', {
367
+ $ref: 'foo',
368
+ namespace: 'bar'
369
+ });
370
+ schema._assignRefMap();
371
+ assert.deepEqual(schema._browser, {
372
+ foo: {
373
+ baz: {}
374
+ },
375
+ bar: {
376
+ baz: {}
377
+ }
378
+ }, 'browser');
379
+ assert.strictEqual(schema._refMap.size, 0, 'size');
380
+ });
381
+
382
+ it('should assign', () => {
383
+ const schema = new Schema();
384
+ schema._browser = {
385
+ foo: {
386
+ baz: {
387
+ qux: {}
388
+ }
389
+ },
390
+ bar: {}
391
+ };
392
+ schema._refMap.set('bar', {
393
+ $ref: 'foo.baz',
394
+ namespace: 'bar'
395
+ });
396
+ schema._assignRefMap();
397
+ assert.deepEqual(schema._browser, {
398
+ foo: {
399
+ baz: {
400
+ qux: {}
401
+ }
402
+ },
403
+ bar: {
404
+ qux: {}
405
+ }
406
+ }, 'browser');
407
+ assert.strictEqual(schema._refMap.size, 0, 'size');
408
+ });
409
+
410
+ it('should assign', () => {
411
+ const schema = new Schema();
412
+ schema._browser = {
413
+ foo: {
414
+ baz: {
415
+ qux: {}
416
+ }
417
+ },
418
+ bar: {
419
+ quux: {}
420
+ }
421
+ };
422
+ schema._refMap.set('bar.quux', {
423
+ $ref: 'foo.baz',
424
+ namespace: 'bar'
425
+ });
426
+ schema._assignRefMap();
427
+ assert.deepEqual(schema._browser, {
428
+ foo: {
429
+ baz: {
430
+ qux: {}
431
+ }
432
+ },
433
+ bar: {
434
+ quux: {
435
+ qux: {}
436
+ }
437
+ }
438
+ }, 'browser');
439
+ assert.strictEqual(schema._refMap.size, 0, 'size');
440
+ });
441
+
442
+ it('should assign', () => {
443
+ const schema = new Schema();
444
+ schema._browser = {
445
+ foo: {
446
+ bar: {},
447
+ baz: {
448
+ qux: {}
449
+ }
450
+ }
451
+ };
452
+ schema._refMap.set('foo.bar', {
453
+ $ref: 'baz',
454
+ namespace: 'foo'
455
+ });
456
+ schema._assignRefMap();
457
+ assert.deepEqual(schema._browser, {
458
+ foo: {
459
+ bar: {
460
+ qux: {}
461
+ },
462
+ baz: {
463
+ qux: {}
464
+ }
465
+ }
466
+ }, 'browser');
467
+ assert.strictEqual(schema._refMap.size, 0, 'size');
468
+ });
469
+ });
470
+
471
+ describe('mock events', () => {
472
+ it('should throw', () => {
473
+ const schema = new Schema();
474
+ assert.throws(() => schema._mockEvents());
475
+ });
476
+
477
+ it('should throw', () => {
478
+ const schema = new Schema();
479
+ assert.throws(() => schema._mockEvents({}));
480
+ });
481
+
482
+ it('should not add listeners', () => {
483
+ const schema = new Schema();
484
+ const res = schema._mockEvents({}, []);
485
+ assert.deepEqual(res, {}, 'result');
486
+ });
487
+
488
+ it('should not add listeners', () => {
489
+ const schema = new Schema();
490
+ const res = schema._mockEvents({}, [
491
+ {
492
+ name: 'foo',
493
+ type: 'function',
494
+ unsupported: true
495
+ }
496
+ ]);
497
+ assert.deepEqual(res, {}, 'result');
498
+ });
499
+
500
+ it('should not add listeners', () => {
501
+ const schema = new Schema();
502
+ const res = schema._mockEvents({}, [
503
+ {
504
+ name: 'foo',
505
+ type: 'object'
506
+ }
507
+ ]);
508
+ assert.deepEqual(res, {}, 'result');
509
+ });
510
+
511
+ it('should add listeners', () => {
512
+ const schema = new Schema();
513
+ const res = schema._mockEvents({}, [
514
+ {
515
+ name: 'foo',
516
+ type: 'function'
517
+ }
518
+ ]);
519
+ assert.strictEqual(typeof res, 'object', 'result');
520
+ assert.strictEqual(typeof res.foo, 'object', 'result');
521
+ assert.strictEqual(typeof res.foo.addListener, 'function', 'addListener');
522
+ assert.strictEqual(typeof res.foo.hasListener, 'function', 'hasListener');
523
+ assert.strictEqual(typeof res.foo.removeListener, 'function',
524
+ 'removeListener');
525
+ });
526
+
527
+ it('should add listeners', () => {
528
+ const schema = new Schema();
529
+ const res = schema._mockEvents({
530
+ foo: {}
531
+ }, [
532
+ {
533
+ name: 'foo',
534
+ type: 'function'
535
+ }
536
+ ]);
537
+ assert.strictEqual(typeof res, 'object', 'result');
538
+ assert.strictEqual(typeof res.foo, 'object', 'result');
539
+ assert.strictEqual(typeof res.foo.addListener, 'function', 'addListener');
540
+ assert.strictEqual(typeof res.foo.hasListener, 'function', 'hasListener');
541
+ assert.strictEqual(typeof res.foo.removeListener, 'function',
542
+ 'removeListener');
543
+ });
544
+ });
545
+
546
+ describe('mock functions', () => {
547
+ it('should throw', () => {
548
+ const schema = new Schema();
549
+ assert.throws(() => schema._mockFunctions());
550
+ });
551
+
552
+ it('should throw', () => {
553
+ const schema = new Schema();
554
+ assert.throws(() => schema._mockFunctions({}));
555
+ });
556
+
557
+ it('should not add functions', () => {
558
+ const schema = new Schema();
559
+ const res = schema._mockFunctions({}, []);
560
+ assert.deepEqual(res, {}, 'result');
561
+ });
562
+
563
+ it('should not add functions', () => {
564
+ const schema = new Schema();
565
+ const res = schema._mockFunctions({}, [
566
+ {
567
+ name: 'foo',
568
+ type: 'function',
569
+ unsupported: true
570
+ }
571
+ ]);
572
+ assert.deepEqual(res, {}, 'result');
573
+ });
574
+
575
+ it('should not add functions', () => {
576
+ const schema = new Schema();
577
+ const res = schema._mockFunctions({}, [
578
+ {
579
+ name: 'foo',
580
+ type: 'object'
581
+ }
582
+ ]);
583
+ assert.deepEqual(res, {}, 'result');
584
+ });
585
+
586
+ it('should add functions', () => {
587
+ const schema = new Schema();
588
+ const res = schema._mockFunctions({}, [
589
+ {
590
+ name: 'foo',
591
+ type: 'function'
592
+ }
593
+ ]);
594
+ assert.strictEqual(typeof res, 'object', 'result');
595
+ assert.strictEqual(typeof res.foo, 'function', 'function');
596
+ });
597
+
598
+ it('should add functions', () => {
599
+ const schema = new Schema();
600
+ const res = schema._mockFunctions({
601
+ foo: {}
602
+ }, [
603
+ {
604
+ name: 'foo',
605
+ type: 'function'
606
+ }
607
+ ]);
608
+ assert.strictEqual(typeof res, 'object', 'result');
609
+ assert.strictEqual(typeof res.foo, 'function', 'function');
610
+ });
611
+ });
612
+
613
+ describe('mock properties', () => {
614
+ it('should throw', () => {
615
+ const schema = new Schema();
616
+ assert.throws(() => schema._mockProperties());
617
+ });
618
+
619
+ it('should throw', () => {
620
+ const schema = new Schema();
621
+ assert.throws(() => schema._mockProperties({}));
622
+ });
623
+
624
+ it('should throw', () => {
625
+ const schema = new Schema();
626
+ assert.throws(() => schema._mockProperties({}, {}));
627
+ });
628
+
629
+ it('should not add properties', () => {
630
+ const schema = new Schema();
631
+ const res = schema._mockProperties({}, {}, 'foo');
632
+ assert.deepEqual(res, {}, 'result');
633
+ });
634
+
635
+ it('should not add properties', () => {
636
+ const schema = new Schema();
637
+ const res = schema._mockProperties({}, {
638
+ bar: {
639
+ name: 'baz',
640
+ type: 'function',
641
+ unsupported: true
642
+ }
643
+ }, 'foo');
644
+ assert.deepEqual(res, {}, 'result');
645
+ });
646
+
647
+ it('should set map', () => {
648
+ const schema = new Schema();
649
+ const res = schema._mockProperties({
650
+ }, {
651
+ bar: {
652
+ $ref: 'baz'
653
+ }
654
+ }, 'foo');
655
+ assert.deepEqual(res, {
656
+ bar: {}
657
+ }, 'result');
658
+ assert.deepEqual(Array.from(schema._refMap), [
659
+ [
660
+ 'foo.bar',
661
+ {
662
+ $ref: 'baz',
663
+ namespace: 'foo'
664
+ }
665
+ ]
666
+ ], 'map');
667
+ });
668
+
669
+ it('should add properties', () => {
670
+ const schema = new Schema();
671
+ const res = schema._mockProperties({
672
+ }, {
673
+ bar: {
674
+ value: -1
675
+ }
676
+ }, 'foo');
677
+ assert.deepEqual(res, {
678
+ bar: -1
679
+ }, 'result');
680
+ });
681
+
682
+ it('should add properties', () => {
683
+ const schema = new Schema();
684
+ const res = schema._mockProperties({
685
+ }, {
686
+ bar: {
687
+ type: 'function'
688
+ }
689
+ }, 'foo');
690
+ assert.strictEqual(typeof res, 'object', 'result');
691
+ assert.strictEqual(typeof res.bar, 'function', 'property');
692
+ });
693
+
694
+ it('should add properties', () => {
695
+ const schema = new Schema();
696
+ const res = schema._mockProperties({
697
+ }, {
698
+ bar: {
699
+ type: 'object'
700
+ }
701
+ }, 'foo');
702
+ assert.deepEqual(res, {
703
+ bar: {}
704
+ }, 'result');
705
+ });
706
+
707
+ it('should add properties', () => {
708
+ const schema = new Schema();
709
+ const res = schema._mockProperties({
710
+ bar: {}
711
+ }, {
712
+ bar: {
713
+ properties: {
714
+ baz: {
715
+ type: 'object'
716
+ }
717
+ }
718
+ }
719
+ }, 'foo');
720
+ assert.deepEqual(res, {
721
+ bar: {
722
+ baz: {}
723
+ }
724
+ }, 'result');
725
+ });
726
+
727
+ it('should add properties', () => {
728
+ const schema = new Schema();
729
+ const res = schema._mockProperties({
730
+ }, {
731
+ bar: {
732
+ type: 'string'
733
+ }
734
+ }, 'foo');
735
+ assert.deepEqual(res, {
736
+ bar: null
737
+ }, 'result');
738
+ });
739
+ });
740
+
741
+ describe('mock types', () => {
742
+ it('should throw', () => {
743
+ const schema = new Schema();
744
+ assert.throws(() => schema._mockTypes());
745
+ });
746
+
747
+ it('should throw', () => {
748
+ const schema = new Schema();
749
+ assert.throws(() => schema._mockTypes({}));
750
+ });
751
+
752
+ it('should throw', () => {
753
+ const schema = new Schema();
754
+ assert.throws(() => schema._mockTypes({}, []));
755
+ });
756
+
757
+ it('should not add types', () => {
758
+ const schema = new Schema();
759
+ const res = schema._mockTypes({}, [{}], 'foo');
760
+ assert.deepEqual(res, {}, 'result');
761
+ });
762
+
763
+ it('should set map', () => {
764
+ const schema = new Schema();
765
+ const res = schema._mockTypes({
766
+ }, [
767
+ {
768
+ $import: 'bar',
769
+ id: 'baz'
770
+ }
771
+ ], 'foo');
772
+ assert.deepEqual(res, {
773
+ baz: {}
774
+ }, 'result');
775
+ assert.deepEqual(Array.from(schema._importMap), [
776
+ [
777
+ 'foo.baz',
778
+ {
779
+ $import: 'bar',
780
+ namespace: 'foo'
781
+ }
782
+ ]
783
+ ], 'map');
784
+ });
785
+
786
+ it('should add types', () => {
787
+ const schema = new Schema();
788
+ const res = schema._mockTypes({
789
+ }, [
790
+ {
791
+ id: 'baz',
792
+ type: 'object'
793
+ }
794
+ ], 'foo');
795
+ assert.deepEqual(res, {
796
+ baz: {}
797
+ }, 'result');
798
+ });
799
+
800
+ it('should add types', () => {
801
+ const schema = new Schema();
802
+ const res = schema._mockTypes({
803
+ baz: {}
804
+ }, [
805
+ {
806
+ events: [],
807
+ functions: [],
808
+ id: 'baz',
809
+ properties: {},
810
+ type: 'object'
811
+ }
812
+ ], 'foo');
813
+ assert.deepEqual(res, {
814
+ baz: {}
815
+ }, 'result');
816
+ });
817
+ });
818
+
819
+ describe('parse schema content', () => {
820
+ it('should get object', () => {
821
+ const schema = new Schema();
822
+ const res = schema._parseSchemaContent();
823
+ assert.deepEqual(res, schema._schema, 'schema');
824
+ const items = Object.entries(res);
825
+ for (const [key, value] of items) {
826
+ assert.strictEqual(key.endsWith('.json'), true, `${key}`);
827
+ assert.strictEqual(Array.isArray(value), true, `${key} value`);
828
+ }
829
+ });
830
+
831
+ it('should get object', () => {
832
+ const schema = new Schema('beta');
833
+ const res = schema._parseSchemaContent();
834
+ assert.deepEqual(res, schema._schema, 'schema');
835
+ const items = Object.entries(res);
836
+ for (const [key, value] of items) {
837
+ assert.strictEqual(key.endsWith('.json'), true, `${key}`);
838
+ assert.strictEqual(Array.isArray(value), true, `${key} value`);
839
+ }
840
+ });
841
+
842
+ it('should get object', () => {
843
+ const schema = new Schema('central');
844
+ const res = schema._parseSchemaContent();
845
+ assert.deepEqual(res, schema._schema, 'schema');
846
+ const items = Object.entries(res);
847
+ for (const [key, value] of items) {
848
+ assert.strictEqual(key.endsWith('.json'), true, `${key}`);
849
+ assert.strictEqual(Array.isArray(value), true, `${key} value`);
850
+ }
851
+ });
852
+
853
+ it('should get object', () => {
854
+ const schema = new Schema('mail');
855
+ const res = schema._parseSchemaContent();
856
+ assert.deepEqual(res, schema._schema, 'schema');
857
+ const items = Object.entries(res);
858
+ for (const [key, value] of items) {
859
+ assert.strictEqual(key.endsWith('.json'), true, `${key}`);
860
+ assert.strictEqual(Array.isArray(value), true, `${key} value`);
861
+ }
862
+ });
863
+ });
864
+
865
+ describe('get schema', () => {
866
+ it('should throw', () => {
867
+ const schema = new Schema();
868
+ assert.throws(() => schema.get(), TypeError,
869
+ 'Expected String but got Undefined.');
870
+ });
871
+
872
+ it('should get null', () => {
873
+ const schema = new Schema();
874
+ const res = schema.get('foo');
875
+ assert.deepEqual(res, null, 'result');
876
+ });
877
+
878
+ it('should get array', () => {
879
+ const schema = new Schema();
880
+ const res = schema.get('browser_action.json');
881
+ assert.strictEqual(Array.isArray(res), true, 'result');
882
+ });
883
+
884
+ it('should get array', () => {
885
+ const schema = new Schema();
886
+ schema._parseSchemaContent();
887
+ const res = schema.get('browser_action.json');
888
+ assert.strictEqual(Array.isArray(res), true, 'result');
889
+ });
890
+
891
+ it('should get array', () => {
892
+ const schema = new Schema();
893
+ const res = schema.get('browser_action');
894
+ assert.strictEqual(Array.isArray(res), true, 'result');
895
+ });
896
+
897
+ it('should get array', () => {
898
+ const schema = new Schema();
899
+ const res = schema.get('browserAction');
900
+ assert.strictEqual(Array.isArray(res), true, 'result');
901
+ });
902
+ });
903
+
904
+ describe('get all schemas', () => {
905
+ it('should get object', () => {
906
+ const schema = new Schema();
907
+ schema._parseSchemaContent();
908
+ assert.strictEqual(typeof schema._schema, 'object', 'schema');
909
+ const res = schema.getAll();
910
+ assert.strictEqual(typeof res, 'object', 'result');
911
+ const keys = Object.keys(res);
912
+ for (const key of keys) {
913
+ assert.strictEqual(key.endsWith('.json'), true, 'key');
914
+ }
915
+ });
916
+
917
+ it('should get object', () => {
918
+ const schema = new Schema();
919
+ const res = schema.getAll();
920
+ assert.strictEqual(typeof res, 'object', 'result');
921
+ const keys = Object.keys(res);
922
+ for (const key of keys) {
923
+ assert.strictEqual(key.endsWith('.json'), true, 'key');
924
+ }
925
+ });
926
+ });
927
+
928
+ describe('list schemas', () => {
929
+ it('should get array', () => {
930
+ const schema = new Schema();
931
+ schema._parseSchemaContent();
932
+ assert.strictEqual(typeof schema._schema, 'object', 'schema');
933
+ const res = schema.list();
934
+ assert.strictEqual(Array.isArray(res), true, 'result');
935
+ for (const key of res) {
936
+ assert.strictEqual(key.endsWith('.json'), true, 'key');
937
+ }
938
+ });
939
+
940
+ it('should get array', () => {
941
+ const schema = new Schema();
942
+ const res = schema.list();
943
+ assert.strictEqual(Array.isArray(res), true, 'result');
944
+ for (const key of res) {
945
+ assert.strictEqual(key.endsWith('.json'), true, 'key');
946
+ }
947
+ });
948
+
949
+ it('should get array', () => {
950
+ const schema = new Schema('beta');
951
+ const res = schema.list();
952
+ assert.strictEqual(Array.isArray(res), true, 'result');
953
+ for (const key of res) {
954
+ assert.strictEqual(key.endsWith('.json'), true, 'key');
955
+ }
956
+ });
957
+
958
+ it('should get array', () => {
959
+ const schema = new Schema('central');
960
+ const res = schema.list();
961
+ assert.strictEqual(Array.isArray(res), true, 'result');
962
+ for (const key of res) {
963
+ assert.strictEqual(key.endsWith('.json'), true, 'key');
964
+ }
965
+ });
966
+ });
967
+
968
+ describe('mock browser api', () => {
969
+ it('should get stubbed api', () => {
970
+ const schema = new Schema();
971
+ schema._parseSchemaContent();
972
+ assert.strictEqual(typeof schema._schema, 'object', 'schema');
973
+ const browser = schema.mock();
974
+ assert.strictEqual(typeof browser, 'object', 'browser');
975
+ const {
976
+ bookmarks, browserAction, browserSettings, commands, contextMenus,
977
+ contextualIdentities, devtools, i18n, management, menus, notifications,
978
+ permissions, privacy, runtime, sessions, storage, tabs, theme, windows
979
+ } = browser;
980
+ const { inspectedWindow } = devtools;
981
+ assert.strictEqual(typeof bookmarks, 'object', 'bookmarks');
982
+ assert.strictEqual(typeof bookmarks.create, 'function',
983
+ 'bookmarks.create');
984
+ assert.strictEqual(typeof bookmarks.create.callCount, 'number',
985
+ 'stub bookmarks.create');
986
+ assert.strictEqual(typeof browserAction, 'object', 'browserAction');
987
+ assert.strictEqual(typeof browserAction.setTitle, 'function',
988
+ 'browserAction.setTitle');
989
+ assert.strictEqual(typeof browserAction.setTitle.callCount, 'number',
990
+ 'stub browserAction.setTitle');
991
+ assert.strictEqual(typeof browserAction.onClicked, 'object',
992
+ 'browserAction.onClicked');
993
+ assert.strictEqual(typeof browserAction.onClicked.addListener, 'function',
994
+ 'browserAction.onClicked.addListener');
995
+ assert.strictEqual(typeof browserAction.onClicked.addListener.callCount,
996
+ 'number', 'stub browserAction.onClicked.addListener.callCount');
997
+ assert.strictEqual(typeof browserSettings, 'object', 'browserSettings');
998
+ assert.strictEqual(typeof browserSettings.closeTabsByDoubleClick,
999
+ 'object', 'browserSettings.closeTabsByDoubleClick');
1000
+ assert.strictEqual(typeof commands, 'object', 'commands');
1001
+ assert.strictEqual(typeof commands.update, 'function',
1002
+ 'commands.update');
1003
+ assert.strictEqual(typeof commands.update.callCount, 'number',
1004
+ 'stub commands.update');
1005
+ assert.strictEqual(typeof contextMenus, 'object', 'contextMenus');
1006
+ assert.strictEqual(typeof contextMenus.create, 'function',
1007
+ 'contextMenus.create');
1008
+ assert.strictEqual(typeof contextMenus.create.callCount, 'number',
1009
+ 'stub contextMenus.create');
1010
+ assert.strictEqual(typeof contextualIdentities, 'object',
1011
+ 'contextualIdentities');
1012
+ assert.strictEqual(typeof contextualIdentities.get, 'function',
1013
+ 'contextualIdentities.get');
1014
+ assert.strictEqual(typeof contextualIdentities.get.callCount, 'number',
1015
+ 'stub contextualIdentities.get');
1016
+ assert.strictEqual(typeof devtools, 'object', 'devtools');
1017
+ assert.strictEqual(typeof inspectedWindow, 'object', 'inspectedWindow');
1018
+ assert.strictEqual(typeof inspectedWindow.reload, 'function',
1019
+ 'inspectedWindow.reload');
1020
+ assert.strictEqual(typeof inspectedWindow.reload.callCount, 'number',
1021
+ 'stub inspectedWindow.reload');
1022
+ assert.strictEqual(typeof i18n, 'object', 'i18n');
1023
+ assert.strictEqual(typeof i18n.getMessage, 'function',
1024
+ 'i18n.getMessage');
1025
+ assert.strictEqual(typeof i18n.getMessage.callCount, 'number',
1026
+ 'stub i18n.getMessage');
1027
+ assert.strictEqual(typeof management, 'object', 'management');
1028
+ assert.strictEqual(typeof management.get, 'function',
1029
+ 'management.get');
1030
+ assert.strictEqual(typeof management.get.callCount, 'number',
1031
+ 'stub management.get');
1032
+ assert.strictEqual(typeof menus, 'object', 'menus');
1033
+ assert.strictEqual(typeof menus.create, 'function',
1034
+ 'menus.create');
1035
+ assert.strictEqual(typeof menus.create.callCount, 'number',
1036
+ 'stub menus.create');
1037
+ assert.strictEqual(typeof menus.getTargetElement, 'function',
1038
+ 'menus.getTargetElement');
1039
+ assert.strictEqual(typeof menus.getTargetElement.callCount, 'number',
1040
+ 'stub menus.getTargetElement');
1041
+ assert.strictEqual(typeof menus.removeAll, 'function',
1042
+ 'menus.removeAll');
1043
+ assert.strictEqual(typeof menus.removeAll.callCount, 'number',
1044
+ 'stub menus.removeAll');
1045
+ assert.strictEqual(typeof notifications, 'object', 'notifications');
1046
+ assert.strictEqual(typeof notifications.create, 'function',
1047
+ 'notifications.create');
1048
+ assert.strictEqual(typeof notifications.create.callCount, 'number',
1049
+ 'stub notifications.create');
1050
+ assert.strictEqual(typeof notifications.onClosed, 'object',
1051
+ 'notifications.onClosed');
1052
+ assert.strictEqual(typeof notifications.onClosed.addListener, 'function',
1053
+ 'notifications.onClosed.addListener');
1054
+ assert.strictEqual(typeof notifications.onClosed.addListener.callCount,
1055
+ 'number', 'stub notifications.onClosed.addListener');
1056
+ assert.strictEqual(typeof permissions, 'object', 'permissions');
1057
+ assert.strictEqual(typeof permissions.request, 'function',
1058
+ 'permissions.request');
1059
+ assert.strictEqual(typeof permissions.request.callCount, 'number',
1060
+ 'stub permissions.request');
1061
+ assert.strictEqual(typeof privacy.network.tlsVersionRestrictionConfig,
1062
+ 'object', 'privacy.network.tlsVersionRestrictionConfig');
1063
+ assert.strictEqual(typeof runtime, 'object', 'runtime');
1064
+ assert.strictEqual(typeof runtime.connect, 'function',
1065
+ 'runtime.connect');
1066
+ assert.strictEqual(typeof runtime.connect.callCount, 'number',
1067
+ 'stub runtime.connect');
1068
+ assert.strictEqual(typeof runtime.Port, 'object', 'runtime.Port');
1069
+ assert.strictEqual(typeof runtime.Port.disconnect, 'function',
1070
+ 'runtime.Port.disconnect');
1071
+ assert.strictEqual(typeof runtime.Port.onDisconnect.addListener,
1072
+ 'function', 'runtime.Port.disconnect');
1073
+ assert.strictEqual(typeof runtime.Port.onDisconnect.addListener,
1074
+ 'function', 'runtime.Port.onDisconnect.addListener');
1075
+ assert.strictEqual(typeof sessions, 'object', 'sessions');
1076
+ assert.strictEqual(typeof sessions.getRecentlyClosed, 'function',
1077
+ 'sessions.getRecentlyClosed');
1078
+ assert.strictEqual(typeof sessions.getRecentlyClosed.callCount, 'number',
1079
+ 'stub sessions.getRecentlyClosed');
1080
+ assert.strictEqual(typeof storage, 'object', 'storage');
1081
+ assert.strictEqual(typeof storage.local, 'object', 'storage.local');
1082
+ assert.strictEqual(typeof storage.local.get, 'function',
1083
+ 'storage.local.get');
1084
+ assert.strictEqual(typeof storage.local.get.callCount, 'number',
1085
+ 'stub storage.local.get');
1086
+ assert.strictEqual(typeof storage.onChanged, 'object',
1087
+ 'storage.onChanged');
1088
+ assert.strictEqual(typeof storage.onChanged.addListener, 'function',
1089
+ 'storage.onChanged.addListener');
1090
+ assert.strictEqual(typeof storage.onChanged.addListener.callCount,
1091
+ 'number', 'stub storage.onChanged.addListener');
1092
+ assert.strictEqual(typeof tabs, 'object', 'tabs');
1093
+ assert.strictEqual(typeof tabs.get, 'function',
1094
+ 'tabs.get');
1095
+ assert.strictEqual(typeof tabs.get.callCount, 'number',
1096
+ 'stub tabs.get');
1097
+ assert.strictEqual(typeof theme, 'object', 'theme');
1098
+ assert.strictEqual(typeof theme.getCurrent, 'function',
1099
+ 'theme.getCurrent');
1100
+ assert.strictEqual(typeof theme.getCurrent.callCount, 'number',
1101
+ 'stub theme.getCurrent');
1102
+ assert.strictEqual(typeof windows, 'object', 'windows');
1103
+ assert.strictEqual(typeof windows.get, 'function',
1104
+ 'windows.get');
1105
+ assert.strictEqual(typeof windows.get.callCount, 'number',
1106
+ 'stub windows.get');
1107
+ });
1108
+
1109
+ it('should get stubbed api', () => {
1110
+ const schema = new Schema();
1111
+ const browser = schema.mock();
1112
+ assert.strictEqual(typeof browser, 'object', 'browser');
1113
+ const {
1114
+ bookmarks, browserAction, browserSettings, commands, contextMenus,
1115
+ contextualIdentities, devtools, i18n, management, menus, notifications,
1116
+ permissions, privacy, runtime, sessions, storage, tabs, theme, windows
1117
+ } = browser;
1118
+ const { inspectedWindow } = devtools;
1119
+ assert.strictEqual(typeof bookmarks, 'object', 'bookmarks');
1120
+ assert.strictEqual(typeof bookmarks.create, 'function',
1121
+ 'bookmarks.create');
1122
+ assert.strictEqual(typeof bookmarks.create.callCount, 'number',
1123
+ 'stub bookmarks.create');
1124
+ assert.strictEqual(typeof browserAction, 'object', 'browserAction');
1125
+ assert.strictEqual(typeof browserAction.setTitle, 'function',
1126
+ 'browserAction.setTitle');
1127
+ assert.strictEqual(typeof browserAction.setTitle.callCount, 'number',
1128
+ 'stub browserAction.setTitle');
1129
+ assert.strictEqual(typeof browserAction.onClicked, 'object',
1130
+ 'browserAction.onClicked');
1131
+ assert.strictEqual(typeof browserAction.onClicked.addListener, 'function',
1132
+ 'browserAction.onClicked.addListener');
1133
+ assert.strictEqual(typeof browserAction.onClicked.addListener.callCount,
1134
+ 'number', 'stub browserAction.onClicked.addListener.callCount');
1135
+ assert.strictEqual(typeof browserSettings, 'object', 'browserSettings');
1136
+ assert.strictEqual(typeof browserSettings.closeTabsByDoubleClick,
1137
+ 'object', 'browserSettings.closeTabsByDoubleClick');
1138
+ assert.strictEqual(typeof commands, 'object', 'commands');
1139
+ assert.strictEqual(typeof commands.update, 'function',
1140
+ 'commands.update');
1141
+ assert.strictEqual(typeof commands.update.callCount, 'number',
1142
+ 'stub commands.update');
1143
+ assert.strictEqual(typeof contextMenus, 'object', 'contextMenus');
1144
+ assert.strictEqual(typeof contextMenus.create, 'function',
1145
+ 'contextMenus.create');
1146
+ assert.strictEqual(typeof contextMenus.create.callCount, 'number',
1147
+ 'stub contextMenus.create');
1148
+ assert.strictEqual(typeof contextualIdentities, 'object',
1149
+ 'contextualIdentities');
1150
+ assert.strictEqual(typeof contextualIdentities.get, 'function',
1151
+ 'contextualIdentities.get');
1152
+ assert.strictEqual(typeof contextualIdentities.get.callCount, 'number',
1153
+ 'stub contextualIdentities.get');
1154
+ assert.strictEqual(typeof devtools, 'object', 'devtools');
1155
+ assert.strictEqual(typeof inspectedWindow, 'object', 'inspectedWindow');
1156
+ assert.strictEqual(typeof inspectedWindow.reload, 'function',
1157
+ 'inspectedWindow.reload');
1158
+ assert.strictEqual(typeof inspectedWindow.reload.callCount, 'number',
1159
+ 'stub inspectedWindow.reload');
1160
+ assert.strictEqual(typeof i18n, 'object', 'i18n');
1161
+ assert.strictEqual(typeof i18n.getMessage, 'function',
1162
+ 'i18n.getMessage');
1163
+ assert.strictEqual(typeof i18n.getMessage.callCount, 'number',
1164
+ 'stub i18n.getMessage');
1165
+ assert.strictEqual(typeof management, 'object', 'management');
1166
+ assert.strictEqual(typeof management.get, 'function',
1167
+ 'management.get');
1168
+ assert.strictEqual(typeof management.get.callCount, 'number',
1169
+ 'stub management.get');
1170
+ assert.strictEqual(typeof menus, 'object', 'menus');
1171
+ assert.strictEqual(typeof menus.create, 'function',
1172
+ 'menus.create');
1173
+ assert.strictEqual(typeof menus.create.callCount, 'number',
1174
+ 'stub menus.create');
1175
+ assert.strictEqual(typeof menus.getTargetElement, 'function',
1176
+ 'menus.getTargetElement');
1177
+ assert.strictEqual(typeof menus.getTargetElement.callCount, 'number',
1178
+ 'stub menus.getTargetElement');
1179
+ assert.strictEqual(typeof menus.removeAll, 'function',
1180
+ 'menus.removeAll');
1181
+ assert.strictEqual(typeof menus.removeAll.callCount, 'number',
1182
+ 'stub menus.removeAll');
1183
+ assert.strictEqual(typeof notifications, 'object', 'notifications');
1184
+ assert.strictEqual(typeof notifications.create, 'function',
1185
+ 'notifications.create');
1186
+ assert.strictEqual(typeof notifications.create.callCount, 'number',
1187
+ 'stub notifications.create');
1188
+ assert.strictEqual(typeof notifications.onClosed, 'object',
1189
+ 'notifications.onClosed');
1190
+ assert.strictEqual(typeof notifications.onClosed.addListener, 'function',
1191
+ 'notifications.onClosed.addListener');
1192
+ assert.strictEqual(typeof notifications.onClosed.addListener.callCount,
1193
+ 'number', 'stub notifications.onClosed.addListener');
1194
+ assert.strictEqual(typeof permissions, 'object', 'permissions');
1195
+ assert.strictEqual(typeof permissions.request, 'function',
1196
+ 'permissions.request');
1197
+ assert.strictEqual(typeof permissions.request.callCount, 'number',
1198
+ 'stub permissions.request');
1199
+ assert.strictEqual(typeof privacy.network.tlsVersionRestrictionConfig,
1200
+ 'object', 'privacy.network.tlsVersionRestrictionConfig');
1201
+ assert.strictEqual(typeof runtime, 'object', 'runtime');
1202
+ assert.strictEqual(typeof runtime.connect, 'function',
1203
+ 'runtime.connect');
1204
+ assert.strictEqual(typeof runtime.connect.callCount, 'number',
1205
+ 'stub runtime.connect');
1206
+ assert.strictEqual(typeof runtime.Port, 'object', 'runtime.Port');
1207
+ assert.strictEqual(typeof runtime.Port.disconnect, 'function',
1208
+ 'runtime.Port.disconnect');
1209
+ assert.strictEqual(typeof runtime.Port.onDisconnect.addListener,
1210
+ 'function', 'runtime.Port.disconnect');
1211
+ assert.strictEqual(typeof runtime.Port.onDisconnect.addListener,
1212
+ 'function', 'runtime.Port.onDisconnect.addListener');
1213
+ assert.strictEqual(typeof sessions, 'object', 'sessions');
1214
+ assert.strictEqual(typeof sessions.getRecentlyClosed, 'function',
1215
+ 'sessions.getRecentlyClosed');
1216
+ assert.strictEqual(typeof sessions.getRecentlyClosed.callCount, 'number',
1217
+ 'stub sessions.getRecentlyClosed');
1218
+ assert.strictEqual(typeof storage, 'object', 'storage');
1219
+ assert.strictEqual(typeof storage.local, 'object', 'storage.local');
1220
+ assert.strictEqual(typeof storage.local.get, 'function',
1221
+ 'storage.local.get');
1222
+ assert.strictEqual(typeof storage.local.get.callCount, 'number',
1223
+ 'stub storage.local.get');
1224
+ assert.strictEqual(typeof storage.onChanged, 'object',
1225
+ 'storage.onChanged');
1226
+ assert.strictEqual(typeof storage.onChanged.addListener, 'function',
1227
+ 'storage.onChanged.addListener');
1228
+ assert.strictEqual(typeof storage.onChanged.addListener.callCount,
1229
+ 'number', 'stub storage.onChanged.addListener');
1230
+ assert.strictEqual(typeof tabs, 'object', 'tabs');
1231
+ assert.strictEqual(typeof tabs.get, 'function',
1232
+ 'tabs.get');
1233
+ assert.strictEqual(typeof tabs.get.callCount, 'number',
1234
+ 'stub tabs.get');
1235
+ assert.strictEqual(typeof theme, 'object', 'theme');
1236
+ assert.strictEqual(typeof theme.getCurrent, 'function',
1237
+ 'theme.getCurrent');
1238
+ assert.strictEqual(typeof theme.getCurrent.callCount, 'number',
1239
+ 'stub theme.getCurrent');
1240
+ assert.strictEqual(typeof windows, 'object', 'windows');
1241
+ assert.strictEqual(typeof windows.get, 'function',
1242
+ 'windows.get');
1243
+ assert.strictEqual(typeof windows.get.callCount, 'number',
1244
+ 'stub windows.get');
1245
+ });
1246
+
1247
+ it('should access sandbox', () => {
1248
+ const browser = new Schema().mock();
1249
+ assert.strictEqual(typeof browser._sandbox, 'object', 'sandbox');
1250
+ assert.strictEqual(typeof browser._sandbox.stub, 'function',
1251
+ 'stub');
1252
+ const i = browser.runtime.connect.callCount;
1253
+ browser.runtime.connect();
1254
+ assert.strictEqual(browser.runtime.connect.callCount, i + 1, 'called');
1255
+ browser._sandbox.reset();
1256
+ assert.strictEqual(browser.runtime.connect.callCount, 0, 'reset');
1257
+ });
1258
+
1259
+ it('mock runtime.connect', () => {
1260
+ const browser = new Schema().mock();
1261
+ const mockConnect = browser.runtime.connect.callsFake(({ name }) => {
1262
+ const port = Object.assign({}, browser.runtime.Port);
1263
+ port.name = name;
1264
+ return port;
1265
+ });
1266
+
1267
+ const port1 = mockConnect({ name: 'foo' });
1268
+ const port2 = mockConnect({ name: 'bar' });
1269
+ assert.strictEqual(port1.name, 'foo', 'name');
1270
+ assert.strictEqual(port2.name, 'bar', 'name');
1271
+ assert.strictEqual(typeof port1.onDisconnect.addListener, 'function',
1272
+ 'function');
1273
+ assert.strictEqual(typeof port2.onDisconnect.addListener, 'function',
1274
+ 'function');
1275
+
1276
+ // reset
1277
+ mockConnect.reset();
1278
+ });
1279
+ });
1280
+ });