diff --git a/.gitignore b/.gitignore index 7a1537b..53cf153 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea node_modules +test/tmp diff --git a/README.md b/README.md index e1c3657..0855536 100644 --- a/README.md +++ b/README.md @@ -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 +``` diff --git a/generators/screen/index.js b/generators/screen/index.js index 0d99291..c04bca0 100644 --- a/generators/screen/index.js +++ b/generators/screen/index.js @@ -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; diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..36a69a0 --- /dev/null +++ b/gulpfile.js @@ -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']); diff --git a/package.json b/package.json index 57eba5b..3435717 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/test/mute.js b/test/mute.js new file mode 100644 index 0000000..fea91fb --- /dev/null +++ b/test/mute.js @@ -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; diff --git a/test/test-screen-creation.js b/test/test-screen-creation.js new file mode 100644 index 0000000..b544cd5 --- /dev/null +++ b/test/test-screen-creation.js @@ -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(); + }); + }); +});