Skip to content

Commit ce9fd2d

Browse files
Angular tests subgenerator and test added
1 parent eb527fb commit ce9fd2d

File tree

4 files changed

+210
-0
lines changed

4 files changed

+210
-0
lines changed

angular-tests/index.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict';
2+
3+
var fs = require('fs'),
4+
s = require('underscore.string'),
5+
yeoman = require('yeoman-generator');
6+
7+
var TestGenerator = yeoman.generators.Base.extend({
8+
askForModuleName: function () {
9+
var modulesFolder = process.cwd() + '/modules/';
10+
var done = this.async();
11+
12+
var prompts = [{
13+
type: 'list',
14+
name: 'moduleName',
15+
default: 'core',
16+
message: 'Which module does this test belongs to?',
17+
choices: []
18+
},{
19+
type: 'input',
20+
name: 'name',
21+
default: '',
22+
message: 'What is the name of the tests (leave it blank to inherit it from module)?'
23+
}];
24+
25+
// Add module choices
26+
if (fs.existsSync(modulesFolder)) {
27+
fs.readdirSync(modulesFolder).forEach(function (folder) {
28+
var stat = fs.statSync(modulesFolder + '/' + folder);
29+
30+
if (stat.isDirectory()) {
31+
prompts[0].choices.push({
32+
value: folder,
33+
name: folder
34+
});
35+
}
36+
});
37+
}
38+
39+
this.prompt(prompts, function (props) {
40+
this.moduleName = props.moduleName;
41+
this.name = props.name || this.moduleName;
42+
43+
this.slugifiedModuleName = s(this.moduleName).slugify().value();
44+
45+
this.slugifiedControllerName = s(this.name).humanize().slugify().value();
46+
this.classifiedControllerName = s(this.slugifiedControllerName).classify().value();
47+
this.humanizedControllerName = s(this.slugifiedControllerName).humanize().value();
48+
49+
done();
50+
}.bind(this));
51+
},
52+
53+
renderTestsFile: function () {
54+
var controllerFilePath = process.cwd() + '/modules/' + this.slugifiedModuleName + '/client/controllers/' + this.slugifiedControllerName + '.client.controller.js';
55+
56+
// If controller file exists we create a test for it otherwise we will first create a controller
57+
if (!fs.existsSync(controllerFilePath)) {
58+
this.template('_.client.controller.js', 'modules/' + this.slugifiedModuleName + '/client/controllers/' + this.slugifiedControllerName + '.client.controller.js')
59+
}
60+
61+
// If controller file exists we create a test for it otherwise we will first create a controller
62+
if (!fs.existsSync(controllerFilePath)) {
63+
this.template('_.client.controller.js', 'modules/' + this.slugifiedModuleName + '/client/controllers/' + this.slugifiedControllerName + '.client.controller.js')
64+
}
65+
66+
this.template('_.client.controller.tests.js', 'modules/' + this.slugifiedModuleName + '/tests/client/' + this.slugifiedControllerName + '.client.controller.tests.js')
67+
}
68+
});
69+
70+
module.exports = TestGenerator;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
angular.module('<%= slugifiedModuleName %>').controller('<%= classifiedControllerName %>Controller', ['$scope',
4+
function ($scope) {
5+
// <%= humanizedControllerName %> controller logic
6+
// ...
7+
}
8+
]);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
(function() {
4+
// <%= humanizedControllerName %> Controller Spec
5+
describe('<%= humanizedControllerName %> Controller Tests', function() {
6+
// Initialize global variables
7+
var <%= classifiedControllerName %>Controller,
8+
$scope,
9+
$httpBackend,
10+
$stateParams,
11+
$location;
12+
13+
// The $resource service augments the response object with methods for updating and deleting the resource.
14+
// If we were to use the standard toEqual matcher, our tests would fail because the test values would not match
15+
// the responses exactly. To solve the problem, we define a new toEqualData Jasmine matcher.
16+
// When the toEqualData matcher compares two objects, it takes only object properties into
17+
// account and ignores methods.
18+
beforeEach(function() {
19+
jasmine.addMatchers({
20+
toEqualData: function(util, customEqualityTesters) {
21+
return {
22+
compare: function(actual, expected) {
23+
return {
24+
pass: angular.equals(actual, expected)
25+
};
26+
}
27+
};
28+
}
29+
});
30+
});
31+
32+
// Then we can start by loading the main application module
33+
beforeEach(module(ApplicationConfiguration.applicationModuleName));
34+
35+
// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
36+
// This allows us to inject a service but then attach it to a variable
37+
// with the same name as the service.
38+
beforeEach(inject(function($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_) {
39+
// Set a new global scope
40+
$scope = $rootScope.$new();
41+
42+
// Point global variables to injected services
43+
$stateParams = _$stateParams_;
44+
$httpBackend = _$httpBackend_;
45+
$location = _$location_;
46+
47+
// Initialize the <%= humanizedControllerName %> controller.
48+
<%= classifiedControllerName %>Controller = $controller('<%= classifiedControllerName %>Controller', {
49+
$scope: $scope
50+
});
51+
}));
52+
53+
it('Should do some controller test', inject(function() {
54+
// The test logic
55+
// ...
56+
}));
57+
});
58+
}());

test/angular-tests.test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
3+
var path = require('path'),
4+
helpers = require('yeoman-generator').test,
5+
assert = require('yeoman-generator').assert,
6+
temp = require('temp').track();
7+
8+
describe('AngularJS Sub Generators Tests', function () {
9+
this.timeout(0);
10+
/**
11+
* Setup the temp directory
12+
*/
13+
before(function (done) {
14+
helpers.testDirectory(path.join(__dirname, 'temp'), done);
15+
});
16+
17+
/**
18+
* Clean up temp directory
19+
*/
20+
after(function () {
21+
temp.cleanup();
22+
});
23+
24+
describe('Generate an AngularJS controller with tests through the sub-generator', function () {
25+
beforeEach(function (done) {
26+
27+
helpers.run(path.join(__dirname, '../angular-tests'))
28+
.withOptions({
29+
'skip-install': true
30+
})
31+
.withPrompts({
32+
'moduleName': 'core',
33+
'name': 'foo'
34+
})
35+
.on('ready', function (generator) {
36+
// this is called right before `generator.run()` is called
37+
})
38+
.on('end', function () {
39+
done();
40+
});
41+
});
42+
43+
it('should generate an angular controller and tests file', function (done) {
44+
assert.file('modules/core/client/controllers/foo.client.controller.js');
45+
assert.file('modules/core/tests/client/foo.client.controller.tests.js');
46+
done();
47+
});
48+
});
49+
50+
describe('Generate an AngularJS controller with tests through the sub-generator (no name specified)', function () {
51+
beforeEach(function (done) {
52+
53+
helpers.run(path.join(__dirname, '../angular-tests'))
54+
.withOptions({
55+
'skip-install': true
56+
})
57+
.withPrompts({
58+
'moduleName': 'core'
59+
})
60+
.on('ready', function (generator) {
61+
// this is called right before `generator.run()` is called
62+
})
63+
.on('end', function () {
64+
done();
65+
});
66+
});
67+
68+
it('should generate an angular controller and tests file', function (done) {
69+
assert.file('modules/core/client/controllers/core.client.controller.js');
70+
assert.file('modules/core/tests/client/core.client.controller.tests.js');
71+
done();
72+
});
73+
});
74+
});

0 commit comments

Comments
 (0)