Skip to content

Commit

Permalink
fix(schematics): add comma before devtools for empty imports
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Jun 11, 2020
1 parent de02aa7 commit a385a78
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
36 changes: 27 additions & 9 deletions modules/schematics/src/store/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ describe('Store Schematic', () => {
expect(content).not.toMatch(
/import { reducers, metaReducers } from '\.\/reducers';/
);
expect(content).toMatch(/StoreModule.forRoot\({}/);

expect(content).toMatch(/StoreModule.forRoot\({}\),/);
expect(files.indexOf(`${projectPath}/src/app/reducers/index.ts`)).toBe(-1);
});

Expand Down Expand Up @@ -301,13 +300,32 @@ describe('Store Schematic', () => {
expect(content).not.toMatch(/fooFeatureKey = 'foo'/);
});

it('should add store runtime checks', () => {
const options = { ...defaultOptions, module: 'app.module.ts' };
it('should add the initial config correctly into an empty module', async () => {
const options = {
...defaultOptions,
module: 'empty.module.ts',
};

const tree = schematicRunner.runSchematic('store', options, appTree);
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
expect(content).toMatch(/runtimeChecks: {/);
expect(content).toMatch(/strictStateImmutability: true,/);
expect(content).toMatch(/strictActionImmutability: true/);
appTree.create(
`${projectPath}/src/app/empty.module.ts`,
`
import { NgModule } from '@angular/core';
@NgModule({
declarations: [],
imports: [],
})
export class EmptyModule {}
`
);

const tree = await schematicRunner
.runSchematicAsync('store', options, appTree)
.toPromise();
const content = tree.readContent(`${projectPath}/src/app/empty.module.ts`);

expect(content).toMatch(
/imports: \[StoreModule.forRoot\(reducers, { metaReducers }\), !environment.production \? StoreDevtoolsModule.instrument\(\) : \[\]\]/
);
});
});
28 changes: 14 additions & 14 deletions modules/schematics/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
findModuleFromOptions,
addImportToModule,
parseName,
visitNgModuleImports,
} from '@ngrx/schematics/schematics-core';
import { Schema as StoreOptions } from './schema';

Expand Down Expand Up @@ -63,25 +64,14 @@ function addImportToNgModule(options: StoreOptions): Rule {
`${options.path}/environments/environment`
);

const runtimeChecks = `
runtimeChecks: {
strictStateImmutability: true,
strictActionImmutability: true,
}
`;

const rootStoreReducers = options.minimal ? `{}` : `reducers`;

const rootStoreConfig = options.minimal
? `{ ${runtimeChecks} }`
: `{
metaReducers, ${runtimeChecks} }`;
const rootStoreConfig = options.minimal ? `` : `, { metaReducers }`;

const storeNgModuleImport = addImportToModule(
source,
modulePath,
options.root
? `StoreModule.forRoot(${rootStoreReducers}, ${rootStoreConfig})`
? `StoreModule.forRoot(${rootStoreReducers}${rootStoreConfig})`
: `StoreModule.forFeature(from${stringUtils.classify(
options.name
)}.${stringUtils.camelize(
Expand Down Expand Up @@ -123,10 +113,20 @@ function addImportToNgModule(options: StoreOptions): Rule {
let rootImports: (Change | undefined)[] = [];

if (options.root) {
let hasImports = false;
visitNgModuleImports(source, (_, importNodes) => {
hasImports = importNodes.length > 0;
});

// `addImportToModule` adds a comma to imports when there are already imports present
// because at this time the store import hasn't been committed yet, `addImportToModule` wont add a comma
// so we have to add it here for empty import arrays
let adjectiveComma = hasImports ? '' : ', ';

const storeDevtoolsNgModuleImport = addImportToModule(
source,
modulePath,
`!environment.production ? StoreDevtoolsModule.instrument() : []`,
`${adjectiveComma}!environment.production ? StoreDevtoolsModule.instrument() : []`,
relativePath
).shift();

Expand Down

0 comments on commit a385a78

Please sign in to comment.