diff --git a/app/index.js b/app/index.js index fb5b70c..5654180 100644 --- a/app/index.js +++ b/app/index.js @@ -59,6 +59,7 @@ ReactWebpackGenerator.prototype.createIndexHtml = function createIndexHtml() { ReactWebpackGenerator.prototype.packageFiles = function () { this.template('../../templates/common/_package.json', 'package.json'); this.copy('../../templates/common/Gruntfile.js', 'Gruntfile.js'); + this.copy('../../templates/common/gitignore', '.gitignore'); }; ReactWebpackGenerator.prototype.styleFiles = function styleFiles() { diff --git a/package.json b/package.json index fbe5ca8..b8b2656 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "yeoman-generator": "~0.14.0" }, "devDependencies": { - "mocha": "~1.14.0" + "mocha": "~1.14.0", + "underscore.string": "~2.3.1" }, "peerDependencies": { "yo": ">=1.0.0" diff --git a/test/test-creation.js b/test/test-creation.js index afbabb7..a928c7d 100644 --- a/test/test-creation.js +++ b/test/test-creation.js @@ -1,38 +1,101 @@ /*global describe, beforeEach, it*/ 'use strict'; -var path = require('path'); +var path = require('path'); var helpers = require('yeoman-generator').test; - +var _ = require('underscore.string'); describe('react-webpack generator', function () { + var react; + beforeEach(function (done) { + var deps = [ + '../../app', + '../../common', + '../../component', + '../../main' + ]; helpers.testDirectory(path.join(__dirname, 'temp'), function (err) { if (err) { return done(err); } - this.app = helpers.createGenerator('react-webpack:app', [ - '../../app' - ]); + react = helpers.createGenerator('react-webpack:app', deps); + react.options['skip-install'] = true; done(); }.bind(this)); }); + it('should generate dotfiles', function (done) { + react.run({}, function () { + helpers.assertFiles(['.gitignore', '.editorconfig', '.jshintrc']); + done(); + }); + }); + it('creates expected files', function (done) { - var expected = [ - // add files you expect to exist here. - '.jshintrc', - '.editorconfig' + var expected = ['src/favicon.ico', + 'src/styles/reset.css', + 'src/styles/main.css', + 'src/index.html', + 'Gruntfile.js', + 'karma.conf.js', + 'package.json', + 'package.json', + 'src/scripts/components/TempApp.js', + 'test/helpers/phantomjs-shims.js', + 'test/helpers/react/addons.js' ]; - helpers.mockPrompt(this.app, { - 'someOption': true - }); - this.app.options['skip-install'] = true; - this.app.run({}, function () { + react.run({}, function () { helpers.assertFiles(expected); done(); }); }); + + /** + * Generic test function that can be used to cover the scenarios where a generator is creating both a source file + * and a test file. The function will run the respective generator, and then check for the existence of the two + * generated files. A RegExp check is done on each file, checking for the generated content with a pattern. + * + * The number of parameters is quite huge due to the many options in which the generated files differ, + * e.g. Components start with an upper case letter. + * + * The generated items all use the dummy name 'foo'. + * + * @param generatorType The type of generator to run, e.g. 'component'. + * @param specType The type of the generated spec file, e.g. 'component'. + * @param targetDirectory The directory into which the files are generated, e.g. 'components' - this will be + * located under 'src/scripts/components' for the sources and 'test/spec/components' for the tests. + * @param scriptNameFn The function used to create the name of the created item, e.g. _.classify to generate 'Foo', + * or _.camelize to generate 'foo'. + * @param specNameFn Same as scriptNameFn, but for the describe text used in the Spec file. Some generators use + * _.classify, others use _.camelize. + * @param suffix An optional suffix to be appended to the generated item name. + * @param done The done function. + */ + + function generatorTest(generatorType, specType, targetDirectory, scriptNameFn, specNameFn, suffix, done) { + var reactGenerator; + var name = 'Foo'; + var deps = [path.join('../..', generatorType)]; + reactGenerator = helpers.createGenerator('react-webpack:' + generatorType, deps, [name]); + react.run([], function () { + //var Foo = React.createClass({ + reactGenerator.run([], function () { + helpers.assertFiles([ + [path.join('src/scripts', targetDirectory, name + '.js'), new RegExp('var ' + scriptNameFn(name) + suffix, 'g')], + [path.join('test/spec', targetDirectory, name + '.js'), new RegExp('describe\\(\'' + specNameFn(name) + suffix + '\'', 'g')] + ]); + done(); + }); + }); + } + + describe('Component', function () { + it('should generate a new component', function (done) { + generatorTest('component', 'component', 'components', _.capitalize, _.capitalize, '', done); + }); + }); + }); diff --git a/test/test-load.js b/test/test-load.js deleted file mode 100644 index 344c7c3..0000000 --- a/test/test-load.js +++ /dev/null @@ -1,11 +0,0 @@ -/*global describe, beforeEach, it*/ -'use strict'; - -var assert = require('assert'); - -describe('react-webpack generator', function () { - it('can be imported without blowing up', function () { - var app = require('../app'); - assert(app !== undefined); - }); -});