Skip to content

Commit f332cf2

Browse files
author
Brad Berger
committed
Adds basic unit tests for scripts/stylesheets
1 parent 49c8914 commit f332cf2

11 files changed

Lines changed: 209 additions & 204 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
22
bower_components/
3+
reports/

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ do not support those features.
2020

2121
- `Array.isArray` [Browser support](http://kangax.github.io/compat-table/es5/#Array.isArray)
2222
- `Array.prototype.forEach` [Browser support](http://kangax.github.io/compat-table/es5/#Array.prototype.forEach)
23-
- `String.prototype.endsWith` [Browser support](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith#Browser_compatibility)
2423
- `localStorage` [Browser support](http://caniuse.com/#search=localstorage)
2524
- `Array.prototype.map` [Browser support](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Browser_compatibility)
2625
- `window.requestAnimationFrame` [Browser support](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame)

dist/static.compat.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.

dist/static.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.

karma.conf.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = function(config) {
1111

1212
// list of files / patterns to load in the browser
1313
files: [
14+
"dist/static.js",
1415
"test/**/*.spec.js"
1516
],
1617

@@ -21,12 +22,18 @@ module.exports = function(config) {
2122
// preprocess matching files before serving them to the browser
2223
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
2324
preprocessors: {
25+
"src/static.js": ["coverage"]
26+
},
27+
28+
coverageReporter: {
29+
type: "html",
30+
dir: "reports/"
2431
},
2532

2633
// test results reporter to use
2734
// possible values: "dots", "progress"
2835
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
29-
reporters: ["progress"],
36+
reporters: ["dots", "progress", "coverage"],
3037

3138
// web server port
3239
port: 9876,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"gulp-uglify": "^1.2.0",
2828
"karma": "^0.13.10",
2929
"karma-chrome-launcher": "^0.2.0",
30+
"karma-coverage": "^0.5.2",
3031
"karma-firefox-launcher": "^0.1.6",
3132
"karma-jasmine": "^0.3.6"
3233
}

src/polyfills.js

Lines changed: 0 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,171 +1,3 @@
1-
// Production steps of ECMA-262, Edition 5, 15.4.4.18
2-
// Reference: http://es5.github.io/#x15.4.4.18
3-
if (!Array.prototype.forEach) {
4-
5-
Array.prototype.forEach = function(callback, thisArg) {
6-
7-
var T, k;
8-
9-
if (this == null) {
10-
throw new TypeError(" this is null or not defined");
11-
}
12-
13-
// 1. Let O be the result of calling ToObject passing the |this| value as the argument.
14-
var O = Object(this);
15-
16-
// 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
17-
// 3. Let len be ToUint32(lenValue).
18-
var len = O.length >>> 0;
19-
20-
// 4. If IsCallable(callback) is false, throw a TypeError exception.
21-
// See: http://es5.github.com/#x9.11
22-
if (typeof callback !== "function") {
23-
throw new TypeError(callback + " is not a function");
24-
}
25-
26-
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
27-
if (arguments.length > 1) {
28-
T = thisArg;
29-
}
30-
31-
// 6. Let k be 0
32-
k = 0;
33-
34-
// 7. Repeat, while k < len
35-
while (k < len) {
36-
37-
var kValue;
38-
39-
// a. Let Pk be ToString(k).
40-
// This is implicit for LHS operands of the in operator
41-
// b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
42-
// This step can be combined with c
43-
// c. If kPresent is true, then
44-
if (k in O) {
45-
46-
// i. Let kValue be the result of calling the Get internal method of O with argument Pk.
47-
kValue = O[k];
48-
49-
// ii. Call the Call internal method of callback with T as the this value and
50-
// argument list containing kValue, k, and O.
51-
callback.call(T, kValue, k, O);
52-
}
53-
// d. Increase k by 1.
54-
k++;
55-
}
56-
// 8. return undefined
57-
};
58-
}
59-
60-
// Production steps of ECMA-262, Edition 5, 15.4.4.19
61-
// Reference: http://es5.github.io/#x15.4.4.19
62-
if (!Array.prototype.map) {
63-
64-
Array.prototype.map = function(callback, thisArg) {
65-
66-
var T, A, k;
67-
68-
if (this == null) {
69-
throw new TypeError(" this is null or not defined");
70-
}
71-
72-
// 1. Let O be the result of calling ToObject passing the |this|
73-
// value as the argument.
74-
var O = Object(this);
75-
76-
// 2. Let lenValue be the result of calling the Get internal
77-
// method of O with the argument "length".
78-
// 3. Let len be ToUint32(lenValue).
79-
var len = O.length >>> 0;
80-
81-
// 4. If IsCallable(callback) is false, throw a TypeError exception.
82-
// See: http://es5.github.com/#x9.11
83-
if (typeof callback !== "function") {
84-
throw new TypeError(callback + " is not a function");
85-
}
86-
87-
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
88-
if (arguments.length > 1) {
89-
T = thisArg;
90-
}
91-
92-
// 6. Let A be a new array created as if by the expression new Array(len)
93-
// where Array is the standard built-in constructor with that name and
94-
// len is the value of len.
95-
A = new Array(len);
96-
97-
// 7. Let k be 0
98-
k = 0;
99-
100-
// 8. Repeat, while k < len
101-
while (k < len) {
102-
103-
var kValue, mappedValue;
104-
105-
// a. Let Pk be ToString(k).
106-
// This is implicit for LHS operands of the in operator
107-
// b. Let kPresent be the result of calling the HasProperty internal
108-
// method of O with argument Pk.
109-
// This step can be combined with c
110-
// c. If kPresent is true, then
111-
if (k in O) {
112-
113-
// i. Let kValue be the result of calling the Get internal
114-
// method of O with argument Pk.
115-
kValue = O[k];
116-
117-
// ii. Let mappedValue be the result of calling the Call internal
118-
// method of callback with T as the this value and argument
119-
// list containing kValue, k, and O.
120-
mappedValue = callback.call(T, kValue, k, O);
121-
122-
// iii. Call the DefineOwnProperty internal method of A with arguments
123-
// Pk, Property Descriptor
124-
// { Value: mappedValue,
125-
// Writable: true,
126-
// Enumerable: true,
127-
// Configurable: true },
128-
// and false.
129-
130-
// In browsers that support Object.defineProperty, use the following:
131-
// Object.defineProperty(A, k, {
132-
// value: mappedValue,
133-
// writable: true,
134-
// enumerable: true,
135-
// configurable: true
136-
// });
137-
138-
// For best browser support, use the following:
139-
A[k] = mappedValue;
140-
}
141-
// d. Increase k by 1.
142-
k++;
143-
}
144-
145-
// 9. return A
146-
return A;
147-
};
148-
}
149-
150-
if (!Array.isArray) {
151-
Array.isArray = function(arg) {
152-
return Object.prototype.toString.call(arg) === "[object Array]";
153-
};
154-
}
155-
156-
157-
if (!String.prototype.endsWith) {
158-
String.prototype.endsWith = function(searchString, position) {
159-
var subjectString = this.toString();
160-
if (position === undefined || position > subjectString.length) {
161-
position = subjectString.length;
162-
}
163-
position -= searchString.length;
164-
var lastIndex = subjectString.indexOf(searchString, position);
165-
return lastIndex !== -1 && lastIndex === position;
166-
};
167-
}
168-
1691
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
1702
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
1713
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel

src/static.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1-
(function(window) {
1+
(function() {
22

33
var noop = function() { };
44
var storage = window.localStorage || {
55
getItem: noop,
66
setItem: noop,
7-
removeItem: noop
8-
};
9-
10-
var isJavaScript = function(url) {
11-
return url.toString().toLowerCase().endsWith(".js");
12-
};
13-
14-
var isCss = function(url) {
15-
return url.toString().toLowerCase().endsWith(".css");
16-
};
17-
18-
var isImage = function(url) {
19-
var src = url.toLowerCase();
20-
return src.endsWith(".png") || src.endsWith(".jpg") || src.endsWith(".jpeg") || src.endsWith(".webp");
7+
removeItem: noop,
8+
clear: noop
219
};
2210

2311
/**
@@ -31,6 +19,18 @@
3119
this.images = {};
3220
};
3321

22+
Static.prototype.isJS = function(url) {
23+
return !!url.match(/.*\.js+$/i);
24+
};
25+
26+
Static.prototype.isCSS = function(url) {
27+
return !!url.match(/.*\.css+$/i);
28+
};
29+
30+
Static.prototype.isImage = function(url) {
31+
return !!url.match(/.*\.(jpe?g|png|webp|gif)+$/i);
32+
};
33+
3434
/**
3535
*
3636
* @param {string} bundle Name of the bundle.
@@ -91,13 +91,13 @@
9191

9292
return this[bundle] = this.bundles[bundle] = Promise.all(resources.map(function(url) {
9393

94-
if (isJavaScript(url)) {
94+
if (self.isJS(url)) {
9595
return self.script(url);
9696
}
9797

98-
if (isCss(url)) {
98+
if (self.isCSS(url)) {
9999
return self.css(url);
100-
}
100+
}
101101

102102
return self.image(url);
103103

@@ -389,15 +389,15 @@
389389

390390
var promise = Promise.all(url.map(function(src) {
391391

392-
if (isJavaScript(src)) {
392+
if (self.isJS(src)) {
393393
return self.getScript(url);
394394
}
395395

396-
if (isCss(src)) {
396+
if (self.isCSS(src)) {
397397
return self.getStylesheet(url);
398398
}
399399

400-
if (isImage(url)) {
400+
if (self.isImage(url)) {
401401
return self.getImage(url);
402402
}
403403

@@ -423,6 +423,13 @@
423423

424424
};
425425

426+
Static.prototype._$$reset = function() {
427+
this.bundles = [];
428+
this.scripts = {};
429+
this.stylesheets = {};
430+
this.images = {};
431+
};
432+
426433
window.$static = new Static();
427434

428-
})(window);
435+
})();

test/caching.spec.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)