Skip to content

Commit 3438402

Browse files
author
Alex Ross
committed
loader tests
1 parent e0da19c commit 3438402

4 files changed

Lines changed: 40 additions & 28 deletions

File tree

segment.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,23 @@ angular.module('ngSegment').constant('segmentDefaultConfig', {
4949

5050
(function (module) {
5151

52-
var hasLoaded = false;
52+
function SegmentLoader(hasLoaded) {
5353

54-
function SegmentLoader() {
55-
56-
this.hasLoaded = function () {
57-
return hasLoaded;
58-
};
54+
this.hasLoaded = hasLoaded || false;
5955

6056
this.load = function (apiKey, delayMs) {
6157

62-
// Warn if analytics.js has already been loaded, because it most likely
58+
// If analytics.js has already been loaded, it most likely
6359
// means the user has made an error
64-
if (hasLoaded || window.analytics.initialized) {
65-
console.warn('Attempting to load Segment twice.');
60+
if (this.hasLoaded || window.analytics.initialized) {
61+
throw new Error('Attempting to load Segment twice.');
6662
} else {
6763

6864
// Only load if we've been given or have set an API key
6965
if (apiKey) {
7066

7167
// Prevent double .load() calls
72-
hasLoaded = true;
68+
this.hasLoaded = true;
7369

7470
window.setTimeout(function () {
7571

@@ -91,7 +87,7 @@ angular.module('ngSegment').constant('segmentDefaultConfig', {
9187
first.parentNode.insertBefore(script, first);
9288
}, delayMs);
9389
} else {
94-
throw new Error('Cannot load Analytics.js without an API key');
90+
throw new Error('Cannot load Analytics.js without an API key.');
9591
}
9692
}
9793
};
@@ -103,7 +99,7 @@ angular.module('ngSegment').constant('segmentDefaultConfig', {
10399
SegmentLoader.call(this);
104100

105101
this.$get = function () {
106-
return new SegmentLoader();
102+
return new SegmentLoader(this.hasLoaded);
107103
};
108104
}
109105

segment.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/loader.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
(function (module) {
22

3-
var hasLoaded = false;
3+
function SegmentLoader(hasLoaded) {
44

5-
function SegmentLoader() {
6-
7-
this.hasLoaded = function () {
8-
return hasLoaded;
9-
};
5+
this.hasLoaded = hasLoaded || false;
106

117
this.load = function (apiKey, delayMs) {
128

13-
// Warn if analytics.js has already been loaded, because it most likely
9+
// If analytics.js has already been loaded, it most likely
1410
// means the user has made an error
15-
if (hasLoaded || window.analytics.initialized) {
16-
console.warn('Attempting to load Segment twice.');
11+
if (this.hasLoaded || window.analytics.initialized) {
12+
throw new Error('Attempting to load Segment twice.');
1713
} else {
1814

1915
// Only load if we've been given or have set an API key
2016
if (apiKey) {
2117

2218
// Prevent double .load() calls
23-
hasLoaded = true;
19+
this.hasLoaded = true;
2420

2521
window.setTimeout(function () {
2622

@@ -42,7 +38,7 @@
4238
first.parentNode.insertBefore(script, first);
4339
}, delayMs);
4440
} else {
45-
throw new Error('Cannot load Analytics.js without an API key');
41+
throw new Error('Cannot load Analytics.js without an API key.');
4642
}
4743
}
4844
};
@@ -54,7 +50,7 @@
5450
SegmentLoader.call(this);
5551

5652
this.$get = function () {
57-
return new SegmentLoader();
53+
return new SegmentLoader(this.hasLoaded);
5854
};
5955
}
6056

test/segmentLoaderSpec.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
describe('segmentLoader', function () {
22
'use strict';
33

4-
beforeEach(module('ngSegment'));
4+
beforeEach(function () {
5+
module('ngSegment');
6+
window.analytics.initialized = false;
7+
});
58

69
it('should require an API key to load Analytics.js', function () {
7-
10+
inject(function (segmentLoader) {
11+
var error = new Error('Cannot load Analytics.js without an API key.');
12+
expect(segmentLoader.load).toThrow(error);
13+
});
814
});
915

1016
it('should not allow loading Analytics.js twice', function () {
17+
inject(function (segmentLoader) {
18+
segmentLoader.load('abc');
19+
expect(function () {
20+
segmentLoader.load('abc');
21+
}).toThrow(new Error('Attempting to load Segment twice.'));
22+
});
23+
});
1124

25+
it('should not allow loading Analytics.js if it has already been included', function () {
26+
inject(function (segment, segmentLoader) {
27+
window.analytics.initialized = true;
28+
expect(function () {
29+
segmentLoader.load('abc');
30+
}).toThrow(new Error('Attempting to load Segment twice.'));
31+
});
1232
});
1333

1434
it('should not load Analytics.js automatically if autoload is not enabled', function () {
@@ -17,7 +37,7 @@ describe('segmentLoader', function () {
1737
});
1838

1939
inject(function (segment, segmentLoader) {
20-
expect(segmentLoader.hasLoaded()).toEqual(false);
40+
expect(segmentLoader.hasLoaded).toEqual(false);
2141
});
2242
});
2343
});

0 commit comments

Comments
 (0)