Skip to content

Commit 95ff6c8

Browse files
brandonrobertsMikeRyanDev
authored andcommitted
fix(Schematics): Distinguish between root and feature effect arrays when registering (#718)
1 parent f1082fe commit 95ff6c8

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

modules/schematics/src/effect/index.spec.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ describe('Effect Schematic', () => {
104104

105105
it('should add an effect to the empty array of registered effects', () => {
106106
const storeModule = '/src/app/store.module.ts';
107-
const options = { ...defaultOptions, module: 'store.module.ts' };
107+
const options = {
108+
...defaultOptions,
109+
root: true,
110+
module: 'store.module.ts',
111+
};
108112
appTree = createAppModuleWithEffects(
109113
appTree,
110114
storeModule,
@@ -117,9 +121,13 @@ describe('Effect Schematic', () => {
117121
expect(content).toMatch(/EffectsModule\.forRoot\(\[FooEffects\]\)/);
118122
});
119123

120-
it('should add an effect to the existing registered effects', () => {
124+
it('should add an effect to the existing registered root effects', () => {
121125
const storeModule = '/src/app/store.module.ts';
122-
const options = { ...defaultOptions, module: 'store.module.ts' };
126+
const options = {
127+
...defaultOptions,
128+
root: true,
129+
module: 'store.module.ts',
130+
};
123131
appTree = createAppModuleWithEffects(
124132
appTree,
125133
storeModule,
@@ -134,6 +142,23 @@ describe('Effect Schematic', () => {
134142
);
135143
});
136144

145+
it('should add an effect to the existing registered feature effects', () => {
146+
const storeModule = '/src/app/store.module.ts';
147+
const options = { ...defaultOptions, module: 'store.module.ts' };
148+
appTree = createAppModuleWithEffects(
149+
appTree,
150+
storeModule,
151+
`EffectsModule.forRoot([RootEffects])\n EffectsModule.forFeature([UserEffects])`
152+
);
153+
154+
const tree = schematicRunner.runSchematic('effect', options, appTree);
155+
const content = getFileContent(tree, '/src/app/store.module.ts');
156+
157+
expect(content).toMatch(
158+
/EffectsModule\.forFeature\(\[UserEffects, FooEffects\]\)/
159+
);
160+
});
161+
137162
it('should not add an effect to registered effects defined with a variable', () => {
138163
const storeModule = '/src/app/store.module.ts';
139164
const options = { ...defaultOptions, module: 'store.module.ts' };

modules/schematics/src/utility/ast-utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,12 @@ function _addSymbolToNgModuleMetadata(
352352
return [];
353353
}
354354

355-
const effectsModule = nodeArray.find(node =>
356-
node.getText().includes('EffectsModule')
355+
const effectsModule = nodeArray.find(
356+
node =>
357+
(node.getText().includes('EffectsModule.forRoot') &&
358+
symbolName.includes('EffectsModule.forRoot')) ||
359+
(node.getText().includes('EffectsModule.forFeature') &&
360+
symbolName.includes('EffectsModule.forFeature'))
357361
);
358362

359363
if (effectsModule && symbolName.includes('EffectsModule')) {

0 commit comments

Comments
 (0)