Skip to content

Commit dcc4ff6

Browse files
committed
feat: started unit tests
1 parent c4f1038 commit dcc4ff6

File tree

11 files changed

+246
-22
lines changed

11 files changed

+246
-22
lines changed

.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ Desktop.ini
4343
# Recycle Bin used on file shares
4444
$RECYCLE.BIN/
4545

46-
# Mac crap
46+
#############
47+
## Mac crap
48+
#############
4749
.DS_Store
4850

4951

@@ -55,7 +57,6 @@ nbproject
5557
manifest.mf
5658
build.xml
5759

58-
.project
5960
.settings
6061
.idea/*
6162
*.iml
@@ -68,5 +69,4 @@ public
6869
node_modules/*
6970
npm-debug.log
7071
bower_components
71-
72-
test/test-results.xml
72+
coverage

.npmignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
**/.*
2+
examples/
3+
src/
4+
tests/
5+
bower.json
6+
changelog.js
7+
gulpfile.js
8+
validate-commit-msg.js
9+
karma.conf.js

bower.json

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,34 @@
44
"description": "Load modules on demand (lazy load) with angularJS",
55
"main": "dist/ocLazyLoad.min.js",
66
"homepage": "https://github.com/ocombe/ocLazyLoad",
7-
"authors": ["Olivier Combe <[email protected]>"],
7+
"authors": [
8+
"Olivier Combe <[email protected]>"
9+
],
810
"license": "MIT",
9-
"keywords": ["lazy load", "lazy-load", "load on demand", "module", "angular", "angularJS"],
10-
"ignore": ["**/.*", "node_modules", "bower_components", "src", "gulpfile.js", "package.json"],
11+
"keywords": [
12+
"lazy load",
13+
"lazy-load",
14+
"load on demand",
15+
"module",
16+
"angular",
17+
"angularJS"
18+
],
19+
"ignore": [
20+
"**/.*",
21+
"node_modules",
22+
"bower_components",
23+
"src",
24+
"gulpfile.js",
25+
"package.json",
26+
"changelog.js",
27+
"validate-commit-msg.js",
28+
"tests",
29+
"karma.conf.js"
30+
],
31+
"devDependencies": {
32+
"angular-mocks": "~1.3.2"
33+
},
1134
"dependencies": {
1235
"angular": "1.2.x - 1.3.x"
1336
}
14-
}
37+
}
File renamed without changes.

gulpfile.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
var gulp = require('gulp');
22

