bv-ui-core 0.1.1

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.
Files changed (122) hide show
  1. package/.eslintrc +40 -0
  2. package/.travis.yml +5 -0
  3. package/CONTRIBUTING.md +98 -0
  4. package/LICENSE.md +13 -0
  5. package/OWNERS +2 -0
  6. package/README.md +63 -0
  7. package/catalog-info.yaml +30 -0
  8. package/karma.conf.js +130 -0
  9. package/lib/README.md +61 -0
  10. package/lib/api/.babelrc +3 -0
  11. package/lib/api/.eslintrc +9 -0
  12. package/lib/api/README.md +134 -0
  13. package/lib/api/index.js +15 -0
  14. package/lib/api/statistics.js +148 -0
  15. package/lib/application/README.md +96 -0
  16. package/lib/application/index.js +171 -0
  17. package/lib/body/README.md +13 -0
  18. package/lib/body/index.js +19 -0
  19. package/lib/bvFetch/README.md +101 -0
  20. package/lib/bvFetch/index.js +383 -0
  21. package/lib/checkHighContrast/README.md +26 -0
  22. package/lib/checkHighContrast/index.js +36 -0
  23. package/lib/cookie/README.md +29 -0
  24. package/lib/cookie/index.js +111 -0
  25. package/lib/cookieConsent/README.md +41 -0
  26. package/lib/cookieConsent/index.js +227 -0
  27. package/lib/cssLoadChecker/README.md +19 -0
  28. package/lib/cssLoadChecker/index.js +72 -0
  29. package/lib/date.now/README.md +11 -0
  30. package/lib/date.now/index.js +40 -0
  31. package/lib/domainPolice/README.md +66 -0
  32. package/lib/domainPolice/index.js +95 -0
  33. package/lib/evented/README.md +42 -0
  34. package/lib/evented/index.js +135 -0
  35. package/lib/getOriginalConstructor/README.md +16 -0
  36. package/lib/getOriginalConstructor/index.js +52 -0
  37. package/lib/global/README.md +10 -0
  38. package/lib/global/index.js +48 -0
  39. package/lib/ie/README.md +13 -0
  40. package/lib/ie/index.js +25 -0
  41. package/lib/loader/README.md +73 -0
  42. package/lib/loader/index.js +358 -0
  43. package/lib/logger/README.md +82 -0
  44. package/lib/logger/index.js +180 -0
  45. package/lib/messageFormat/README.md +10 -0
  46. package/lib/messageFormat/index.js +42 -0
  47. package/lib/namespacer/README.md +57 -0
  48. package/lib/namespacer/index.js +98 -0
  49. package/lib/performance/README.md +35 -0
  50. package/lib/performance/entries.js +5 -0
  51. package/lib/performance/getEntries.js +42 -0
  52. package/lib/performance/getEntriesByName.js +61 -0
  53. package/lib/performance/getEntriesByType.js +55 -0
  54. package/lib/performance/index.js +11 -0
  55. package/lib/performance/mark.js +97 -0
  56. package/lib/performance/measure.js +106 -0
  57. package/lib/performance/now.js +63 -0
  58. package/lib/pixelsDisplayed/README.md +19 -0
  59. package/lib/pixelsDisplayed/index.js +113 -0
  60. package/lib/polyfills/README.md +39 -0
  61. package/lib/polyfills/fetch/Headers.js +3 -0
  62. package/lib/polyfills/fetch/Request.js +3 -0
  63. package/lib/polyfills/fetch/Response.js +3 -0
  64. package/lib/polyfills/fetch/fetch.js +3 -0
  65. package/lib/polyfills/fetch/index.js +57 -0
  66. package/lib/polyfills/promise.js +265 -0
  67. package/lib/queryShadowDom/README.md +50 -0
  68. package/lib/queryShadowDom/index.js +93 -0
  69. package/lib/region/README.md +64 -0
  70. package/lib/region/europe/index.js +94 -0
  71. package/lib/region/europe/territories.js +40 -0
  72. package/lib/region/index.js +123 -0
  73. package/lib/staticAssetLoader/README.md +116 -0
  74. package/lib/staticAssetLoader/index.js +248 -0
  75. package/lib/waitForBody/README.md +21 -0
  76. package/lib/waitForBody/index.js +36 -0
  77. package/mkdocs.yaml +5 -0
  78. package/package.json +55 -0
  79. package/test/.eslintrc +5 -0
  80. package/test/fixtures/lib.loader.document-is-wrong-in-ie8.js +5 -0
  81. package/test/fixtures/lib.loader.loadscript-attribute.js +3 -0
  82. package/test/fixtures/lib.loader.loadscript.js +3 -0
  83. package/test/fixtures/lib.loader.loadstylesheet.css +3 -0
  84. package/test/unit/api/.eslintrc +6 -0
  85. package/test/unit/api/index.spec.js +37 -0
  86. package/test/unit/api/response.json +67 -0
  87. package/test/unit/api/statistics.spec.js +218 -0
  88. package/test/unit/application/index.spec.js +313 -0
  89. package/test/unit/body/index.spec.js +15 -0
  90. package/test/unit/bvFetch/index.spec.js +246 -0
  91. package/test/unit/checkHighContrast/index.spec.js +16 -0
  92. package/test/unit/cookie/index.spec.js +191 -0
  93. package/test/unit/cookieConsent/index.spec.js +119 -0
  94. package/test/unit/cssLoadChecker/index.spec.js +75 -0
  95. package/test/unit/date.now/index.spec.js +21 -0
  96. package/test/unit/domainPolice/index.spec.js +141 -0
  97. package/test/unit/evented/index.spec.js +59 -0
  98. package/test/unit/getOriginalConstructor/index.spec.js +60 -0
  99. package/test/unit/global/index.spec.js +84 -0
  100. package/test/unit/ie/index.spec.js +14 -0
  101. package/test/unit/loader/index.spec.js +644 -0
  102. package/test/unit/logger/index.spec.js +141 -0
  103. package/test/unit/messageFormat/index.spec.js +31 -0
  104. package/test/unit/mochaInit.js +8 -0
  105. package/test/unit/namespacer/index.spec.js +100 -0
  106. package/test/unit/performance/getEntries.spec.js +30 -0
  107. package/test/unit/performance/getEntriesByName.spec.js +88 -0
  108. package/test/unit/performance/getEntriesByType.spec.js +48 -0
  109. package/test/unit/performance/index.spec.js +35 -0
  110. package/test/unit/performance/mark.spec.js +40 -0
  111. package/test/unit/performance/measure.spec.js +50 -0
  112. package/test/unit/performance/now.spec.js +21 -0
  113. package/test/unit/pixelsDisplayed/index.spec.js +271 -0
  114. package/test/unit/polyfills/fetch/Headers.js +12 -0
  115. package/test/unit/polyfills/fetch/Request.js +12 -0
  116. package/test/unit/polyfills/fetch/Response.js +12 -0
  117. package/test/unit/polyfills/fetch/fetch.js +12 -0
  118. package/test/unit/polyfills/fetch/index.js +61 -0
  119. package/test/unit/polyfills/promise.spec.js +48 -0
  120. package/test/unit/region/europe/index.spec.js +70 -0
  121. package/test/unit/region/index.spec.js +113 -0
  122. package/test/unit/staticAssetLoader/index.spec.js +215 -0
