Skip to content

Commit

Permalink
add tests for the rang:screen command
Browse files Browse the repository at this point in the history
  • Loading branch information
rise2semi committed Dec 9, 2014
1 parent c4347ca commit e40e6fb
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
node_modules
test/tmp
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ yo rang

**Note: Generators are to be run from the root directory of your app.**

### Screen
### Screen

Generates a screen module with a controller and view.

Example:

```bash
yo rang:screen
```

## Testing

TODO
```bash
gulp test
```
9 changes: 8 additions & 1 deletion generators/screen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,18 @@ RangScreenGenerator.prototype.writing = function () {
}, {});
};

/**
* Read an application dependencies
*/
RangScreenGenerator.prototype._getDependencies = function () {
return this.readFileAsString( config.appModules );
};

/**
* Add module as the application dependency
*/
RangScreenGenerator.prototype.updateDependencies = function () {
var appModulesSrc = this.readFileAsString( config.appModules );
var appModulesSrc = this._getDependencies();
var appModules = [];
var lastModule;

Expand Down
23 changes: 23 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

/**
* Dependencies
*/
var gulp = require('gulp');
var mocha = require('gulp-mocha');

gulp.task('test', function () {
return gulp.src(['test/test-*.js'], { read: false })
.pipe(mocha({
reporter: 'dot',
globals: {
chai: require('chai')
}
}));
});

gulp.task('watch-test', function() {
gulp.watch(['generators/**', 'test/**'], ['test']);
});

gulp.task('default', ['test']);
20 changes: 19 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,23 @@
"dependencies": {
"chalk": "^0.5.1",
"yeoman-generator": "^0.17.7"
}
},
"main": "app/index.js",
"devDependencies": {
"fixture-stdout": "^0.2.1",
"gulp": "^3.8.10",
"gulp-mocha": "^2.0.0"
},
"scripts": {
"test": "gulp test"
},
"repository": {
"type": "git",
"url": "https://github.com/rise2semi/generator-rang.git"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/rise2semi/generator-rang/issues"
},
"homepage": "https://github.com/rise2semi/generator-rang"
}
53 changes: 53 additions & 0 deletions test/mute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Silence Yeoman during tests
* uses https://github.com/balderdashy/fixture-stdout
*/

'use strict';

var Fixture = require('fixture-stdout');
var fixtureOut = new Fixture();
var fixtureErr = new Fixture({
stream: process.stderr
});

var _writesOut = [];
var _writesErr = [];

/**
* Mute
*/
function mute() {
var captureStdout = function ( string ) {
_writesOut.push({ string: string });
return false;
};

var captureStderr = function ( string ) {
_writesErr.push({ string: string });
return false;
};

fixtureOut.capture( captureStdout );
fixtureErr.capture( captureStderr );
}

/**
* Unmute
*/
function unmute() {
fixtureOut.release();
fixtureErr.release();
}

// Return the output that was captured
function getMutedWrites() {
return {
out: _writesOut,
err: _writesErr
}
}

module.exports.mute = mute;
module.exports.unmute = unmute;
module.exports.getMutedWrites = getMutedWrites;
76 changes: 76 additions & 0 deletions test/test-screen-creation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';

/**
* Dependencies
*/
var path = require('path');
var helpers = require('yeoman-generator').test;
var assert = require('yeoman-generator').assert;
var output = require('./mute');

describe('rang:screen generator', function () {
var rangScreen;
var tempFolder = 'tmp/tmpRangScreen';
var mockPrompts = {
screen: 'test',
url: 'test'
};

var expectedFile = [
'workspace/scripts/app/screens/test/main.js',
'workspace/scripts/app/screens/test/templates/test.tmpl.html',
'workspace/scripts/app/screens/test/controllers/testController.js'
];

var expectedContent = [
[
'workspace/scripts/app/screens/test/main.js',
/var testTemplate = require\('text!\.\/templates\/test\.tmpl\.html'\);/
],
[
'workspace/scripts/app/screens/test/main.js',
/var testController = require\('\.\/controllers\/testController'\);/
],
[ 'workspace/scripts/app/screens/test/main.js', /Module: test screen/ ],
[ 'workspace/scripts/app/screens/test/main.js', /var module = angular.module\('app\.test', \[]\);/ ],
[ 'workspace/scripts/app/screens/test/main.js', /controller\( testController \)/ ],
[ 'workspace/scripts/app/screens/test/main.js', /state\('root\.test'/ ],
[ 'workspace/scripts/app/screens/test/main.js', /url: '\/test',/ ],
[ 'workspace/scripts/app/screens/test/main.js', /template: testTemplate,/ ],
[ 'workspace/scripts/app/screens/test/main.js', /controller: 'testController'/ ],
[ 'workspace/scripts/app/screens/test/controllers/testController.js', /testController:/ ],
[ 'workspace/scripts/app/appModules.js', /require\('screens\/test\/main'\)/ ]
];

beforeEach(function (done) {
helpers.testDirectory( path.join( __dirname, tempFolder ), function ( error ) {
if ( error ) {
done( error );
}

rangScreen = helpers.createGenerator( 'rang:screen', ['../../../generators/screen'], [], {} );
rangScreen.on('run', output.mute);
rangScreen.on('end', output.unmute);

helpers.stub( rangScreen, '_getDependencies', function () {
return "return [\n" +
" require('screens/old/module')\n" +
"];";
});

done();
});
});

it('should generate the expected files and their content', function ( done ) {
helpers.mockPrompt( rangScreen, mockPrompts );

rangScreen.run({}, function () {

assert.file( expectedFile );
assert.fileContent( expectedContent );

done();
});
});
});

0 comments on commit e40e6fb

Please sign in to comment.