Skip to content

Commit 26ff502

Browse files
committed
Added QUnit testing. Updated Async for better response.
1 parent 240fbb6 commit 26ff502

File tree

8 files changed

+151
-54
lines changed

8 files changed

+151
-54
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.DS_Store
12
node_modules
23
bower_components
34
*.sublime*

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
before_script:
2+
- sudo npm install -g grunt
3+
4+
script: grunt test --verbose --force

dist/jquery.getscripts.min.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* Released Under the Following Licenses
1010
* https://github.com/hudsonfoo/jquery-getscripts/blob/1.0.0/MIT-LICENSE.txt
1111
*
12-
* Date: 2014-03-22T20:08:45
13-
*
12+
* Date: 2014-03-23T21:01:51
1413
*/
15-
!function(a){"use strict";a.getScripts||(a.getScripts=function(b){var c,d,e;if(c=a.extend({async:!1,cache:!0},b),"string"==typeof c.urls&&(c.urls=[c.urls]),e=[],d=function(){a.ajax({url:c.urls.shift(),dataType:"script",cache:c.cache,success:function(){e.push(arguments),c.urls.length>0?d():"function"==typeof b.success&&b.success(a.merge([],e))}})},c.async===!0){for(var f=0;f<c.urls.length;f++)e.push(a.ajax({url:c.urls[f],cache:c.cache,dataType:"script"}));b.success(a.merge([],e))}else d()})}(jQuery);
14+
!function(a){"use strict";a.getScripts||(a.getScripts=function(b){var c,d,e,f;if(c=a.extend({async:!1,cache:!0},b),"string"==typeof c.urls&&(c.urls=[c.urls]),f=[],d=function(){a.ajax({url:c.urls.shift(),dataType:"script",cache:c.cache,success:function(){f.push(arguments),c.urls.length>0?d():"function"==typeof b.success&&b.success(a.merge([],f))}})},e=function(){f.push(arguments),f.length===c.urls.length&&"function"==typeof b.success&&b.success(a.merge([],f))},c.async===!0)for(var g=0;g<c.urls.length;g++)a.ajax({url:c.urls[g],dataType:"script",cache:c.cache,success:e});else d()})}(jQuery);

gruntfile.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ module.exports = function(grunt) {
1111
all: ["source/jquery.getscripts.js"]
1212
},
1313

