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
|
/* global ClassSpecHelper */
/*= require lib/utils/load_script */
/*= require class_spec_helper */
describe('LoadScript', () => {
const global = window.gl || (window.gl = {});
const LoadScript = global.LoadScript;
it('should be defined in the global scope', () => {
expect(LoadScript).toBeDefined();
});
describe('.load', () => {
ClassSpecHelper.itShouldBeAStaticMethod(LoadScript, 'load');
it('should reject if no source argument is provided', () => {
spyOn(Promise, 'reject');
LoadScript.load();
expect(Promise.reject).toHaveBeenCalledWith('source url must be defined');
});
it('should reject if the script id already exists', () => {
spyOn(Promise, 'reject');
spyOn(document, 'querySelector').and.returnValue({});
LoadScript.load('src.js', 'src-id');
expect(Promise.reject).toHaveBeenCalledWith('script id already exists');
});
it('should return a promise on completion', () => {
expect(LoadScript.load('src.js')).toEqual(jasmine.any(Promise));
});
it('should call appendScript when the promise is constructed', () => {
spyOn(LoadScript, 'appendScript');
LoadScript.load('src.js', 'src-id');
expect(LoadScript.appendScript).toHaveBeenCalledWith('src.js', 'src-id', jasmine.any(Promise.resolve.constructor), jasmine.any(Promise.reject.constructor));
});
});
describe('.appendScript', () => {
beforeEach(() => {
spyOn(document.body, 'appendChild');
});
ClassSpecHelper.itShouldBeAStaticMethod(LoadScript, 'appendScript');
describe('when called', () => {
let mockScriptTag;
beforeEach(() => {
mockScriptTag = {};
spyOn(document, 'createElement').and.returnValue(mockScriptTag);
LoadScript.appendScript('src.js', 'src-id', () => {}, () => {});
});
it('should create a script tag', () => {
expect(document.createElement).toHaveBeenCalledWith('script');
});
it('should set the MIME type', () => {
expect(mockScriptTag.type).toBe('text/javascript');
});
it('should set the script id', () => {
expect(mockScriptTag.id).toBe('src-id');
});
it('should set an onload handler', () => {
expect(mockScriptTag.onload).toEqual(jasmine.any(Function));
});
it('should set an onerror handler', () => {
expect(mockScriptTag.onerror).toEqual(jasmine.any(Function));
});
it('should set the src attribute', () => {
expect(mockScriptTag.src).toBe('src.js');
});
it('should append the script tag to the body element', () => {
expect(document.body.appendChild).toHaveBeenCalledWith(mockScriptTag);
});
});
it('should not set the script id if no id is provided', () => {
const mockScriptTag = {};
spyOn(document, 'createElement').and.returnValue(mockScriptTag);
LoadScript.appendScript('src.js', undefined);
expect(mockScriptTag.id).toBeUndefined();
});
});
});
|