forked from urish/angular-load
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.js
143 lines (127 loc) · 5.35 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
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
/* 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 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('#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);
});
});
});