diff --git a/test/test-creation.js b/test/test-creation.js index 83615c0..e9e589b 100644 --- a/test/test-creation.js +++ b/test/test-creation.js @@ -5,99 +5,124 @@ var path = require('path'); var helpers = require('yeoman-generator').test; var _ = require('underscore.string'); -describe('react-webpack generator', function () { - var react; +describe('react-webpack generator', function() { + var react; + var expected = [ + 'src/favicon.ico', + 'src/styles/normalize.css', + 'src/styles/main.css', + 'src/index.html', + 'Gruntfile.js', + 'webpack.config.js', + 'karma.conf.js', + 'package.json' + ]; + var mockPrompts = {}; + var genOptions = { + 'appPath': 'src', + 'skip-install': true, + 'skip-welcome-message': true, + 'skip-message': true + }; + var deps = [ + '../../app', + '../../common', + '../../component', + '../../main' + ]; - beforeEach(function (done) { - var deps = [ - '../../app', - '../../common', - '../../component', - '../../main' - ]; - helpers.testDirectory(path.join(__dirname, 'temp-test'), function (err) { - if (err) { - return done(err); - } + beforeEach(function(done) { + helpers.testDirectory(path.join(__dirname, 'temp-test'), function(err) { + if (err) { + return done(err); + } + react = helpers.createGenerator('react-webpack:app', deps, false, genOptions); + helpers.mockPrompt(react, mockPrompts); - react = helpers.createGenerator('react-webpack:app', deps); - react.options['skip-install'] = true; - done(); - }.bind(this)); + done(); + }); + }); + + describe('App files', function() { + it('should generate dotfiles', function(done) { + react.run({}, function() { + helpers.assertFile([].concat(expected, [ + '.editorconfig', + '.gitignore', + '.jshintrc' + ])); + done(); + }); }); - it('should generate dotfiles', function (done) { - react.run({}, function () { - helpers.assertFiles(['.gitignore', '.editorconfig', '.jshintrc']); - done(); - }); - }); - - it('creates expected files', function (done) { - var expected = ['src/favicon.ico', - 'src/styles/normalize.css', - 'src/styles/main.css', - 'src/index.html', - 'Gruntfile.js', - 'webpack.config.js', - 'karma.conf.js', - 'package.json', - 'package.json', - 'src/scripts/components/TempTestApp.jsx', - 'test/helpers/phantomjs-shims.js', - 'test/helpers/react/addons.js', - 'test/spec/components/TempTestApp.js', - ]; - - 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 + '.jsx'), 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); + it('should generate app files', function(done) { + react.run({}, function() { + // TODO: Hack, no time to work out why generated + // files not present at point of test... + setTimeout(function() { + helpers.assertFile(expected); + done(); }); }); + }); + + it('should generate expected JS files', function(done) { + react.run({}, function() { + setTimeout(function() { + helpers.assertFile([].concat(expected, [ + 'src/scripts/components/TempTestApp.jsx', + 'src/scripts/components/main.jsx' + ])); + done(); + }); + }); + }); + + it('should generate expected test JS files', function(done) { + react.run({}, function() { + // TODO: Hack, no time to work out why generated + // files not present at point of test... + setTimeout(function() { + helpers.assertFile([].concat(expected, [ + 'test/helpers/phantomjs-shims.js', + 'test/helpers/react/addons.js', + 'test/spec/components/TempTestApp.js' + ])); + done(); + }); + }); + }); + + }); + + describe('Subgenerators', function() { + var generatorTest = function(generatorType, specType, targetDirectory, scriptNameFn, specNameFn, suffix, done) { + + var name = 'Foo'; + var deps = [path.join('../..', generatorType)]; + genOptions.appPath += '/scripts' + + var reactGenerator = helpers.createGenerator('react-webpack:' + generatorType, deps, [name], genOptions); + + react.run([], function() { + reactGenerator.run([], function() { + helpers.assertFileContent([ + + [path.join('src/scripts', targetDirectory, name + '.jsx'), new RegExp('var ' + scriptNameFn(name) + suffix, 'g')], + [path.join('test/spec', targetDirectory, name + '.js'), new RegExp('describe\\(\'' + specNameFn(name) + suffix + '\'', 'g')] + + ]); + done(); + }); + }); + } + + it('should generate a new component', function(done) { + react.run({}, function() { + generatorTest('component', 'component', 'components', _.capitalize, _.capitalize, '', done); + }); + }); + + }); });