From 1f251ad257ccf3f550aa9adbc53b58b5c20c98ad Mon Sep 17 00:00:00 2001 From: EV Date: Fri, 29 Mar 2019 22:35:42 +0100 Subject: [PATCH] fix(schematics): avoid lastIndexOf error while creating store schematics (#1659) --- modules/schematics/src/store/index.spec.ts | 27 ++++++++++++++++++++++ modules/schematics/src/store/index.ts | 6 ++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/modules/schematics/src/store/index.spec.ts b/modules/schematics/src/store/index.spec.ts index 741ae04756..5d8295ffe0 100644 --- a/modules/schematics/src/store/index.spec.ts +++ b/modules/schematics/src/store/index.spec.ts @@ -215,4 +215,31 @@ describe('Store Schematic', () => { ); expect(content).toMatch(/export interface FeatureState {/); }); + + it('should fail if a feature state name is not specified', () => { + const options = { + ...defaultOptions, + name: undefined, + root: false, + }; + + let thrownError: Error | null = null; + try { + schematicRunner.runSchematic('store', options, appTree); + } catch (err) { + thrownError = err; + } + expect(thrownError).toBeDefined(); + }); + + it('should pass if a root state name is not specified', () => { + const options = { + ...defaultOptions, + name: undefined, + }; + + expect(function() { + schematicRunner.runSchematic('store', options, appTree); + }).not.toThrow(); + }); }); diff --git a/modules/schematics/src/store/index.ts b/modules/schematics/src/store/index.ts index db06f31df2..51c67a4935 100644 --- a/modules/schematics/src/store/index.ts +++ b/modules/schematics/src/store/index.ts @@ -131,9 +131,13 @@ function addImportToNgModule(options: StoreOptions): Rule { export default function(options: StoreOptions): Rule { return (host: Tree, context: SchematicContext) => { + if (!options.name && !options.root) { + throw new Error(`Please provide a name for the feature state`); + } + options.path = getProjectPath(host, options); - const parsedPath = parseName(options.path, options.name); + const parsedPath = parseName(options.path, options.name || ''); options.name = parsedPath.name; options.path = parsedPath.path;