From f4cb5f794bb9303719e3321528f147eacb0093ea Mon Sep 17 00:00:00 2001 From: Simon Boudrias Date: Wed, 30 Dec 2015 03:23:07 -0500 Subject: [PATCH] Combine and refactor angular:app tests --- test/app-with-appname.js | 58 ----------- test/app-with-apppath.js | 83 --------------- test/app.js | 144 ++++++++++++++++++++++++++ test/test-file-creation.js | 201 ------------------------------------- 4 files changed, 144 insertions(+), 342 deletions(-) delete mode 100644 test/app-with-appname.js delete mode 100644 test/app-with-apppath.js create mode 100644 test/app.js delete mode 100644 test/test-file-creation.js diff --git a/test/app-with-appname.js b/test/app-with-appname.js deleted file mode 100644 index 158f338f8..000000000 --- a/test/app-with-appname.js +++ /dev/null @@ -1,58 +0,0 @@ -'use strict'; - -var path = require('path'); -var helpers = require('yeoman-test'); -var assert = require('yeoman-assert'); - -describe('angular:app appName', function () { - beforeEach(function (done) { - helpers - .run(require.resolve('../app')) - .withGenerators([ - require.resolve('../common'), - require.resolve('../controller'), - require.resolve('../main'), - [helpers.createDummyGenerator(), 'karma:app'] - ]) - .withOptions({ - 'appPath': 'app', - 'skip-welcome-message': true, - 'skip-message': true - }) - .withArguments(['upperCaseBug']) - .withPrompts({ - compass: true, - bootstrap: true, - compassBootstrap: true, - modules: [] - }) - .on('end', done); - }); - - it('generates the same appName in every file', function () { - assert.file([ - 'app/scripts/app.js', - 'app/scripts/controllers/main.js', - 'app/index.html', - 'test/spec/controllers/main.js' - ]); - - assert.fileContent( - 'app/scripts/app.js', - /module\('upperCaseBugApp'/ - ); - assert.fileContent( - 'app/scripts/controllers/main.js', - /module\('upperCaseBugApp'/ - ); - assert.fileContent( - 'test/spec/controllers/main.js', - /module\('upperCaseBugApp'/ - ); - - assert.fileContent( - 'app/index.html', - /ng-app="upperCaseBugApp"/ - ); - }); -}); diff --git a/test/app-with-apppath.js b/test/app-with-apppath.js deleted file mode 100644 index c10f9e927..000000000 --- a/test/app-with-apppath.js +++ /dev/null @@ -1,83 +0,0 @@ -'use strict'; - -var path = require('path'); -var helpers = require('yeoman-test'); -var assert = require('yeoman-assert'); -var _ = require('underscore.string'); - -describe('Angular generator appPath option', function () { - var appPath = 'customAppPath'; - var expected = [ - appPath + '/404.html', - appPath + '/favicon.ico', - appPath + '/robots.txt', - appPath + '/styles/main.scss', - appPath + '/views/main.html', - appPath + '/index.html', - '.bowerrc', - '.editorconfig', - '.gitignore', - '.jshintrc', - 'Gruntfile.js', - 'package.json', - 'bower.json' - ]; - - beforeEach(function () { - this.angular = helpers - .run(require.resolve('../app')) - .withGenerators([ - require.resolve('../common'), - require.resolve('../controller'), - require.resolve('../main'), - [helpers.createDummyGenerator(), 'karma:app'] - ]) - .withOptions({ - 'appPath': appPath, - 'skip-welcome-message': true, - 'skip-message': true - }) - .withArguments(['upperCaseBug']) - .withPrompts({ - compass: true, - bootstrap: true, - compassBootstrap: true, - modules: [] - }); - }); - - describe('default settings', function () { - beforeEach(function (done) { - this.angular.on('end', done); - }); - - it('generates base files inside the appPath', function () { - assert.file(expected); - }); - - it('creates JS files in appPath', function () { - assert.file([ - '.jscsrc', - appPath + '/scripts/app.js', - appPath + '/scripts/controllers/main.js', - 'test/spec/controllers/main.js' - ]); - }); - }); - - describe('--coffee', function () { - beforeEach(function (done) { - this.angular.withOptions({ - coffee: true - }).on('end', done); - }); - - it('creates CoffeeScript files', function () { - assert.file([].concat(expected, [ - appPath + '/scripts/app.coffee', - appPath + '/scripts/controllers/main.coffee', - 'test/spec/controllers/main.coffee' - ])); - }); - }); -}); diff --git a/test/app.js b/test/app.js new file mode 100644 index 000000000..5e2116cf2 --- /dev/null +++ b/test/app.js @@ -0,0 +1,144 @@ +'use strict'; + +var path = require('path'); +var helpers = require('yeoman-test'); +var assert = require('yeoman-assert'); + +var getDefaultFilesForAppPath = function (appPath) { + return [ + appPath + '/404.html', + appPath + '/favicon.ico', + appPath + '/robots.txt', + appPath + '/styles/main.scss', + appPath + '/views/main.html', + appPath + '/index.html', + '.bowerrc', + '.editorconfig', + '.gitignore', + '.jshintrc', + 'Gruntfile.js', + 'package.json', + 'bower.json' + ]; +}; + +describe('angular:app', function () { + var appPath = 'customAppPath'; + + beforeEach(function () { + this.angular = helpers + .run(require.resolve('../app')) + .withGenerators([ + require.resolve('../common'), + require.resolve('../controller'), + require.resolve('../main'), + [helpers.createDummyGenerator(), 'karma:app'] + ]) + .withOptions({ + 'skip-welcome-message': true, + 'skip-message': true + }) + .withArguments(['upperCaseBug']) + .withPrompts({ + compass: true, + bootstrap: true, + compassBootstrap: true, + modules: [] + }); + }); + + describe('default settings', function () { + beforeEach(function (done) { + this.angular.on('end', done); + }); + + it('generates base files', function () { + assert.file(getDefaultFilesForAppPath('app')); + assert.file([ + '.jscsrc', + 'app/index.html', + 'app/scripts/app.js', + 'app/scripts/controllers/main.js', + 'test/spec/controllers/main.js' + ]); + }); + }); + + describe('--coffee', function () { + beforeEach(function (done) { + this.angular.withOptions({ + coffee: true + }).on('end', done); + }); + + it('generates CoffeeScript files', function () { + assert.file([].concat(getDefaultFilesForAppPath('app'), [ + 'app/scripts/app.coffee', + 'app/scripts/controllers/main.coffee', + 'test/spec/controllers/main.coffee' + ])); + }); + }); + + describe('--typescript', function () { + beforeEach(function (done) { + this.angular.withOptions({ + typescript: true + }).on('end', done); + }); + + it('generates CoffeeScript files', function () { + assert.file([].concat(getDefaultFilesForAppPath('app'), [ + 'app/scripts/app.ts', + 'app/scripts/controllers/main.ts', + 'test/spec/controllers/main.ts' + ])); + }); + }); + + describe('--appPath', function () { + beforeEach(function (done) { + this.angular.withOptions({ + appPath: 'alternative' + }).on('end', done); + }); + + it('generates base files inside the appPath', function () { + assert.file(getDefaultFilesForAppPath('alternative')); + assert.file([ + '.jscsrc', + 'alternative/scripts/app.js', + 'alternative/scripts/controllers/main.js', + 'test/spec/controllers/main.js' + ]); + }); + }); + + describe('--appName', function () { + beforeEach(function (done) { + this.angular + .withArguments(['upperCaseBug']) + .on('end', done); + }); + + it('generates the same appName in every file', function () { + assert.fileContent( + 'app/scripts/app.js', + /module\('upperCaseBugApp'/ + ); + assert.fileContent( + 'app/scripts/controllers/main.js', + /module\('upperCaseBugApp'/ + ); + assert.fileContent( + 'test/spec/controllers/main.js', + /module\('upperCaseBugApp'/ + ); + + assert.fileContent( + 'app/index.html', + /ng-app="upperCaseBugApp"/ + ); + }); + }); +}); diff --git a/test/test-file-creation.js b/test/test-file-creation.js deleted file mode 100644 index 64b32bdb0..000000000 --- a/test/test-file-creation.js +++ /dev/null @@ -1,201 +0,0 @@ -'use strict'; - -var path = require('path'); -var helpers = require('yeoman-generator').test; -var assert = require('yeoman-assert'); -var _ = require('underscore.string'); - -describe('Angular generator file creation', function () { - var angular; - var expected = [ - 'app/404.html', - 'app/favicon.ico', - 'app/robots.txt', - 'app/styles/main.scss', - 'app/views/main.html', - 'app/index.html', - '.bowerrc', - '.editorconfig', - '.gitignore', - '.jshintrc', - 'Gruntfile.js', - 'package.json', - 'bower.json' - ]; - var mockPrompts = { - compass: true, - bootstrap: true, - compassBootstrap: true, - modules: [] - }; - var genOptions = { - 'appPath': 'app', - 'skip-install': true, - 'skip-welcome-message': true, - 'skip-message': true - }; - - beforeEach(function (done) { - helpers.testDirectory(path.join(__dirname, 'tmp', 'file'), function (err) { - if (err) { - done(err); - } - angular = helpers.createGenerator( - 'angular:app', - [ - '../../../app', - '../../../common', - '../../../controller', - '../../../main', - [ helpers.createDummyGenerator(), 'karma:app' ] - ], - false, - genOptions - ); - helpers.mockPrompt(angular, mockPrompts); - - done(); - }); - }); - - describe('App files', function () { - it('should generate dotfiles', function (done) { - angular.run({}, function () { - assert.file(expected); - done(); - }); - }); - - it('creates expected JS files', function (done) { - angular.run({}, function() { - assert.file([].concat(expected, [ - '.jscsrc', - 'app/scripts/app.js', - 'app/scripts/controllers/main.js', - 'test/spec/controllers/main.js' - ])); - done(); - }); - }); - - it('creates CoffeeScript files', function (done) { - angular.env.options.coffee = true; - angular.run([], function () { - assert.file([].concat(expected, [ - 'app/scripts/app.coffee', - 'app/scripts/controllers/main.coffee', - 'test/spec/controllers/main.coffee' - ])); - done(); - }); - }); - }); - - describe('Service Subgenerators', function () { - var generatorTest = function (generatorType, specType, targetDirectory, scriptNameFn, specNameFn, suffix, done) { - var name = 'foo'; - var deps = [path.join('../../..', generatorType)]; - var genTester = helpers.createGenerator('angular:' + generatorType, deps, [name], genOptions); - - angular.run([], function () { - genTester.run([], function () { - assert.fileContent([ - [ - path.join('app/scripts', targetDirectory, name + '.js'), - new RegExp( - generatorType + '\\(\'' + scriptNameFn(name) + suffix + '\'', - 'g' - ) - ], - [ - path.join('test/spec', targetDirectory, name + '.js'), - new RegExp( - 'describe\\(\'' + _.classify(specType) + ': ' + specNameFn(name) + suffix + '\'', - 'g' - ) - ] - ]); - done(); - }); - }); - }; - - it('should generate a new controller', function (done) { - generatorTest('controller', 'controller', 'controllers', _.classify, _.classify, 'Ctrl', done); - }); - - it('should generate a new directive', function (done) { - generatorTest('directive', 'directive', 'directives', _.camelize, _.camelize, '', done); - }); - - it('should generate a new filter', function (done) { - generatorTest('filter', 'filter', 'filters', _.camelize, _.camelize, '', done); - }); - - ['constant', 'factory', 'provider', 'value'].forEach(function(t) { - it('should generate a new ' + t, function (done) { - generatorTest(t, 'service', 'services', _.camelize, _.camelize, '', done); - }); - }); - - it('should generate a new service', function (done) { - generatorTest('service', 'service', 'services', _.capitalize, _.capitalize, '', done); - }); - - it('creates typescript files', function (done) { - var expected = [ - 'app/404.html', - 'app/favicon.ico', - 'app/robots.txt', - 'app/styles/main.scss', - 'app/views/main.html', - '.bowerrc', - 'Gruntfile.js', - 'package.json', - 'bower.json', - 'app/scripts/app.ts', - 'app/index.html', - 'app/scripts/controllers/main.ts', - 'test/spec/controllers/main.ts' - ]; - helpers.mockPrompt(angular, { - compass: true, - bootstrap: true, - compassBootstrap: true, - modules: [] - }); - - angular.env.options.typescript = true; - angular.run([], function () { - assert.file(expected); - done(); - }); - }); - }); - - describe('View', function () { - it('should generate a new view', function (done) { - var angularView; - var deps = [ '../../../view' ]; - angularView = helpers.createGenerator('angular:view', deps, ['foo'], genOptions); - - helpers.mockPrompt(angularView, mockPrompts); - angularView.run([], function () { - assert.file(['app/views/foo.html']); - done(); - }); - }); - - it('should generate a new view in subdirectories', function (done) { - var angularView; - var deps = [ '../../../view' ]; - angularView = helpers.createGenerator('angular:view', deps, ['foo/bar'], genOptions); - - helpers.mockPrompt(angularView, mockPrompts); - angularView.run([], function () { - assert.file(['app/views/foo/bar.html']); - done(); - }); - }); - }); -});