Skip to content

Commit

Permalink
fixup! refactor: reorganize core-module
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi committed May 4, 2020
1 parent 2e4d554 commit b6421bf
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 108 deletions.
3 changes: 1 addition & 2 deletions schematics/src/store-group/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ const registration_1 = require("../utils/registration");
function determineStoreGroupLocation(host, options) {
const project = project_1.getProject(host, options.project);
const path = core_1.normalize(`${project_1.buildDefaultPath(project)}/core/store/`);
const module = core_1.normalize(`${path}/core-store.module.ts`);
const module = core_1.normalize(`${project_1.buildDefaultPath(project)}/core/state-management.module.ts`);
const artifactName = `${core_1.strings.classify(options.name)}StoreModule`;
const moduleImportPath = core_1.normalize(`${path}/${core_1.strings.dasherize(options.name)}/${core_1.strings.dasherize(options.name)}-store.module`);
return Object.assign({}, options, { path,
module,
artifactName,
moduleImportPath });
}
exports.determineStoreGroupLocation = determineStoreGroupLocation;
function createStoreGroup(options) {
return host => {
if (!options.project) {
Expand Down
4 changes: 2 additions & 2 deletions schematics/src/store-group/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { addImportToNgModule } from '../utils/registration';

import { PwaStoreGroupOptionsSchema as Options } from './schema';

export function determineStoreGroupLocation(
function determineStoreGroupLocation(
host: Tree,
options: {
path?: string;
Expand All @@ -28,7 +28,7 @@ export function determineStoreGroupLocation(
const project = getProject(host, options.project);

const path = normalize(`${buildDefaultPath(project)}/core/store/`);
const module = normalize(`${path}/core-store.module.ts`);
const module = normalize(`${buildDefaultPath(project)}/core/state-management.module.ts`);
const artifactName = `${strings.classify(options.name)}StoreModule`;
const moduleImportPath = normalize(
`${path}/${strings.dasherize(options.name)}/${strings.dasherize(options.name)}-store.module`
Expand Down
51 changes: 9 additions & 42 deletions schematics/src/store-group/factory_spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UnitTestTree } from '@angular-devkit/schematics/testing';

import { createApplication, createSchematicRunner } from '../utils/testHelper';
import { copyFileFromPWA, createApplication, createSchematicRunner } from '../utils/testHelper';

import { PwaStoreGroupOptionsSchema as Options } from './schema';

Expand All @@ -13,45 +13,12 @@ describe('Store Group Schematic', () => {

let appTree: UnitTestTree;
beforeEach(async () => {
appTree = await createApplication(schematicRunner).toPromise();
appTree.create(
'/projects/bar/src/app/core/store/core-store.module.ts',
`import { NgModule } from '@angular/core';
import { EffectsModule } from '@ngrx/effects';
import { ActionReducerMap, StoreModule } from '@ngrx/store';
import { CoreState } from './core-store';
import { CountriesEffects } from './countries/countries.effects';
import { countriesReducer } from './countries/countries.reducer';
export const coreReducers: ActionReducerMap<CoreState> = {countries: countriesReducer};
export const coreEffects = [
CountriesEffects,
];
@NgModule({
imports: [
EffectsModule.forRoot(coreEffects),
StoreModule.forRoot(coreReducers),
],
})
export class CoreStoreModule {}
`
);
appTree.create(
'/projects/bar/src/app/core/store/core-store.ts',
`import { Selector } from '@ngrx/store';
import { CountriesState } from './countries/countries.reducer';
export interface CoreState {
countries: CountriesState;
}
export const getCoreState: Selector<CoreState, CoreState> = state => state;
`
);
appTree = await createApplication(schematicRunner)
.pipe(
copyFileFromPWA('src/app/core/state-management.module.ts'),
copyFileFromPWA('src/app/core/store/core/core-store.ts')
)
.toPromise();
});

it('should create a store-group in core store by default', async () => {
Expand All @@ -67,11 +34,11 @@ Array [
`);
});

it('should register a store group in core store by default', async () => {
it('should register a store group module in state management by default', async () => {
const options = { ...defaultOptions };

const tree = await schematicRunner.runSchematicAsync('store-group', options, appTree).toPromise();
const storeModuleContent = tree.readContent('/projects/bar/src/app/core/store/core-store.module.ts');
const storeModuleContent = tree.readContent('/projects/bar/src/app/core/state-management.module.ts');
expect(storeModuleContent).toContain('import { FooStoreModule }');
});
});
5 changes: 4 additions & 1 deletion schematics/src/store/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ function determineStoreLocation(host, options) {
const pathFragments = nameWOStore.split('/');
feature = pathFragments[pathFragments.length - 2];
}
else {
feature = 'core';
}
}
let parent;
let path = options.path;
if (!extension && !feature) {
path = `${project.sourceRoot}/app/core/store/`;
path = `${project.sourceRoot}/app/core/store/core/`;
parent = 'core';
}
else if (!extension && feature) {
Expand Down
4 changes: 3 additions & 1 deletion schematics/src/store/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ export function determineStoreLocation(
if (nameWOStore.includes('/')) {
const pathFragments = nameWOStore.split('/');
feature = pathFragments[pathFragments.length - 2];
} else {
feature = 'core';
}
}

let parent: string;

let path = options.path;
if (!extension && !feature) {
path = `${project.sourceRoot}/app/core/store/`;
path = `${project.sourceRoot}/app/core/store/core/`;
parent = 'core';
} else if (!extension && feature) {
path = `${project.sourceRoot}/app/core/store/${feature}/`;
Expand Down
75 changes: 19 additions & 56 deletions schematics/src/store/factory_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { UnitTestTree } from '@angular-devkit/schematics/testing';
import { mergeMap } from 'rxjs/operators';

import {
copyFileFromPWA,
createAppLastRoutingModule,
createApplication,
createModule,
Expand All @@ -25,51 +26,13 @@ describe('Store Schematic', () => {
createModule(schematicRunner, { name: 'shared' }),
createModule(schematicRunner, { name: 'shell' }),
createAppLastRoutingModule(schematicRunner),
mergeMap(tree => schematicRunner.runSchematicAsync('extension', { name: 'feature', project: 'bar' }, tree))
mergeMap(tree => schematicRunner.runSchematicAsync('extension', { name: 'feature', project: 'bar' }, tree)),
copyFileFromPWA('src/app/core/store/core/core-store.module.ts'),
copyFileFromPWA('src/app/core/store/core/core-store.ts'),
copyFileFromPWA('src/app/core/state-management.module.ts'),
mergeMap(tree => schematicRunner.runSchematicAsync('store-group', { ...defaultOptions, name: 'bar' }, tree))
)
.toPromise();

appTree.create(
'/projects/bar/src/app/core/store/core-store.module.ts',
`import { NgModule } from '@angular/core';
import { EffectsModule } from '@ngrx/effects';
import { ActionReducerMap, StoreModule } from '@ngrx/store';
import { CoreState } from './core-store';
import { CountriesEffects } from './countries/countries.effects';
import { countriesReducer } from './countries/countries.reducer';
export const coreReducers: ActionReducerMap<CoreState> = {countries: countriesReducer};
export const coreEffects = [
CountriesEffects,
];
@NgModule({
imports: [
EffectsModule.forRoot(coreEffects),
StoreModule.forRoot(coreReducers),
],
})
export class CoreStoreModule {}
`
);
appTree.create(
'/projects/bar/src/app/core/store/core-store.ts',
`import { Selector } from '@ngrx/store';
import { CountriesState } from './countries/countries.reducer';
export interface CoreState {
countries: CountriesState;
}
export const getCoreState: Selector<CoreState, CoreState> = state => state;
`
);
appTree = await schematicRunner
.runSchematicAsync('store-group', { ...defaultOptions, name: 'bar' }, appTree)
.toPromise();
});

it('should create a store in core store by default', async () => {
Expand All @@ -79,13 +42,13 @@ export const getCoreState: Selector<CoreState, CoreState> = state => state;
const files = tree.files.filter(x => x.search('foo') >= 0);
expect(files).toMatchInlineSnapshot(`
Array [
"/projects/bar/src/app/core/store/foo/foo.actions.ts",
"/projects/bar/src/app/core/store/foo/foo.effects.ts",
"/projects/bar/src/app/core/store/foo/foo.effects.spec.ts",
"/projects/bar/src/app/core/store/foo/foo.reducer.ts",
"/projects/bar/src/app/core/store/foo/foo.selectors.ts",
"/projects/bar/src/app/core/store/foo/foo.selectors.spec.ts",
"/projects/bar/src/app/core/store/foo/index.ts",
"/projects/bar/src/app/core/store/core/foo/foo.actions.ts",
"/projects/bar/src/app/core/store/core/foo/foo.effects.ts",
"/projects/bar/src/app/core/store/core/foo/foo.effects.spec.ts",
"/projects/bar/src/app/core/store/core/foo/foo.reducer.ts",
"/projects/bar/src/app/core/store/core/foo/foo.selectors.ts",
"/projects/bar/src/app/core/store/core/foo/foo.selectors.spec.ts",
"/projects/bar/src/app/core/store/core/foo/index.ts",
]
`);
});
Expand All @@ -94,10 +57,10 @@ export const getCoreState: Selector<CoreState, CoreState> = state => state;
const options = { ...defaultOptions };

const tree = await schematicRunner.runSchematicAsync('store', options, appTree).toPromise();
const storeContent = tree.readContent('/projects/bar/src/app/core/store/core-store.ts');
const storeContent = tree.readContent('/projects/bar/src/app/core/store/core/core-store.ts');
expect(storeContent).toContain('import { FooState }');
expect(storeContent).toContain('foo: FooState');
const storeModuleContent = tree.readContent('/projects/bar/src/app/core/store/core-store.module.ts');
const storeModuleContent = tree.readContent('/projects/bar/src/app/core/store/core/core-store.module.ts');
expect(storeModuleContent).toContain('import { fooReducer }');
expect(storeModuleContent).toContain('foo: fooReducer');
expect(storeModuleContent).toContain('import { FooEffects }');
Expand Down Expand Up @@ -183,16 +146,16 @@ export const getCoreState: Selector<CoreState, CoreState> = state => state;
it('should handle simple stores', () => {
const config = {
extension: undefined,
feature: undefined,
feature: 'core',
name: 'foobar',
parent: 'core',
parentStorePath: 'projects/bar/src/app/core/store/core',
path: 'projects/bar/src/app/core/store/',
parentStorePath: 'projects/bar/src/app/core/store/core/core',
path: 'projects/bar/src/app/core/store/core/',
project: 'bar',
};

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

it('should handle feature stores', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TestBed } from '@angular/core/testing';

import { CoreStoreModule } from 'ish-core/store/core-store.module';
import { CoreStoreModule } from 'ish-core/store/core/core-store.module';
import { StoreWithSnapshots, provideStoreSnapshots } from 'ish-core/utils/dev/ngrx-testing';
<% if (feature) { %>import { <%= classify(feature) %>StoreModule } from 'ish-core/store/<%= dasherize(feature) %>/<%= dasherize(feature) %>-store.module';<% } else if (extension) { %>import { <%= classify(extension) %>StoreModule } from '../<%= dasherize(extension) %>-store.module';<% } %>
<% if(entity) { %>import { HttpError } from 'ish-core/models/http-error/http-error.model';
Expand All @@ -15,8 +15,8 @@ describe('<%= classify(name) %> Selectors', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
CoreStoreModule.forTesting(<% if (!feature && !extension) { %>['<%= camelize(name) %>']<% } %>)
<% if (feature) { %>, <%= classify(feature) %>StoreModule.forTesting('<%= camelize(name) %>')<% } else if (extension) { %>, <%= classify(extension) %>StoreModule.forTesting('<%= camelize(name) %>')<% } %>
CoreStoreModule.forTesting(<% if (feature === 'core') { %>['<%= camelize(name) %>']<% } %>)
<% if (feature && feature !== 'core') { %>, <%= classify(feature) %>StoreModule.forTesting('<%= camelize(name) %>')<% } else if (extension) { %>, <%= classify(extension) %>StoreModule.forTesting('<%= camelize(name) %>')<% } %>
],
providers: [provideStoreSnapshots()],
});
Expand Down
7 changes: 7 additions & 0 deletions schematics/src/utils/testHelper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const testing_1 = require("@angular-devkit/schematics/testing");
const fs_1 = require("fs");
const operators_1 = require("rxjs/operators");
function createSchematicRunner() {
return new testing_1.SchematicTestRunner('intershop-schematics', require.resolve('../collection.json'));
Expand Down Expand Up @@ -29,6 +30,12 @@ function createModule(schematicRunner, options) {
return (source$) => source$.pipe(operators_1.switchMap(tree => schematicRunner.runSchematicAsync('module', Object.assign({}, options, { project: 'bar' }), tree)));
}
exports.createModule = createModule;
function copyFileFromPWA(path) {
return (source$) => source$.pipe(operators_1.tap(tree => {
tree.create(`/projects/bar/${path}`, fs_1.readFileSync(`../${path}`));
}));
}
exports.copyFileFromPWA = copyFileFromPWA;
function createAppLastRoutingModule(schematicRunner) {
return (source$) => source$.pipe(operators_1.switchMap(tree => schematicRunner.runExternalSchematicAsync('@schematics/angular', 'module', {
name: 'pages/app-last-routing',
Expand Down
12 changes: 11 additions & 1 deletion schematics/src/utils/testHelper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { Schema as ModuleOptions } from '@schematics/angular/module/schema';
import { readFileSync } from 'fs';
import { Observable, OperatorFunction } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { switchMap, tap } from 'rxjs/operators';

export function createSchematicRunner() {
return new SchematicTestRunner('intershop-schematics', require.resolve('../collection.json'));
Expand Down Expand Up @@ -43,6 +44,15 @@ export function createModule(
source$.pipe(switchMap(tree => schematicRunner.runSchematicAsync('module', { ...options, project: 'bar' }, tree)));
}

export function copyFileFromPWA(path: string): OperatorFunction<UnitTestTree, UnitTestTree> {
return (source$: Observable<UnitTestTree>) =>
source$.pipe(
tap(tree => {
tree.create(`/projects/bar/${path}`, readFileSync(`../${path}`));
})
);
}

export function createAppLastRoutingModule(schematicRunner: SchematicTestRunner) {
return (source$: Observable<UnitTestTree>) =>
source$.pipe(
Expand Down

0 comments on commit b6421bf

Please sign in to comment.