Skip to content

Commit

Permalink
feat: started unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe committed Nov 11, 2014
1 parent c4f1038 commit dcc4ff6
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 22 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/

# Mac crap
#############
## Mac crap
#############
.DS_Store


Expand All @@ -55,7 +57,6 @@ nbproject
manifest.mf
build.xml

.project
.settings
.idea/*
*.iml
Expand All @@ -68,5 +69,4 @@ public
node_modules/*
npm-debug.log
bower_components

test/test-results.xml
coverage
9 changes: 9 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
**/.*
examples/
src/
tests/
bower.json
changelog.js
gulpfile.js
validate-commit-msg.js
karma.conf.js
31 changes: 27 additions & 4 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,34 @@
"description": "Load modules on demand (lazy load) with angularJS",
"main": "dist/ocLazyLoad.min.js",
"homepage": "https://github.com/ocombe/ocLazyLoad",
"authors": ["Olivier Combe <olivier.combe@gmail.com>"],
"authors": [
"Olivier Combe <olivier.combe@gmail.com>"
],
"license": "MIT",
"keywords": ["lazy load", "lazy-load", "load on demand", "module", "angular", "angularJS"],
"ignore": ["**/.*", "node_modules", "bower_components", "src", "gulpfile.js", "package.json"],
"keywords": [
"lazy load",
"lazy-load",
"load on demand",
"module",
"angular",
"angularJS"
],
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"src",
"gulpfile.js",
"package.json",
"changelog.js",
"validate-commit-msg.js",
"tests",
"karma.conf.js"
],
"devDependencies": {
"angular-mocks": "~1.3.2"
},
"dependencies": {
"angular": "1.2.x - 1.3.x"
}
}
}
File renamed without changes.
12 changes: 9 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
var gulp = require('gulp');

gulp.task('karma', function(callback) {
var conf = require('./karma.conf.js').conf;
conf.singleRun = true;
return require('karma-as-promised').server.start(conf);
});

var build = function(newVer) {
var rename = require('gulp-rename'),
uglify = require('gulp-uglify'),
Expand All @@ -25,7 +31,7 @@ var build = function(newVer) {
.pipe(gulp.dest('dist'));
}

gulp.task('build', function() {
gulp.task('build', ['karma'], function() {
return build();
});

Expand Down Expand Up @@ -69,7 +75,7 @@ var makeChangelog = function(newVer) {
concat = require('gulp-concat'),
clean = require('gulp-clean');

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

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

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

return promptBump(function(newVer) {
Expand Down
63 changes: 63 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module.exports = function(config) {
config.set(module.exports.conf);
};

module.exports.conf = {

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],


// list of files / patterns to load in the browser
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'src/**/*.js',
'tests/unit/**/*.spec.js',
'tests/unit/**/*.mock.js'
],


// list of files to exclude
exclude: [],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'src/ocLazyLoad.js': ['coverage']
},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress', 'coverage'],


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
};
20 changes: 17 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@
"example": "examples"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "karma start tests/unit/karma.conf.js",
"build": "gulp build"
},
"repository": {
"type": "git",
"url": "git://github.com/ocombe/ocLazyLoad.git"
},
"keywords": ["lazy load", "lazy-load", "load on demand", "module", "angular", "angularJS"],
"keywords": [
"lazy load",
"lazy-load",
"load on demand",
"module",
"angular",
"angularJS"
],
"bugs": {
"url": "https://github.com/ocombe/ocLazyLoad/issues"
},
Expand All @@ -31,8 +38,15 @@
"gulp-prompt": "^0.1.1",
"gulp-rename": "^1.2.0",
"gulp-uglify": "^0.3.1",
"karma": "^0.12.24",
"karma-as-promised": "^1.0.0",
"karma-chrome-launcher": "^0.1.5",
"karma-coverage": "^0.2.6",
"karma-jasmine": "^0.2.3",
"karma-requirejs": "^0.2.2",
"qq": "^0.3.5",
"requirejs": "^2.1.15",
"semver": "^2.3.1",
"streamqueue": "^0.1.1"
}
}
}
12 changes: 6 additions & 6 deletions src/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,24 +605,24 @@

