Skip to content

Commit

Permalink
Merge pull request #20 from reflexdemon/unit-testing
Browse files Browse the repository at this point in the history
Unit testing
  • Loading branch information
reflexdemon committed Aug 1, 2015
2 parents fb828bd + 41161e7 commit c6281f8
Show file tree
Hide file tree
Showing 19 changed files with 974 additions and 60 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
"name": "slush-angular-gulp",
"version": "0.3.4",
"version": "0.3.6",
"description": "Gulp, Angular, Less with Web server, this generator is build with inspiration from the below projects. slush-angular, angular-styleguide and generator-angular",
"main": "slushfile.js",
"repository": {
"type": "git",
"url": "https://github.com/reflexdemon/slush-angular-gulp"
},
"scripts": {
"test": "NODE_ENV=test mocha -R spec test/*.spec.js",
"posttest": "NODE_ENV=test mocha -R travis-cov test/*.spec.js",
"test": "NODE_ENV=test mocha -R spec test/top.js",
"posttest": "NODE_ENV=test mocha -R travis-cov test/top.js",
"old": "NODE_ENV=test mocha -R spec test/*.spec.js",
"old-travis": "NODE_ENV=test mocha -R travis-cov test/*.spec.js",
"coveralls": "NODE_ENV=test ./node_modules/.bin/mocha test/*.spec.js -R mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js"
},
"config": {
Expand Down
133 changes: 76 additions & 57 deletions tasks/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,87 @@
*/

(function() {
var gulp = require('gulp'),
install = require('gulp-install'),
conflict = require('gulp-conflict'),
template = require('gulp-template'),
rename = require('gulp-rename'),
inquirer = require('inquirer')
var gulp = require('gulp'),
install = require('gulp-install'),
conflict = require('gulp-conflict'),
template = require('gulp-template'),
rename = require('gulp-rename'),
inquirer = require('inquirer')
_ = require('underscore.string');

//Local dependencies
var util = require('../util');
//Local dependencies
var util = require('../util');

module.exports = function(gulp) {
'use strict';
module.exports = function(gulp) {
'use strict';

gulp.task('controller', function(done) {
var _this = this;
var name = util.getDefaultOption(_this.args, 0);
var options = util.getGlobalOptions();
var modules = util.getModuleProposal(options.appDir);
gulp.task('controller', function(done) {
var _this = this;
var name = util.getDefaultOption(_this.args, 0);
var options = util.getGlobalOptions();
var modules = util.getModuleProposal(options.appDir);

if (modules.length === 0) {
throw new Error('Controller must be created in a module, but no modules exist. Create a module using "slush angular-gulp:module <module-Name>".');
}

inquirer.prompt([{
type: 'input',
name: 'fileName',
message: 'What is the name of your controller?',
default: name
}, {
type: 'list',
name: 'module',
message: 'What is your AngularJS module name?',
choices: modules
}, {
type: 'confirm',
name: 'test',
message: 'Do you want to include unit testing?',
default: true
}], function(answers) {
//Init
answers.nameDashed = _.slugify(util.getNameProposal());
answers.scriptAppName = _.camelize(answers.nameDashed) + '.' +answers.module ;
answers.classedName = _.capitalize(_.camelize(answers.fileName));
// test
if (answers.test) {
gulp.src(__dirname + '/../templates/controller/controller.spec.js')
.pipe(template(answers))
.pipe(rename(answers.fileName + '-controller.spec.js'))
.pipe(conflict(options.base + options.appDir + '/components/' + answers.module))
.pipe(gulp.dest(options.base + options.appDir + '/components/' + answers.module))
if (modules.length === 0) {
throw new Error('Controller must be created in a module, but no modules exist. Create a module using "slush angular-gulp:module <module-Name>".');
}
//Source
gulp.src(__dirname + '/../templates/controller/controller.js')
.pipe(template(answers))
.pipe(rename(answers.fileName + '-controller.js'))
.pipe(conflict(options.base + options.appDir + '/components/' + answers.module))
.pipe(gulp.dest(options.base + options.appDir + '/components/' + answers.module))
.on('finish', function() {
done();
});

inquirer.prompt([{
type: 'input',
name: 'fileName',
message: 'What is the name of your controller?'
}, {
type: 'list',
name: 'module',
message: 'What is your AngularJS module name?',
choices: modules
}, {
type: 'confirm',
name: 'spec',
message: 'Do you want to include unit testing?',
default: true
}], function(answers) {
//Init
answers.nameDashed = _.slugify(util.getNameProposal());
answers.scriptAppName = _.camelize(answers.nameDashed) + '.' + answers.module;
answers.classedName = _.capitalize(_.camelize(answers.fileName));
// console.log('answers:', answers);
// test
if (answers.spec === true) {
gulp.src(__dirname + '/../templates/controller/controller.spec.js')
.pipe(template(answers))
.pipe(rename(answers.fileName + '-controller.spec.js'))
.pipe(conflict(options.base + options.appDir + '/components/' + answers.module))
.pipe(gulp.dest(options.base + options.appDir + '/components/' + answers.module));
gulp.src(__dirname + '/../templates/controller/controller.js')
.pipe(template(answers))
.pipe(rename(answers.fileName + '-controller.js'))
.pipe(conflict(options.base + options.appDir + '/components/' + answers.module))
.pipe(gulp.dest(options.base + options.appDir + '/components/' + answers.module))
.on('finish', function() {
done();
});

} else {
gulp.src(__dirname + '/../templates/controller/controller.js')
.pipe(template(answers))
.pipe(rename(answers.fileName + '-controller.js'))
.pipe(conflict(options.base + options.appDir + '/components/' + answers.module))
.pipe(gulp.dest(options.base + options.appDir + '/components/' + answers.module))
.on('finish', function() {
done();
});

}
// //Source
// gulp.src(__dirname + '/../templates/controller/controller.js')
// .pipe(template(answers))
// .pipe(rename(answers.fileName + '-controller.js'))
// .pipe(conflict(options.base + options.appDir + '/components/' + answers.module))
// .pipe(gulp.dest(options.base + options.appDir + '/components/' + answers.module))
// .on('finish', function() {
// done();
// });
});
});
});
}
}
})();
202 changes: 202 additions & 0 deletions test/cases/app.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
(function() {
'use strict';

var common = require("../common");
var options = common.options;
var assert = common.assert;
var testingUtil = common.testingUtil;
var util = common.util;
var mockGulpDest = common.mockGulpDest;
var gulp = common.gulp;


function beforeEach() {
process.chdir(__dirname);
testingUtil.mockPrompt({
name: 'module'
});
util.setRuntimeMode('TEST');
}


it('should put all project files in current working directory', function(done) {
beforeEach();
gulp.start('default').once('stop', function() {
// mockGulpDest.cwd().should.equal(__dirname);
// mockGulpDest.basePath().should.equal(__dirname);

assert.that(mockGulpDest.cwd()).is.equalTo(__dirname);
assert.that(mockGulpDest.basePath()).is.equalTo(__dirname);
done();
});
});
it('should add dot files to project root', function(done) {
beforeEach();
gulp.start('default').once('stop', function() {
mockGulpDest.assertDestContains([
'.bowerrc',
'.csslintrc',
'.editorconfig',
'.gitignore',
'.jshintrc'
]);
done();
});
});
it('should add bower.json and package.json to project root', function(done) {
beforeEach();
gulp.start('default').once('stop', function() {
mockGulpDest.assertDestContains([
'package.json',
'bower.json'
]);
done();
});
});
it('should add a gulpfile to project root', function(done) {
beforeEach();
gulp.start('default').once('stop', function() {
mockGulpDest.assertDestContains('gulpfile.js');
done();
});
});
it('should add a karma config file to project root', function(done) {
beforeEach();
gulp.start('default').once('stop', function() {
mockGulpDest.assertDestContains('karma.conf.js');
done();
});
});
it('should add a readme file to project root', function(done) {
beforeEach();
gulp.start('default').once('stop', function() {
mockGulpDest.assertDestContains('README.md');
done();
});
});
it('should add an index.html to the app folder', function(done) {
beforeEach();
gulp.start('default').once('stop', function() {
mockGulpDest.assertDestContains('src/app/index.html');
done();
});
});
it('should add a JavaScript app module definition file by default', function(done) {
testingUtil.mockPrompt({
name: 'module'
});
beforeEach();
gulp.start('default').once('stop', function() {
mockGulpDest.assertDestContains('src/app/app.js');
done();
});
});
it('should create a gitkeep file in the app assets dir', function(done) {
beforeEach();
gulp.start('default').once('stop', function() {
mockGulpDest.assertDestContains('src/app/assets/.gitkeep');
done();
});
});

//Deep example
describe('Todo example', function() {
it('should not add any todo example files by default', function(done) {
testingUtil.mockPrompt({
name: 'module'
});
gulp.start('default').once('stop', function() {
mockGulpDest.assertDestNotContains({
'src/app/components/todo': [
'todo.js',
'todo.html',
'todo.css',
'todo-route.js',
'todo-controller.spec.js',
'todo-controller.js'
]
});
done();
});
});
describe('When Todo example is included', function() {
function beforeEachTODO() {
testingUtil.mockPrompt({
name: 'module',
example: ['todo']
});
}
it('should add a module specific template', function(done) {
beforeEachTODO();
gulp.start('default').once('stop', function() {
mockGulpDest.assertDestContains('src/app/components/todo/todo.html');
done();
});
});
it('should add a module definition file for the Todo module', function(done) {
beforeEachTODO();
gulp.start('default').once('stop', function() {
mockGulpDest.assertDestContains('src/app/components/todo/todo.js');
done();
});
});
it('should add a Todo controller with a corresponding test file', function(done) {
beforeEachTODO();
gulp.start('default').once('stop', function() {
mockGulpDest.assertDestContains([
'src/app/components/todo/todo-controller.js',
'src/app/components/todo/todo-controller.spec.js'
]);
done();
});
});
});
});
describe('CSS files', function() {
it('should add less stylesheets by default', function(done) {
testingUtil.mockPrompt({
name: 'module',
example: ['todo']
});
gulp.start('default').once('stop', function() {
mockGulpDest.assertDestContains([
'src/app/app.less',
'src/app/styles/_base.less',
'src/app/components/todo/todo.less'
]);
done();
});
});
it('should add LESS stylesheets when LESS is chosen', function(done) {
testingUtil.mockPrompt({
name: 'module',
csstype: 'less',
example: ['todo']
});
gulp.start('default').once('stop', function() {
mockGulpDest.assertDestContains([
'src/app/app.less',
'src/app/styles/_base.less',
'src/app/components/todo/todo.less'
]);
done();
});
});
it('should add Sass stylesheets when Sass is chosen', function(done) {
testingUtil.mockPrompt({
name: 'module',
csstype: 'sass',
example: ['todo']
});
gulp.start('default').once('stop', function() {
mockGulpDest.assertDestContains([
'src/app/app.scss',
'src/app/styles/_base.scss',
'src/app/components/todo/todo.scss'
]);
done();
});
});
});

})();
Loading

0 comments on commit c6281f8

Please sign in to comment.