@@ -0,0 +1,644 @@
1
+ /* eslint no-use-before-define: 0 */
2
+ /**
3
+ * @fileOverview
4
+ * Unit tests for the loader module.
5
+ */
6
+
7
+ var global = require('../../../lib/global');
8
+ var loader = require('../../../lib/loader');
9
+ var namespacer = require('../../../lib/namespacer');
10
+
11
+ describe('lib/loader', function () {
12
+
13
+ var doc;
14
+ var canaryEl;
15
+ var links;
16
+ var originalLinkCount;
17
+ var TESTING_NAMESPACE_NAME = 'BV_LOADER_TESTING';
18
+ var namespace = namespacer.namespace(TESTING_NAMESPACE_NAME);
19
+
20
+ beforeEach(function () {
21
+ doc = global.document;
22
+ links = doc.getElementsByTagName('link');
23
+ originalLinkCount = links.length;
24
+
25
+ loader._clearLoadedUrls();
26
+
27
+ canaryEl = doc.createElement('div');
28
+ doc.body.appendChild(canaryEl);
29
+ canaryEl.id = 'canary';
30
+ });
31
+
32
+ afterEach(function () {
33
+ links = doc.getElementsByTagName('link');
34
+ var newLinks = links.length - originalLinkCount;
35
+
36
+ for (var i=0; i<newLinks; i++) {
37
+ var link = links[originalLinkCount];
38
+ link.parentNode.removeChild(link);
39
+ }
40
+
41
+ // Remove canary element.
42
+ canaryEl.parentNode.removeChild(canaryEl);
43
+ });
44
+
45
+ describe('loadScript', function () {
46
+
47
+ describe('arguments', function () {
48
+
49
+ it('url is required', function () {
50
+ expect(function () {
51
+ loader.loadScript();
52
+ }).to.throw(/`url` must be a string/);
53
+ });
54
+
55
+ it('url must be a string', function () {
56
+ expect(function () {
57
+ loader.loadScript(123);
58
+ }).to.throw(/`url` must be a string/);
59
+ });
60
+
61
+ describe('options', function () {
62
+
63
+ it('argument is optional', function (done) {
64
+ global.libLoaderTestCallback = function () {
65
+ global.libLoaderTestCallback = function () {};
66
+ done();
67
+ };
68
+
69
+ expect(function () {
70
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js');
71
+ }).to.not.throw(Error);
72
+ });
73
+
74
+ it('argument is optional when callback is provided', function (done) {
75
+ global.libLoaderTestCallback = function () {};
76
+
77
+ expect(function () {
78
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', null, function () {
79
+ done();
80
+ });
81
+ }).to.not.throw(Error);
82
+ });
83
+
84
+ it('options is optional', function (done) {
85
+ global.libLoaderTestCallback = function () {
86
+ global.libLoaderTestCallback = function () {};
87
+ done();
88
+ };
89
+
90
+ expect(function () {
91
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', {});
92
+ }).to.not.throw(Error);
93
+ });
94
+
95
+ it('options.timeout must be a number', function () {
96
+ expect(function () {
97
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', {
98
+ timeout: '123'
99
+ });
100
+ }).to.throw(/`options.timeout` must be a number/);
101
+ });
102
+
103
+ it('options.namespaceName must be a string', function () {
104
+ expect(function () {
105
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', {
106
+ namespaceName: 123
107
+ });
108
+ }).to.throw(/`options.namespaceName` must be a string/);
109
+ });
110
+
111
+ it('options.forceLoad must be a boolean', function () {
112
+ expect(function () {
113
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', {
114
+ forceLoad: '123'
115
+ });
116
+ }).to.throw(/`options.forceLoad` must be a boolean/);
117
+ });
118
+
119
+ it('options.attributes sets attributes on the script tag', function (done) {
120
+ global.libLoaderAttributeTestCallback = function () {
121
+ global.libLoaderAttributeTestCallback = function () {};
122
+ expect(doc.querySelector('script[data-main="foo.js"]')).to.not.equal(null);
123
+ done();
124
+ };
125
+
126
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript-attribute.js', {
127
+ attributes: {
128
+ 'data-main': 'foo.js'
129
+ }
130
+ });
131
+ });
132
+
133
+ });
134
+
135
+ it('callback is optional', function (done) {
136
+ global.libLoaderTestCallback = function () {
137
+ done();
138
+ };
139
+ expect(function () {
140
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', {});
141
+ }).to.not.throw(Error);
142
+ });
143
+
144
+ it('callback must be a function', function () {
145
+ expect(function () {
146
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', {}, 123);
147
+ }).to.throw(/`callback` must be a function/);
148
+ });
149
+
150
+ it('doesn\'t try to execute non-existent callback on second load', function () {
151
+ expect(function () {
152
+ window.BV = window.BV || {}
153
+ window.BV.loadedUrls = {
154
+ '/base/test/fixtures/lib.loader.loadscript.js': true
155
+ }
156
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', {
157
+ namespaceName: 'BV',
158
+ forceLoad: false
159
+ });
160
+ }).to.not.throw(Error);
161
+ });
162
+ });
163
+
164
+ it('loads the script at `url`', function (done) {
165
+ global.libLoaderTestCallback = function () {
166
+ global.libLoaderTestCallback = function () {};
167
+ clearTimeout(timeout);
168
+ done();
169
+ };
170
+
171
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js');
172
+ var timeout = setTimeout(function () {
173
+ global.libLoaderTestCallback = function () {};
174
+ done(new Error('script load timed out'));
175
+ }, 1000);
176
+ });
177
+
178
+ it('loads the script on a later turn of the event loop', function (done) {
179
+ var later = false;
180
+ var timeout;
181
+
182
+ setTimeout(function () {
183
+ later = true;
184
+ }, 0);
185
+
186
+ global.libLoaderTestCallback = function () {
187
+ clearTimeout(timeout);
188
+ expect(later).to.equal(true);
189
+ global.libLoaderTestCallback = function () {};
190
+ done();
191
+ };
192
+
193
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js?');
194
+ timeout = setTimeout(function () {
195
+ global.libLoaderTestCallback = function () {};
196
+ done(new Error('script load timed out'));
197
+ }, 1000);
198
+ });
199
+
200
+ it('calls back on success', function (done) {
201
+ var timeout;
202
+ global.libLoaderTestCallback = function () {};
203
+
204
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', function () {
205
+ clearTimeout(timeout);
206
+ done();
207
+ });
208
+
209
+ timeout = setTimeout(function () {
210
+ done(new Error('script load timed out'));
211
+ }, 1000);
212
+ });
213
+
214
+ it('executes callback after the script', function (done) {
215
+ var timeout;
216
+ var scriptExecuted = false;
217
+
218
+ global.libLoaderTestCallback = function () {
219
+ scriptExecuted = true;
220
+ };
221
+
222
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', function () {
223
+ clearTimeout(timeout);
224
+ expect(scriptExecuted).to.equal(true);
225
+ done();
226
+ });
227
+
228
+ timeout = setTimeout(function () {
229
+ done(new Error('script load timed out'));
230
+ }, 1000);
231
+ });
232
+
233
+ it('loads same script once if forceLoad flag not set', function (done) {
234
+ var timeout;
235
+ var scriptExecuted = 0;
236
+
237
+ global.libLoaderTestCallback = function () {
238
+ scriptExecuted = 1;
239
+ };
240
+
241
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', function () {
242
+ expect(scriptExecuted).to.equal(1);
243
+
244
+ global.libLoaderTestCallback = function () {
245
+ scriptExecuted = 2;
246
+ };
247
+
248
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', function () {
249
+ clearTimeout(timeout);
250
+ expect(scriptExecuted).to.equal(1);
251
+ done();
252
+ });
253
+ });
254
+
255
+ timeout = setTimeout(function () {
256
+ done(new Error('script load timed out'));
257
+ }, 2000);
258
+ });
259
+
260
+ it('loads same script multiple times if forceLoad flag set', function (done) {
261
+ var timeout;
262
+ var scriptExecuted = 0;
263
+
264
+ global.libLoaderTestCallback = function () {
265
+ scriptExecuted = 1;
266
+ };
267
+
268
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', function () {
269
+ expect(scriptExecuted).to.equal(1);
270
+
271
+ global.libLoaderTestCallback = function () {
272
+ scriptExecuted = 2;
273
+ };
274
+
275
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', {
276
+ forceLoad: true
277
+ }, function () {
278
+ clearTimeout(timeout);
279
+ expect(scriptExecuted).to.equal(2);
280
+ done();
281
+ });
282
+ });
283
+
284
+ timeout = setTimeout(function () {
285
+ done(new Error('script load timed out'));
286
+ }, 2000);
287
+ });
288
+
289
+ it('loads same script in specified namespaceName', function (done) {
290
+ var scriptExecuted = 0;
291
+ var url = '/base/test/fixtures/lib.loader.loadscript.js';
292
+
293
+ global.libLoaderTestCallback = function () {
294
+ scriptExecuted = 1;
295
+ };
296
+
297
+ loader.loadScript(url, {
298
+ namespaceName: TESTING_NAMESPACE_NAME
299
+ }, function () {
300
+ expect(scriptExecuted).to.equal(1);
301
+ expect(namespace.loadedUrls[url]).to.equal(true);
302
+
303
+ global.libLoaderTestCallback = function () {
304
+ scriptExecuted = 2;
305
+ };
306
+
307
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', {
308
+ namespaceName: TESTING_NAMESPACE_NAME
309
+ }, function () {
310
+ clearTimeout(timeout);
311
+ expect(scriptExecuted).to.equal(1);
312
+ done();
313
+ });
314
+
315
+ });
316
+
317
+ var timeout = setTimeout(function () {
318
+ done(new Error('script load timed out'));
319
+ }, 2000);
320
+ });
321
+
322
+ it('script should not be added to list if failed to load', function (done) {
323
+ // skip this test in IE8 and jsdom
324
+ if (global.attachEvent || global.isJsdom) {
325
+ return done();
326
+ }
327
+
328
+ var timeout;
329
+ var invalidUrl = '/no.such.script.js';
330
+
331
+ loader.loadScript(invalidUrl, {
332
+ namespaceName: TESTING_NAMESPACE_NAME
333
+ }, function (err) {
334
+ clearTimeout(timeout);
335
+ expect(err).to.be.instanceof(Error);
336
+ expect(err.message).to.equal('Error: could not load /no.such.script.js');
337
+ expect(namespace.loadedUrls[invalidUrl]).to.equal(undefined);
338
+ done();
339
+ });
340
+
341
+ timeout = setTimeout(function () {
342
+ done(new Error('script load timed out'));
343
+ }, 1000);
344
+ });
345
+
346
+ it('calls back on failure', function (done) {
347
+ // skip this test in IE8 and jsdom
348
+ if (global.attachEvent || global.isJsdom) {
349
+ return done();
350
+ }
351
+
352
+ var timeout;
353
+
354
+ loader.loadScript('/no.such.script.js', {
355
+ timeout: 1000
356
+ }, function (err) {
357
+ expect(err).to.be.instanceof(Error);
358
+ expect(err.message).to.equal('Error: could not load /no.such.script.js');
359
+ clearTimeout(timeout);
360
+ done();
361
+ });
362
+
363
+ timeout = setTimeout(function () {
364
+ done(new Error('script load error timed out'));
365
+ }, 1100);
366
+ });
367
+
368
+ it('`document` has correct value in loaded script', function (done) {
369
+ global.libLoaderTestCallback = function (providedDoc) {
370
+ expect(providedDoc).to.equal(doc);
371
+ done();
372
+ };
373
+
374
+ loader.loadScript('/base/test/fixtures/lib.loader.document-is-wrong-in-ie8.js');
375
+ });
376
+
377
+ });
378
+
379
+ describe('loadStyleSheet', function () {
380
+
381
+ describe('arguments', function () {
382
+
383
+ it('url is required', function () {
384
+ expect(function () {
385
+ loader.loadStyleSheet();
386
+ }).to.throw(/`url` must be a string/);
387
+ });
388
+
389
+ it('url must be a string', function () {
390
+ expect(function () {
391
+ loader.loadStyleSheet(123);
392
+ }).to.throw(/`url` must be a string/);
393
+ });
394
+
395
+ it('options', function () {
396
+
397
+ it('argument is optional', function () {
398
+ expect(function () {
399
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css');
400
+ }).to.not.throw(Error);
401
+ });
402
+
403
+ it('argument is optional when callback is provided', function () {
404
+ expect(function () {
405
+ loader.loadStyleSheet(
406
+ '/base/test/fixtures/lib.loader.loadstylesheet.css',
407
+ null,
408
+ function () {
409
+ test.done();
410
+ });
411
+ }).to.not.throw(Error);
412
+ });
413
+
414
+ it('options is optional', function () {
415
+ expect(function () {
416
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', {});
417
+ }).to.not.throw(Error);
418
+ });
419
+
420
+ it('options.timeout must be a number', function () {
421
+ expect(function () {
422
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', {
423
+ timeout: '123'
424
+ });
425
+ }).to.throw(/`options.timeout` must be a number/);
426
+ });
427
+
428
+ it('options.attributes sets attributes on the link tag', function (done) {
429
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', {
430
+ attributes: {
431
+ id: 'loaded-css'
432
+ }
433
+ }, function () {
434
+ expect(doc.querySelector('#loaded-css')).to.not.equal(null);
435
+ done();
436
+ });
437
+ });
438
+
439
+ it('options.injectionNode defines injection point for link tag', function (done) {
440
+ var container = doc.createElement('div');
441
+
442
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', {
443
+ injectionNode: container
444
+ }, function () {
445
+ test.ok(container.getElementsByTagName('link').length > 0);
446
+ test.done();
447
+ });
448
+ });
449
+
450
+ it('options.injectionNode must be a DOM node', function () {
451
+ expect(function () {
452
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', {
453
+ injectionNode: false
454
+ });
455
+ }).to.throw(/`options.injectionNode` must be a DOM node/);
456
+ });
457
+
458
+ it('options.injectionNode should throw if appending fails', function (done) {
459
+ var container = doc.createElement('div');
460
+
461
+ container.appendChild = function () {
462
+ throw new Error('Intentional sabotage!');
463
+ };
464
+
465
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', {
466
+ injectionNode: container
467
+ }, function (error) {
468
+ expect(error).to.be.instanceof(Error);
469
+ done();
470
+ });
471
+ });
472
+
473
+ it('options.namespaceName must be a string', function () {
474
+ expect(function () {
475
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', {
476
+ namespaceName: 123
477
+ });
478
+ }).to.throw(/`options.namespaceName` must be a string/);
479
+ });
480
+
481
+ it('options.forceLoad must be a boolean', function () {
482
+ expect(function () {
483
+ loader.loadScript('/base/test/fixtures/lib.loader.loadscript.js', {
484
+ forceLoad: '123'
485
+ });
486
+ }).to.throw(/`options.forceLoad` must be a boolean/);
487
+ });
488
+
489
+ });
490
+
491
+ it('callback is optional', function () {
492
+ expect(function () {
493
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', {});
494
+ }).to.not.throw(Error);
495
+ });
496
+
497
+ it('callback must be a function', function () {
498
+ expect(function () {
499
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', {}, 123);
500
+ }).to.throw(/`callback` must be a function/);
501
+ });
502
+
503
+ it('doesn\'t try to execute non-existent callback on second load', function () {
504
+ expect(function () {
505
+ window.BV = window.BV || {}
506
+ window.BV.loadedUrls = {
507
+ '/base/test/fixtures/lib.loader.loadstylesheet.css': true
508
+ }
509
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', {
510
+ namespaceName: 'BV',
511
+ forceLoad: false
512
+ });
513
+ }).to.not.throw(Error);
514
+ });
515
+
516
+ /*
517
+ // TODO: Fix this test; it fails, even though the actual functionality
518
+ // appears to work fine.
519
+ it('loads the stylesheet at `url`', function (done) {
520
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', function () {
521
+ expect(canaryEl.offsetWidth).to.equal(100);
522
+ done();
523
+ });
524
+ });
525
+ */
526
+
527
+ });
528
+
529
+ it('loads the stylesheet on a later turn of the event loop', function (done) {
530
+ // TODO: this test fails in PhantomJS; the callback is never called.
531
+ if (/PhantomJS/.test(global.navigator.userAgent)) {
532
+ return done();
533
+ }
534
+
535
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', done);
536
+ expect(canaryEl.offsetWidth).to.not.equal(100);
537
+ });
538
+
539
+ it('calls back on success', function (done) {
540
+ // TODO: this test fails in PhantomJS; the callback is never called.
541
+ if (/PhantomJS/.test(global.navigator.userAgent)) {
542
+ return done();
543
+ }
544
+
545
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', done);
546
+ });
547
+
548
+ it('calls back on failure', function (done) {
549
+ if (global.attachEvent) {
550
+ // Skip this test in IE8.
551
+ return done();
552
+ }
553
+
554
+ loader.loadStyleSheet('/no.such.stylesheet.css', {
555
+ timeout: 1000
556
+ }, function (error) {
557
+ clearTimeout(timeout);
558
+ expect(error).to.be.instanceof(Error);
559
+ expect(error.message).to.equal('Error: could not load /no.such.stylesheet.css');
560
+ done();
561
+ });
562
+
563
+ var timeout = setTimeout(function () {
564
+ done(new Error('stylesheet load error timed out'));
565
+ }, 1100);
566
+
567
+ });
568
+
569
+ it('loads same stylesheet once if forceLoad flag not set', function (done) {
570
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', function () {
571
+ var linkElements = doc.getElementsByTagName('link');
572
+ expect(linkElements.length).to.equal(originalLinkCount + 1);
573
+
574
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', function () {
575
+ linkElements = doc.getElementsByTagName('link');
576
+ expect(linkElements.length).to.equal(originalLinkCount + 1);
577
+ done();
578
+ });
579
+ });
580
+ });
581
+
582
+ it('loads same stylesheet multiple times if forceLoad flag set', function (done) {
583
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', function () {
584
+ var linkElements = doc.getElementsByTagName('link');
585
+ expect(linkElements.length).to.equal(originalLinkCount + 1);
586
+
587
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', {
588
+ forceLoad: true
589
+ }, function () {
590
+ linkElements = doc.getElementsByTagName('link');
591
+ expect(linkElements.length).to.equal(originalLinkCount + 2);
592
+ done();
593
+ });
594
+ });
595
+ });
596
+
597
+ it('loads same stylesheet in specified namespaceName', function (done) {
598
+ var url = '/base/test/fixtures/lib.loader.loadstylesheet.css';
599
+
600
+ loader.loadStyleSheet(url, {
601
+ namespaceName: TESTING_NAMESPACE_NAME
602
+ }, function () {
603
+ var linkElements = doc.getElementsByTagName('link');
604
+ expect(linkElements.length).to.equal(originalLinkCount + 1);
605
+
606
+ var namespace = namespacer.namespace(TESTING_NAMESPACE_NAME);
607
+ expect(namespace.loadedUrls[url]).to.equal(true);
608
+
609
+ loader.loadStyleSheet('/base/test/fixtures/lib.loader.loadstylesheet.css', {
610
+ namespaceName: TESTING_NAMESPACE_NAME
611
+ }, function () {
612
+ linkElements = doc.getElementsByTagName('link');
613
+ expect(linkElements.length).to.equal(originalLinkCount + 1);
614
+ done();
615
+ });
616
+ });
617
+ });
618
+
619
+ it('calls back on failure', function (done) {
620
+ if (global.attachEvent) {
621
+ // Skip this test in IE8.
622
+ return done();
623
+ }
624
+
625
+ var invalidUrl = '/no.such.stylesheet.css';
626
+
627
+ loader.loadStyleSheet(invalidUrl, {
628
+ namespaceName: TESTING_NAMESPACE_NAME
629
+ }, function (err) {
630
+ clearTimeout(timeout);
631
+ expect(err).to.be.instanceof(Error);
632
+ expect(err.message).to.equal('Error: could not load /no.such.stylesheet.css');
633
+ expect(namespace.loadedUrls[invalidUrl]).to.equal(undefined);
634
+ done();
635
+ });
636
+
637
+ var timeout = setTimeout(function () {
638
+ done(new Error('stylesheet load error timed out'));
639
+ }, 1000);
640
+
641
+ });
642
+
643
+ });
644
+ });