14+
qunit: {
15+
options: {
16+
timeout: 10000
17+
},
18+
all: ['tests/*.html']
19+
},
20+
1421
uglify: {
1522
options: {
1623
compress: true,
@@ -39,7 +46,9 @@ module.exports = function(grunt) {
3946

4047
grunt.loadNpmTasks('grunt-contrib-jshint');
4148
grunt.loadNpmTasks('grunt-contrib-uglify');
49+
grunt.loadNpmTasks('grunt-contrib-qunit');
4250

43-
grunt.registerTask('dist', ['jshint', 'uglify']);
51+
grunt.registerTask('test', ['jshint', 'qunit']);
52+
grunt.registerTask('dist', ['test', 'uglify']);
4453
grunt.registerTask('default', ['dist']);
4554
};

package.json

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
1-
{
2-
"name": "jquery.getscripts",
3-
"title": "jQuery.getScripts",
4-
"description": "Load one or multiple JavaScript files from the server using a GET HTTP request, then execute them..",
5-
"version": "1.0.0",
6-
"keywords": [
7-
"jquery",
8-
"javascript",
9-
"plugin",
10-
"getscripts",
11-
"getscript"
12-
],
13-
"author": {
14-
"name": "David Hudson",
15-
"email": "im@davidhudson.me",
16-
"url": "http://davidhudson.me/"
17-
},
18-
"maintainers": [
19-
{
20-
"name": "David Hudson",
21-
"email": "im@davidhudson.me",
22-
"url": "http://davidhudson.me/"
23-
}
24-
],
25-
"licenses": [
26-
{
27-
"type": "MIT",
28-
"url": "https://github.com/hudsonfoo/jquery-getscripts/blob/1.0.0/MIT-LICENSE.txt"
29-
}
30-
],
31-
"bugs": "https://github.com/hudsonfoo/jquery-getscripts/issues",
32-
"homepage": "https://github.com/hudsonfoo/jquery-getscripts",
33-
"docs": "https://github.com/hudsonfoo/jquery-getscripts",
34-
"download": "https://github.com/hudsonfoo/jquery-getscripts",
35-
"repository": {
36-
"type": "git",
37-
"url": "https://github.com/hudsonfoo/jquery-getscripts"
38-
},
39-
"devDependencies": {
40-
"grunt": "~0.4.1",
41-
"grunt-contrib-jshint": "~0.6.0",
42-
"grunt-contrib-uglify": "~0.2.2"
43-
}
44-
}
1+
{
2+
"name": "jquery.getscripts",
3+
"title": "jQuery.getScripts",
4+
"description": "Load one or multiple JavaScript files from the server using a GET HTTP request, then execute them..",
5+
"version": "1.0.0",
6+
"keywords": [
7+
"jquery",
8+
"javascript",
9+
"plugin",
10+
"getscripts",
11+
"getscript"
12+
],
13+
"author": {
14+
"name": "David Hudson",
15+
"email": "im@davidhudson.me",
16+
"url": "http://davidhudson.me/"
17+
},
18+
"maintainers": [
19+
{
20+
"name": "David Hudson",
21+
"email": "im@davidhudson.me",
22+
"url": "http://davidhudson.me/"
23+
}
24+
],
25+
"licenses": [
26+
{
27+
"type": "MIT",
28+
"url": "https://github.com/hudsonfoo/jquery-getscripts/blob/1.0.0/MIT-LICENSE.txt"
29+
}
30+
],
31+
"bugs": "https://github.com/hudsonfoo/jquery-getscripts/issues",
32+
"homepage": "https://github.com/hudsonfoo/jquery-getscripts",
33+
"docs": "https://github.com/hudsonfoo/jquery-getscripts",
34+
"download": "https://github.com/hudsonfoo/jquery-getscripts",
35+
"repository": {
36+
"type": "git",
37+
"url": "https://github.com/hudsonfoo/jquery-getscripts"
38+
},
39+
"devDependencies": {
40+
"grunt": "~0.4.1",
41+
"grunt-contrib-jshint": "~0.6.0",
42+
"grunt-contrib-uglify": "~0.2.2",
43+
"grunt-contrib-qunit": "~0.4.0"
44+
}
45+
}

source/jquery.getscripts.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
if ($.getScripts) { return; }
55

66
$.getScripts = function (options) {
7-
var _options, _sync, _response;
7+
var _options, _sync, _async, _response;
88

99
_options = $.extend({
1010
'async': false,
@@ -16,6 +16,7 @@
1616
}
1717

1818
_response = [];
19+
1920
_sync = function () {
2021
$.ajax({
2122
url: _options.urls.shift(),
@@ -32,16 +33,23 @@
3233
});
3334
};
3435

36+
_async = function () {
37+
_response.push(arguments);
38+
if (_response.length === _options.urls.length &&
39+
typeof options.success === 'function') {
40+
options.success($.merge([], _response));
41+
}
42+
};
43+
3544
if (_options.async === true) {
3645
for (var i = 0; i < _options.urls.length; i++) {
37-
_response.push($.ajax({
46+
$.ajax({
3847
url: _options.urls[i],
48+
dataType: 'script',
3949
cache: _options.cache,
40-
dataType: 'script'
41-
}));
50+
success: _async
51+
});
4252
}
43-
44-
options.success($.merge([], _response));
4553
} else {
4654
_sync();
4755
}

tests/index.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset='UTF-8' />
5+
<meta http-equiv='content-type' content='text/html; charset=utf-8' />
6+
7+
<title>jQuery.simpleFAQ QUnit Test Runner</title>
8+
9+
<link rel='stylesheet' href='http://code.jquery.com/qunit/qunit-1.14.0.css'>
10+
11+
<script src='http://code.jquery.com/jquery-1.5.js'></script>
12+
<script src='../source/jquery.getscripts.js'></script>
13+
<script src='http://code.jquery.com/qunit/qunit-1.14.0.js'></script>
14+
<script src='tests.js'></script>
15+
</head>
16+
<body>
17+
<div id="qunit"></div>
18+
<div id='qunit-fixture'></div>
19+
</body>
20+
</html>

tests/tests.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module("Async");
2+
asyncTest("Remote With Cache", function() {
3+
angular = dojo = Prototype = null;
4+
try {
5+
(function($) {
6+
$.getScripts({
7+
urls: [
8+
'http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js',
9+
'http://ajax.googleapis.com/ajax/libs/dojo/1.9.3/dojo/dojo.js',
10+
'http://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js'
11+
],
12+
cache: true,
13+
async: false,
14+
success: function(response) {
15+
start();
16+
notEqual(typeof response, 'undefined', 'Response not undefined');
17+
notEqual(typeof angular, 'undefined', "Angular not undefined");
18+
notEqual(typeof dojo, 'undefined', "Dojo not undefined");
19+
notEqual(typeof Prototype, 'undefined', "Prototype not undefined");
20+
}
21+
});
22+
})(jQuery);
23+
} catch(e) {
24+
start();
25+
ok(false, 'General script failure');
26+
}
27+
});
28+
29+
module("Sync");
30+
asyncTest("Remote With Cache", function() {
31+
angular = dojo = Prototype = null;
32+
try {
33+
(function($) {
34+
$.getScripts({
35+
urls: [
36+
'http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js',
37+
'http://ajax.googleapis.com/ajax/libs/dojo/1.9.3/dojo/dojo.js',
38+
'http://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js'
39+
],
40+
cache: true,
41+
async: false,
42+
success: function(response) {
43+
start();
44+
notEqual(typeof response, 'undefined', 'Response not undefined');
45+
notEqual(typeof angular, 'undefined', "Angular not undefined");
46+
notEqual(typeof dojo, 'undefined', "Dojo not undefined");
47+
notEqual(typeof Prototype, 'undefined', "Prototype not undefined");
48+
}
49+
});
50+
})(jQuery);
51+
} catch(e) {
52+
start();
53+
ok(false, 'General script failure');
54+
}
55+
});

0 commit comments

Comments
 (0)