This repository has been archived by the owner on Oct 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
index.test.js
171 lines (153 loc) · 6.47 KB
/
index.test.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import angular from 'angular';
import 'angular-mocks';
import ngLoad from './index';
describe('service angularLoad', function () {
beforeEach(angular.mock.module(ngLoad));
var mockDocument;
var $timeout, angularLoad;
beforeEach(angular.mock.module(function($provide) {
mockDocument = {
createElement: angular.bind(document, document.createElement),
head: jasmine.createSpyObj('document.head', ['appendChild', 'querySelector']),
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 when `onload` callback is fired', 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 resolve the returned promise as soon as the script has finished loading when `onreadystatechange` callback is fired', 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.readyState = 'loading';
scriptElement.onreadystatechange({});
expect(resolved).toBeFalsy();
scriptElement.readyState = 'complete';
scriptElement.onreadystatechange({});
$timeout.flush();
expect(resolved).toBeTruthy();
});
it('should resolve the returned promise as soon as the script has finished loading when `onreadystatechange` callback is fired in IE8', 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.readyState = 'loading';
scriptElement.onreadystatechange({});
expect(resolved).toBeFalsy();
scriptElement.readyState = 'loaded';
scriptElement.onreadystatechange({});
$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();
});
it('should only append the script tag once', function() {
angularLoad.loadScript('https://www.test.org/somescript.js');
angularLoad.loadScript('https://www.test.org/somescript.js');
expect(mockDocument.body.appendChild.calls.all().length).toBe(1);
});
});
describe('#unloadCSS', function() {
it('should remove the <' + 'link> element from the document head when exist and return true', function() {
var styleTag = {
//Style tag is found in the head
id: 'ElementObject',
remove: function(){}
};
spyOn(styleTag, 'remove');
mockDocument.head.querySelector.and.callFake(function() {
return styleTag;
});
var removed = angularLoad.unloadCSS('https://www.test.org/styles.css');
expect(removed).toBe(true);
expect(styleTag.remove).toHaveBeenCalled();
});
it('should not remove the <' + 'link> element from the document head when not exist and return false', function() {
mockDocument.head.querySelector.and.callFake(function() {
return null; //Style tag not found in the head
});
var removed = angularLoad.unloadCSS('https://www.test.org/styles.css');
expect(removed).toBe(false);
});
it('should not remove the <' + 'link> element from the document head when document head is empty and return false', function() {
mockDocument.head = null;
var removed = angularLoad.unloadCSS('https://www.test.org/styles.css');
expect(removed).toBe(false);
});
});
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();
});
it('should only append the stylesheet link once', function() {
angularLoad.loadCSS('https://www.test.org/styles.css');
angularLoad.loadCSS('https://www.test.org/styles.css');
expect(mockDocument.head.appendChild.calls.all().length).toBe(1);
});
});
});