3+
gulp.task('karma', function(callback) {
4+
var conf = require('./karma.conf.js').conf;
5+
conf.singleRun = true;
6+
return require('karma-as-promised').server.start(conf);
7+
});
8+
39
var build = function(newVer) {
410
var rename = require('gulp-rename'),
511
uglify = require('gulp-uglify'),
@@ -25,7 +31,7 @@ var build = function(newVer) {
2531
.pipe(gulp.dest('dist'));
2632
}
2733

28-
gulp.task('build', function() {
34+
gulp.task('build', ['karma'], function() {
2935
return build();
3036
});
3137

@@ -69,7 +75,7 @@ var makeChangelog = function(newVer) {
6975
concat = require('gulp-concat'),
7076
clean = require('gulp-clean');
7177

72-
stream.queue(gulp.src('').pipe(exec('node ./src/changelog.js ' + newVer, {pipeStdout: true})));
78+
stream.queue(gulp.src('').pipe(exec('node ./changelog.js ' + newVer, {pipeStdout: true})));
7379
stream.queue(gulp.src('CHANGELOG.md').pipe(clean()));
7480

7581
return stream.done()
@@ -84,7 +90,7 @@ gulp.task('changelog', function(event) {
8490
return promptBump(makeChangelog);
8591
})
8692

87-
gulp.task('release', function() {
93+
gulp.task('release', ['karma'], function() {
8894
var jeditor = require("gulp-json-editor");
8995

9096
return promptBump(function(newVer) {

karma.conf.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module.exports = function(config) {
2+
config.set(module.exports.conf);
3+
};
4+
5+
module.exports.conf = {
6+
7+
// base path that will be used to resolve all patterns (eg. files, exclude)
8+
basePath: '',
9+
10+
11+
// frameworks to use
12+
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
13+
frameworks: ['jasmine'],
14+
15+
16+
// list of files / patterns to load in the browser
17+
files: [
18+
'bower_components/angular/angular.js',
19+
'bower_components/angular-mocks/angular-mocks.js',
20+
'src/**/*.js',
21+
'tests/unit/**/*.spec.js',
22+
'tests/unit/**/*.mock.js'
23+
],
24+
25+
26+
// list of files to exclude
27+
exclude: [],
28+
29+
30+
// preprocess matching files before serving them to the browser
31+
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
32+
preprocessors: {
33+
'src/ocLazyLoad.js': ['coverage']
34+
},
35+
36+
37+
// test results reporter to use
38+
// possible values: 'dots', 'progress'
39+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
40+
reporters: ['progress', 'coverage'],
41+
42+
43+
// web server port
44+
port: 9876,
45+
46+
47+
// enable / disable colors in the output (reporters and logs)
48+
colors: true,
49+
50+
51+
// enable / disable watching file and executing tests whenever any file changes
52+
autoWatch: true,
53+
54+
55+
// start these browsers
56+
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
57+
browsers: ['Chrome'],
58+
59+
60+
// Continuous Integration mode
61+
// if true, Karma captures browsers, runs the tests and exits
62+
singleRun: false
63+
};

package.json

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,21 @@
1010
"example": "examples"
1111
},
1212
"scripts": {
13-
"test": "echo \"Error: no test specified\" && exit 1",
13+
"test": "karma start tests/unit/karma.conf.js",
1414
"build": "gulp build"
1515
},
1616
"repository": {
1717
"type": "git",
1818
"url": "git://github.com/ocombe/ocLazyLoad.git"
1919
},
20-
"keywords": ["lazy load", "lazy-load", "load on demand", "module", "angular", "angularJS"],
20+
"keywords": [
21+
"lazy load",
22+
"lazy-load",
23+
"load on demand",
24+
"module",
25+
"angular",
26+
"angularJS"
27+
],
2128
"bugs": {
2229
"url": "https://github.com/ocombe/ocLazyLoad/issues"
2330
},
@@ -31,8 +38,15 @@
3138
"gulp-prompt": "^0.1.1",
3239
"gulp-rename": "^1.2.0",
3340
"gulp-uglify": "^0.3.1",
41+
"karma": "^0.12.24",
42+
"karma-as-promised": "^1.0.0",
43+
"karma-chrome-launcher": "^0.1.5",
44+
"karma-coverage": "^0.2.6",
45+
"karma-jasmine": "^0.2.3",
46+
"karma-requirejs": "^0.2.2",
3447
"qq": "^0.3.5",
48+
"requirejs": "^2.1.15",
3549
"semver": "^2.3.1",
3650
"streamqueue": "^0.1.1"
3751
}
38-
}
52+
}

src/ocLazyLoad.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -605,24 +605,24 @@
605605

606606
this.config = function(config) {
607607
if(angular.isDefined(config.jsLoader) || angular.isDefined(config.asyncLoader)) {
608-
jsLoader = config.jsLoader || config.asyncLoader;
609-
if(!angular.isFunction(jsLoader)) {
608+
if(!angular.isFunction(config.jsLoader || config.asyncLoader)) {
610609
throw('The js loader needs to be a function');
611610
}
611+
jsLoader = config.jsLoader || config.asyncLoader;
612612
}
613613

614614
if(angular.isDefined(config.cssLoader)) {
615-
cssLoader = config.cssLoader;
616-
if(!angular.isFunction(cssLoader)) {
615+
if(!angular.isFunction(config.cssLoader)) {
617616
throw('The css loader needs to be a function');
618617
}
618+
cssLoader = config.cssLoader;
619619
}
620620

621621
if(angular.isDefined(config.templatesLoader)) {
622-
templatesLoader = config.templatesLoader;
623-
if(!angular.isFunction(templatesLoader)) {
622+
if(!angular.isFunction(config.templatesLoader)) {
624623
throw('The template loader needs to be a function');
625624
}
625+
templatesLoader = config.templatesLoader;
626626
}
627627

628628
// for bootstrap apps, we need to define the main module name

tests/unit/mocks/app.mock.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
document.querySelector('html').setAttribute('ng-app','app1');
2+
3+
angular.module('app1', ['oc.lazyLoad'])
4+
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
5+
$ocLazyLoadProvider.config({
6+
modules: [{
7+
name: 'test',
8+
files: []
9+
}]
10+
})
11+
}]);

tests/unit/specs/ocLazyLoad.spec.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*globals describe, beforeEach, afterEach, inject, module, it, expect, angular */
2+
describe('Module: oc.lazyLoad', function() {
3+
'use strict';
4+
5+
var $ocLazyLoad;
6+
7+
describe('with app1', function() {
8+
9+
beforeEach(function() {
10+
module('app1');
11+
12+
inject(function(_$ocLazyLoad_) {
13+
$ocLazyLoad = _$ocLazyLoad_;
14+
});
15+
});
16+
17+
it('service should be defined', function() {
18+
expect($ocLazyLoad).toBeDefined();
19+
});
20+
21+
it('getModules should be working', function() {
22+
expect($ocLazyLoad.getModules).toBeDefined();
23+
expect(angular.isArray($ocLazyLoad.getModules())).toBe(true);
24+
});
25+
26+
it('loadedModules should be working', function() {
27+
expect($ocLazyLoad.getModules()).toEqual(['ng', 'app1', 'oc.lazyLoad']);
28+
});
29+
30+
it('isLoaded should be working', function() {
31+
expect($ocLazyLoad.isLoaded).toBeDefined();
32+
expect($ocLazyLoad.isLoaded).toThrowError('You need to define the module(s) name(s)');
33+
expect($ocLazyLoad.isLoaded('app1')).toBe(true); // string arg
34+
expect($ocLazyLoad.isLoaded(['ng', 'app1'])).toBe(true); // array arg
35+
expect($ocLazyLoad.isLoaded('noModule')).toBe(false);
36+
expect($ocLazyLoad.isLoaded(['ng', 'noModule'])).toBe(false);
37+
});
38+
39+
it('getModuleConfig should be working', function() {
40+
expect($ocLazyLoad.getModuleConfig).toBeDefined();
41+
expect($ocLazyLoad.getModuleConfig).toThrowError('You need to give the name of the module to get');
42+
expect($ocLazyLoad.getModuleConfig('noModule')).toBe(null);
43+
expect($ocLazyLoad.getModuleConfig('test')).toEqual({name: 'test', files: []});
44+
});
45+
46+
it('setModuleConfig should be working', function() {
47+
expect($ocLazyLoad.setModuleConfig).toBeDefined();
48+
expect($ocLazyLoad.setModuleConfig).toThrowError('You need to give the module config object to set');
49+
expect($ocLazyLoad.setModuleConfig({name: 'test2'})).toEqual({name: 'test2'});
50+
expect($ocLazyLoad.getModuleConfig('test2')).toEqual({name: 'test2'}); // check if set worked
51+
});
52+
53+
});
54+
55+
describe('failed configs', function() {
56+
57+
it('should throw an error if js loader is not a function', function() {
58+
expect(function() {
59+
angular.module('app2', ['oc.lazyLoad'])
60+
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
61+
$ocLazyLoadProvider.config({
62+
jsLoader: ''
63+
});
64+
}]);
65+
module('app2');
66+
inject(function(_$ocLazyLoad_) {});
67+
}).toThrowError(/The js loader needs to be a function/);
68+
});
69+
70+
it('should throw an error if css loader is not a function', function() {
71+
expect(function() {
72+
angular.module('app2', ['oc.lazyLoad'])
73+
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
74+
$ocLazyLoadProvider.config({
75+
cssLoader: ''
76+
});
77+
}]);
78+
module('app2');
79+
inject(function(_$ocLazyLoad_) {});
80+
}).toThrowError(/The css loader needs to be a function/);
81+
});
82+
83+
it('should throw an error if css loader is not a function', function() {
84+
expect(function() {
85+
angular.module('app2', ['oc.lazyLoad'])
86+
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
87+
$ocLazyLoadProvider.config({
88+
templatesLoader: ''
89+
});
90+
}]);
91+
module('app2');
92+
inject(function(_$ocLazyLoad_) {});
93+
}).toThrowError(/The template loader needs to be a function/);
94+
});
95+
96+
});
97+
98+
});

0 commit comments

Comments
 (0)