this.config = function(config) {
if(angular.isDefined(config.jsLoader) || angular.isDefined(config.asyncLoader)) {
jsLoader = config.jsLoader || config.asyncLoader;
if(!angular.isFunction(jsLoader)) {
if(!angular.isFunction(config.jsLoader || config.asyncLoader)) {
throw('The js loader needs to be a function');
}
jsLoader = config.jsLoader || config.asyncLoader;
}

if(angular.isDefined(config.cssLoader)) {
cssLoader = config.cssLoader;
if(!angular.isFunction(cssLoader)) {
if(!angular.isFunction(config.cssLoader)) {
throw('The css loader needs to be a function');
}
cssLoader = config.cssLoader;
}

if(angular.isDefined(config.templatesLoader)) {
templatesLoader = config.templatesLoader;
if(!angular.isFunction(templatesLoader)) {
if(!angular.isFunction(config.templatesLoader)) {
throw('The template loader needs to be a function');
}
templatesLoader = config.templatesLoader;
}

// for bootstrap apps, we need to define the main module name
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/mocks/app.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
document.querySelector('html').setAttribute('ng-app','app1');

angular.module('app1', ['oc.lazyLoad'])
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
modules: [{
name: 'test',
files: []
}]
})
}]);
98 changes: 98 additions & 0 deletions tests/unit/specs/ocLazyLoad.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*globals describe, beforeEach, afterEach, inject, module, it, expect, angular */
describe('Module: oc.lazyLoad', function() {
'use strict';

var $ocLazyLoad;

describe('with app1', function() {

beforeEach(function() {
module('app1');

inject(function(_$ocLazyLoad_) {
$ocLazyLoad = _$ocLazyLoad_;
});
});

it('service should be defined', function() {
expect($ocLazyLoad).toBeDefined();
});

it('getModules should be working', function() {
expect($ocLazyLoad.getModules).toBeDefined();
expect(angular.isArray($ocLazyLoad.getModules())).toBe(true);
});

it('loadedModules should be working', function() {
expect($ocLazyLoad.getModules()).toEqual(['ng', 'app1', 'oc.lazyLoad']);
});

it('isLoaded should be working', function() {
expect($ocLazyLoad.isLoaded).toBeDefined();
expect($ocLazyLoad.isLoaded).toThrowError('You need to define the module(s) name(s)');
expect($ocLazyLoad.isLoaded('app1')).toBe(true); // string arg
expect($ocLazyLoad.isLoaded(['ng', 'app1'])).toBe(true); // array arg
expect($ocLazyLoad.isLoaded('noModule')).toBe(false);
expect($ocLazyLoad.isLoaded(['ng', 'noModule'])).toBe(false);
});

it('getModuleConfig should be working', function() {
expect($ocLazyLoad.getModuleConfig).toBeDefined();
expect($ocLazyLoad.getModuleConfig).toThrowError('You need to give the name of the module to get');
expect($ocLazyLoad.getModuleConfig('noModule')).toBe(null);
expect($ocLazyLoad.getModuleConfig('test')).toEqual({name: 'test', files: []});
});

it('setModuleConfig should be working', function() {
expect($ocLazyLoad.setModuleConfig).toBeDefined();
expect($ocLazyLoad.setModuleConfig).toThrowError('You need to give the module config object to set');
expect($ocLazyLoad.setModuleConfig({name: 'test2'})).toEqual({name: 'test2'});
expect($ocLazyLoad.getModuleConfig('test2')).toEqual({name: 'test2'}); // check if set worked
});

});

describe('failed configs', function() {

it('should throw an error if js loader is not a function', function() {
expect(function() {
angular.module('app2', ['oc.lazyLoad'])
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
jsLoader: ''
});
}]);
module('app2');
inject(function(_$ocLazyLoad_) {});
}).toThrowError(/The js loader needs to be a function/);
});

it('should throw an error if css loader is not a function', function() {
expect(function() {
angular.module('app2', ['oc.lazyLoad'])
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
cssLoader: ''
});
}]);
module('app2');
inject(function(_$ocLazyLoad_) {});
}).toThrowError(/The css loader needs to be a function/);
});

it('should throw an error if css loader is not a function', function() {
expect(function() {
angular.module('app2', ['oc.lazyLoad'])
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
templatesLoader: ''
});
}]);
module('app2');
inject(function(_$ocLazyLoad_) {});
}).toThrowError(/The template loader needs to be a function/);
});

});

});
4 changes: 2 additions & 2 deletions src/validate-commit-msg.js → validate-commit-msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Installation:
* >> cd <ocLazyLoad-repo>
* >> ln -s ../../src/validate-commit-msg.js .git/hooks/commit-msg
* >> ln -s ../../validate-commit-msg.js .git/hooks/commit-msg
*/
var fs = require('fs');
var util = require('util');
Expand Down Expand Up @@ -103,4 +103,4 @@ if (process.argv.join('').indexOf('jasmine-node') === -1) {
process.exit(0);
}
});
}
}

0 comments on commit dcc4ff6

Please sign in to comment.