From e5c2aedbd3ab9c56fd729e28af5ed2253b5ce53b Mon Sep 17 00:00:00 2001 From: Brandon Date: Sat, 6 Jan 2018 18:30:37 -0800 Subject: [PATCH] fix(Schematics): Don't add state import if not provided (#697) --- modules/schematics/src/container/index.spec.ts | 14 ++++++++++++++ modules/schematics/src/container/index.ts | 9 +++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/modules/schematics/src/container/index.spec.ts b/modules/schematics/src/container/index.spec.ts index 2ebd11665c..a5ec86b618 100644 --- a/modules/schematics/src/container/index.spec.ts +++ b/modules/schematics/src/container/index.spec.ts @@ -263,4 +263,18 @@ describe('Container Schematic', () => { /import { FooComponent } from '.\/foo\/foo.component/ ); }); + + it('should respect the state option if not provided', () => { + const options = { ...defaultOptions, state: undefined }; + const tree = schematicRunner.runSchematic('container', options, appTree); + const content = getFileContent(tree, '/src/app/foo/foo.component.ts'); + expect(content).not.toMatch(/import \* as fromStore/); + }); + + it('should import the state path if provided', () => { + const options = { ...defaultOptions, state: 'reducers' }; + const tree = schematicRunner.runSchematic('container', options, appTree); + const content = getFileContent(tree, '/src/app/foo/foo.component.ts'); + expect(content).toMatch(/import \* as fromStore from '..\/reducers';/); + }); }); diff --git a/modules/schematics/src/container/index.ts b/modules/schematics/src/container/index.ts index 09821057c3..3a3d894a39 100644 --- a/modules/schematics/src/container/index.ts +++ b/modules/schematics/src/container/index.ts @@ -123,13 +123,18 @@ export default function(options: ContainerOptions): Rule { options.path = options.path ? normalize(options.path) : options.path; options.module = findModuleFromOptions(host, options); - const statePath = `/${options.sourceDir}/${options.path}/${options.state}`; const componentPath = `/${options.sourceDir}/${options.path}/` + (options.flat ? '' : stringUtils.dasherize(options.name) + '/') + stringUtils.dasherize(options.name) + '.component'; - options.state = buildRelativePath(componentPath, statePath); + + if (options.state) { + const statePath = `/${options.sourceDir}/${options.path}/${ + options.state + }`; + options.state = buildRelativePath(componentPath, statePath); + } const templateSource = apply(url('./files'), [ options.spec ? noop() : filter(path => !path.endsWith('__spec.ts')),