forked from urish/angular-load
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.js
101 lines (89 loc) · 3.59 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* License: MIT.
* Copyright (C) 2014, Uri Shaked.
*/
/* global describe, module, beforeEach, it, expect, jasmine, inject, spyOn */
'use strict';
describe('service angularLoad', function () {
beforeEach(module('angularLoad'));
var mockDocument;
var $timeout, angularLoad;
beforeEach(module(function($provide) {
mockDocument = {
createElement: angular.bind(document, document.createElement),
head: jasmine.createSpyObj('document.head', ['appendChild']),
body: jasmine.createSpyObj('document.body', ['appendChild'])
};
spyOn(mockDocument, 'createElement').and.callThrough();
$provide.value('$document', [mockDocument]);
}));
beforeEach(inject(function(_$timeout_, _angularLoad_) {
$timeout = _$timeout_;
angularLoad = _angularLoad_;
}));
describe('#loadScript', function() {
it('should append a new <' + 'script> element to the document body', function() {
angularLoad.loadScript('https://www.test.org/somescript.js');
expect(mockDocument.createElement).toHaveBeenCalledWith('script');
expect(mockDocument.body.appendChild).toHaveBeenCalled();
var scriptElement = mockDocument.body.appendChild.calls.mostRecent().args[0];
expect(scriptElement.tagName.toLowerCase()).toEqual('script');
expect(scriptElement.src).toEqual('https://www.test.org/somescript.js');
});
it('should resolve the returned promise as soon as the script has finished loading', function() {
var resolved = false;
angularLoad.loadScript('https://www.test.org/somescript.js').then(function() {
resolved = true;
});
var scriptElement = mockDocument.body.appendChild.calls.mostRecent().args[0];
scriptElement.onload({});
expect(resolved).toBeFalsy();
$timeout.flush();
expect(resolved).toBeTruthy();
});
it('should reject the returned promise if the script failed to load', function() {
var rejected = false;
angularLoad.loadScript('https://www.test.org/somescript.js').catch(function() {
rejected = true;
});
var scriptElement = mockDocument.body.appendChild.calls.mostRecent().args[0];
scriptElement.onerror({});
expect(rejected).toBeFalsy();
$timeout.flush();
expect(rejected).toBeTruthy();
});
});
describe('#loadCSS', function() {
it('should append a new <' + 'link> element to the document head', function() {
angularLoad.loadCSS('https://www.test.org/styles.css');
expect(mockDocument.createElement).toHaveBeenCalledWith('link');
expect(mockDocument.head.appendChild).toHaveBeenCalled();
var linkElement = mockDocument.head.appendChild.calls.mostRecent().args[0];
expect(linkElement.tagName.toLowerCase()).toEqual('link');
expect(linkElement.rel).toEqual('stylesheet');
expect(linkElement.type).toEqual('text/css');
expect(linkElement.href).toEqual('https://www.test.org/styles.css');
});
it('should resolve the returned promise as soon as the script has finished loading', function() {
var resolved = false;
angularLoad.loadCSS('https://www.test.org/styles.css').then(function() {
resolved = true;
});
var linkElement = mockDocument.head.appendChild.calls.mostRecent().args[0];
linkElement.onload({});
expect(resolved).toBeFalsy();
$timeout.flush();
expect(resolved).toBeTruthy();
});
it('should reject the returned promise if the script failed to load', function() {
var rejected = false;
angularLoad.loadCSS('https://www.test.org/styles.css').catch(function() {
rejected = true;
});
var linkElement = mockDocument.head.appendChild.calls.mostRecent().args[0];
linkElement.onerror({});
expect(rejected).toBeFalsy();
$timeout.flush();
expect(rejected).toBeTruthy();
});
});
});