Skip to content

Commit

Permalink
fix(schematics): make store location selection more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi committed Feb 28, 2020
1 parent c02d4d2 commit c0ab56c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 38 deletions.
2 changes: 1 addition & 1 deletion e2e/test-schematics.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ npx ng g store-group training
stat src/app/core/store/training/training-store.ts
grep "TrainingStoreModule" src/app/core/store/core-store.module.ts

npx ng g store core/store/training/warehouses --entity warehouse
npx ng g store training/warehouses --entity warehouse
stat src/app/core/store/training/warehouses/warehouses.actions.ts
stat src/app/core/store/training/warehouses/warehouses.effects.ts
stat src/app/core/store/training/warehouses/warehouses.reducer.ts
Expand Down
12 changes: 7 additions & 5 deletions schematics/src/store/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ function determineStoreLocation(host, options) {
extension = requestDestination.match(regex)[1];
}
let feature = options.feature;
const regex2 = /store\/([a-z][a-z0-9-]+)\//;
const requestDestination2 = core_1.normalize(`${options.path}/${options.name}`);
if (regex2.test(requestDestination2)) {
feature = requestDestination2.match(regex2)[1];
if (!extension && !feature) {
const nameWOStore = options.name.replace(/.*store\//, '');
if (nameWOStore.includes('/')) {
const pathFragments = nameWOStore.split('/');
feature = pathFragments[pathFragments.length - 2];
}
}
let parent;
let path = options.path;
Expand All @@ -39,7 +41,7 @@ function determineStoreLocation(host, options) {
else {
throw new Error('cannot add feature store in extension');
}
return Object.assign({}, options, { parentStorePath: `${path}${parent}`, extension,
return Object.assign({}, options, { parentStorePath: `${path}${parent}`, name: options.name.split('/').pop(), extension,
feature,
path,
parent });
Expand Down
11 changes: 7 additions & 4 deletions schematics/src/store/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ export function determineStoreLocation(
}

let feature = options.feature;
const regex2 = /store\/([a-z][a-z0-9-]+)\//;
const requestDestination2 = normalize(`${options.path}/${options.name}`);
if (regex2.test(requestDestination2)) {
feature = requestDestination2.match(regex2)[1];
if (!extension && !feature) {
const nameWOStore = options.name.replace(/.*store\//, '');
if (nameWOStore.includes('/')) {
const pathFragments = nameWOStore.split('/');
feature = pathFragments[pathFragments.length - 2];
}
}

let parent: string;
Expand All @@ -66,6 +68,7 @@ export function determineStoreLocation(
return {
...options,
parentStorePath: `${path}${parent}`,
name: options.name.split('/').pop(),
extension,
feature,
path,
Expand Down
80 changes: 52 additions & 28 deletions schematics/src/store/factory_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
createSchematicRunner,
} from '../utils/testHelper';

import { determineStoreLocation } from './factory';
import { PwaStoreOptionsSchema as Options } from './schema';

describe('Store Schematic', () => {
Expand Down Expand Up @@ -67,34 +68,9 @@ export interface CoreState {
export const getCoreState: Selector<CoreState, CoreState> = state => state;
`
);
appTree.create(
'/projects/bar/src/app/core/store/bar/bar-store.module.ts',
`import { NgModule } from '@angular/core';
import { EffectsModule } from '@ngrx/effects';
import { ActionReducerMap, StoreModule } from '@ngrx/store';
import { BarState } from './bar-store';
export const barReducers: ActionReducerMap<BarState> = {};
export const barEffects = [];
@NgModule({
imports: [
EffectsModule.forRoot(barEffects),
StoreModule.forRoot(barReducers),
],
})
export class BarStoreModule {}
`
);
appTree.create(
'/projects/bar/src/app/core/store/bar/bar-store.ts',
`import { Selector } from '@ngrx/store';
export interface BarState {}
`
);
appTree = await schematicRunner
.runSchematicAsync('store-group', { ...defaultOptions, name: 'bar' }, appTree)
.toPromise();
});

it('should create a store in core store by default', async () => {
Expand Down Expand Up @@ -203,4 +179,52 @@ export interface BarState {}
done();
});
});

describe('determineStoreLocation', () => {
it('should handle simple stores', () => {
const config = {
extension: undefined,
feature: undefined,
name: 'foobar',
parent: 'core',
parentStorePath: 'projects/bar/src/app/core/store/core',
path: 'projects/bar/src/app/core/store/',
project: 'bar',
};

expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'foobar' })).toEqual(config);
expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'core/store/foobar' })).toEqual(config);
});

it('should handle feature stores', () => {
const config = {
extension: undefined,
feature: 'bar',
name: 'foobar',
parent: 'bar',
parentStorePath: 'projects/bar/src/app/core/store/bar/bar',
path: 'projects/bar/src/app/core/store/bar/',
project: 'bar',
};

expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'foobar', feature: 'bar' })).toEqual(config);
expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'bar/foobar' })).toEqual(config);
expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'core/store/bar/foobar' })).toEqual(config);
});

it('should handle extension stores', () => {
const config = {
extension: 'bar',
feature: undefined,
name: 'foobar',
parent: 'bar',
parentStorePath: 'projects/bar/src/app/extensions/bar/store/bar',
path: 'projects/bar/src/app/extensions/bar/store/',
project: 'bar',
};

expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'foobar', extension: 'bar' })).toEqual(config);
expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'extensions/bar/foobar' })).toEqual(config);
});
});
});

0 comments on commit c0ab56c

Please sign in to comment.