Skip to content

Commit

Permalink
fix(Schematics): Distinguish between root and feature effect arrays w…
Browse files Browse the repository at this point in the history
…hen registering
  • Loading branch information
brandonroberts committed Jan 13, 2018
1 parent f1082fe commit 7a2e721
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
31 changes: 28 additions & 3 deletions modules/schematics/src/effect/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ describe('Effect Schematic', () => {

it('should add an effect to the empty array of registered effects', () => {
const storeModule = '/src/app/store.module.ts';
const options = { ...defaultOptions, module: 'store.module.ts' };
const options = {
...defaultOptions,
root: true,
module: 'store.module.ts',
};
appTree = createAppModuleWithEffects(
appTree,
storeModule,
Expand All @@ -117,9 +121,13 @@ describe('Effect Schematic', () => {
expect(content).toMatch(/EffectsModule\.forRoot\(\[FooEffects\]\)/);
});

it('should add an effect to the existing registered effects', () => {
it('should add an effect to the existing registered root effects', () => {
const storeModule = '/src/app/store.module.ts';
const options = { ...defaultOptions, module: 'store.module.ts' };
const options = {
...defaultOptions,
root: true,
module: 'store.module.ts',
};
appTree = createAppModuleWithEffects(
appTree,
storeModule,
Expand All @@ -134,6 +142,23 @@ describe('Effect Schematic', () => {
);
});

it('should add an effect to the existing registered feature effects', () => {
const storeModule = '/src/app/store.module.ts';
const options = { ...defaultOptions, module: 'store.module.ts' };
appTree = createAppModuleWithEffects(
appTree,
storeModule,
`EffectsModule.forRoot([RootEffects])\n EffectsModule.forFeature([UserEffects])`
);

const tree = schematicRunner.runSchematic('effect', options, appTree);
const content = getFileContent(tree, '/src/app/store.module.ts');

expect(content).toMatch(
/EffectsModule\.forFeature\(\[UserEffects, FooEffects\]\)/
);
});

it('should not add an effect to registered effects defined with a variable', () => {
const storeModule = '/src/app/store.module.ts';
const options = { ...defaultOptions, module: 'store.module.ts' };
Expand Down
8 changes: 6 additions & 2 deletions modules/schematics/src/utility/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,12 @@ function _addSymbolToNgModuleMetadata(
return [];
}

const effectsModule = nodeArray.find(node =>
node.getText().includes('EffectsModule')
const effectsModule = nodeArray.find(
node =>
(node.getText().includes('EffectsModule.forRoot') &&
symbolName.includes('EffectsModule.forRoot')) ||
(node.getText().includes('EffectsModule.forFeature') &&
symbolName.includes('EffectsModule.forFeature'))
);

if (effectsModule && symbolName.includes('EffectsModule')) {
Expand Down

0 comments on commit 7a2e721

Please sign in to comment.