From 2fb2d13851f45ec1e42d15d91e50b223688cfea9 Mon Sep 17 00:00:00 2001 From: Baruch Date: Thu, 8 Dec 2016 19:53:55 -0500 Subject: [PATCH] feat(module): component optional when generating module (#3389) * change URL query key name * generate component with module made optional * revert changes done to module cause open PR * components when generating module is optional * tests updated for component flag * fix parent/child compnent test to fit new flag * updated test for parent/child module generate * removed component testing from basic module e2e * adjusted e2e test for generate module * ng g m creates module only with routing flag * unit test for new generate module behavior * e2e tests for g m new behavior * remove spec test * shorten test module name to keep 100 char limit --- .../angular-cli/blueprints/module/index.js | 22 ++++++++++--------- tests/acceptance/generate-module.spec.js | 21 ++++++++++++++++++ .../e2e/tests/generate/module/module-basic.ts | 16 ++++++-------- .../tests/generate/module/module-routing.ts | 19 ++++++++-------- 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/packages/angular-cli/blueprints/module/index.js b/packages/angular-cli/blueprints/module/index.js index 1ee4be43aea8..c82dfdfa282a 100644 --- a/packages/angular-cli/blueprints/module/index.js +++ b/packages/angular-cli/blueprints/module/index.js @@ -59,16 +59,18 @@ module.exports = { afterInstall: function (options) { // Note that `this.generatePath` already contains `this.dasherizedModuleName` - // So, the path will end like `name/name`, + // So, the path will end like `name/name`, // which is correct for `name.component.ts` created in module `name` - var componentPath = path.join(this.generatePath, this.dasherizedModuleName); - options.entity.name = path.relative(this.dynamicPath.appRoot, componentPath); - options.flat = true; - options.route = false; - options.inlineTemplate = false; - options.inlineStyle = false; - options.prefix = true; - options.spec = true; - return Blueprint.load(path.join(__dirname, '../component')).install(options); + if (this.options && this.options.routing) { + var componentPath = path.join(this.generatePath, this.dasherizedModuleName); + options.entity.name = path.relative(this.dynamicPath.appRoot, componentPath); + options.flat = true; + options.route = false; + options.inlineTemplate = false; + options.inlineStyle = false; + options.prefix = true; + options.spec = true; + return Blueprint.load(path.join(__dirname, '../component')).install(options); + } } }; diff --git a/tests/acceptance/generate-module.spec.js b/tests/acceptance/generate-module.spec.js index fef9a2ed5091..a4e38274da4c 100644 --- a/tests/acceptance/generate-module.spec.js +++ b/tests/acceptance/generate-module.spec.js @@ -35,9 +35,19 @@ describe('Acceptance: ng generate module', function () { return ng(['generate', 'module', 'my-module']).then(() => { expect(existsSync(path.join(testPath, 'my-module', 'my-module.module.ts'))).to.equal(true); expect(existsSync(path.join(testPath, 'my-module', 'my-module.module.spec.ts'))).to.equal(false); + expect(existsSync(path.join(testPath, 'my-module', 'my-module.component.ts'))).to.equal(false); }); }); + it('ng generate module generate routing and component files when passed flag --routing', function () { + return ng(['generate', 'module', 'my-module', '--routing']).then( () => { + expect(existsSync(path.join(testPath, 'my-module', 'my-module.module.ts'))).to.equal(true); + expect(existsSync(path.join(testPath, 'my-module', 'my-module-routing.module.ts'))).to.equal(true); + expect(existsSync(path.join(testPath, 'my-module', 'my-module.module.spec.ts'))).to.equal(false); + expect(existsSync(path.join(testPath, 'my-module', 'my-module.component.ts'))).to.equal(true); + }) + }); + it('ng generate module my-module --spec', function () { return ng(['generate', 'module', 'my-module', '--spec']).then(() => { expect(existsSync(path.join(testPath, 'my-module', 'my-module.module.ts'))).to.equal(true); @@ -57,6 +67,17 @@ describe('Acceptance: ng generate module', function () { ng(['generate', 'module', 'parent/child']).then(() => { expect(existsSync(path.join(testPath, 'parent/child', 'child.module.ts'))).to.equal(true); expect(existsSync(path.join(testPath, 'parent/child', 'child.module.spec.ts'))).to.equal(false); + expect(existsSync(path.join(testPath, 'parent/child', 'child.component.ts'))).to.equal(false); + }) + ); + }); + + it('ng generate module should generate parent/child module with routing and component files when passed --routing flag', function () { + return ng(['generate', 'module', 'parent']).then(() => + ng(['generate', 'module', 'parent/child', '--routing']).then(() => { + expect(existsSync(path.join(testPath, 'parent/child', 'child.module.ts'))).to.equal(true); + expect(existsSync(path.join(testPath, 'parent/child', 'child-routing.module.ts'))).to.equal(true); + expect(existsSync(path.join(testPath, 'parent/child', 'child.module.spec.ts'))).to.equal(false); expect(existsSync(path.join(testPath, 'parent/child', 'child.component.ts'))).to.equal(true); }) ); diff --git a/tests/e2e/tests/generate/module/module-basic.ts b/tests/e2e/tests/generate/module/module-basic.ts index 071c20f3bea7..382aad79f871 100644 --- a/tests/e2e/tests/generate/module/module-basic.ts +++ b/tests/e2e/tests/generate/module/module-basic.ts @@ -5,17 +5,15 @@ import {expectToFail} from '../../../utils/utils'; export default function() { - const moduleDir = join('src', 'app', 'test-module'); + const moduleDir = join('src', 'app', 'test'); - return ng('generate', 'module', 'test-module') + return ng('generate', 'module', 'test') .then(() => expectFileToExist(moduleDir)) - .then(() => expectFileToExist(join(moduleDir, 'test-module.module.ts'))) - .then(() => expectToFail(() => expectFileToExist(join(moduleDir, 'test-module.routing.ts')))) - .then(() => expectFileToExist(join(moduleDir, 'test-module.component.ts'))) - .then(() => expectFileToExist(join(moduleDir, 'test-module.component.spec.ts'))) - .then(() => expectFileToExist(join(moduleDir, 'test-module.component.html'))) - .then(() => expectFileToExist(join(moduleDir, 'test-module.component.css'))) - .then(() => expectFileToMatch(join(moduleDir, 'test-module.module.ts'), 'TestModuleComponent')) + .then(() => expectFileToExist(join(moduleDir, 'test.module.ts'))) + .then(() => expectToFail(() => expectFileToExist(join(moduleDir, 'test-routing.module.ts')))) + .then(() => expectToFail(() => expectFileToExist(join(moduleDir, 'test.component.ts')))) + .then(() => expectToFail(() => expectFileToExist(join(moduleDir, 'test.spec.ts')))) + .then(() => expectFileToMatch(join(moduleDir, 'test.module.ts'), 'TestModule')) // Try to run the unit tests. .then(() => ng('test', '--single-run')); diff --git a/tests/e2e/tests/generate/module/module-routing.ts b/tests/e2e/tests/generate/module/module-routing.ts index 851d7ebdcf41..8b98e39d5918 100644 --- a/tests/e2e/tests/generate/module/module-routing.ts +++ b/tests/e2e/tests/generate/module/module-routing.ts @@ -1,20 +1,21 @@ import {join} from 'path'; import {ng} from '../../../utils/process'; import {expectFileToExist} from '../../../utils/fs'; +import {expectToFail} from '../../../utils/utils'; export default function() { - const moduleDir = join('src', 'app', 'test-module'); + const moduleDir = join('src', 'app', 'test'); - return ng('generate', 'module', 'test-module', '--routing') + return ng('generate', 'module', 'test', '--routing') .then(() => expectFileToExist(moduleDir)) - .then(() => expectFileToExist(join(moduleDir, 'test-module.module.ts'))) - .then(() => expectFileToExist(join(moduleDir, 'test-module-routing.module.ts'))) - .then(() => expectFileToExist(join(moduleDir, 'test-module.component.ts'))) - .then(() => expectFileToExist(join(moduleDir, 'test-module.component.spec.ts'))) - .then(() => expectFileToExist(join(moduleDir, 'test-module.component.html'))) - .then(() => expectFileToExist(join(moduleDir, 'test-module.component.css'))) - + .then(() => expectFileToExist(join(moduleDir, 'test.module.ts'))) + .then(() => expectFileToExist(join(moduleDir, 'test-routing.module.ts'))) + .then(() => expectFileToExist(join(moduleDir, 'test.component.ts'))) + .then(() => expectFileToExist(join(moduleDir, 'test.component.spec.ts'))) + .then(() => expectFileToExist(join(moduleDir, 'test.component.html'))) + .then(() => expectFileToExist(join(moduleDir, 'test.component.css'))) + .then(() => expectToFail(() => expectFileToExist(join(moduleDir, 'test.spec.ts')))) // Try to run the unit tests. .then(() => ng('test', '--single-run')); }