Skip to content

Commit 6ae4723

Browse files
fix(effects): fix bugs in migration script to v18 (#4402)
1 parent f144f86 commit 6ae4723

File tree

2 files changed

+113
-6
lines changed

2 files changed

+113
-6
lines changed

modules/effects/migrations/18_0_0-beta/index.spec.ts

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
import { createWorkspace } from '@ngrx/schematics-core/testing';
66
import { tags } from '@angular-devkit/core';
77
import * as path from 'path';
8+
import { LogEntry } from '@angular-devkit/core/src/logger';
89

910
describe('Effects Migration to 18.0.0-beta', () => {
1011
const collectionPath = path.join(__dirname, '../migration.json');
@@ -121,7 +122,95 @@ export class SomeEffects {
121122
});
122123
});
123124

124-
it('should add if they are missing', async () => {
125+
it('should work with prior import from same namespace', async () => {
126+
const input = tags.stripIndent`
127+
import { Actions } from '@ngrx/effects';
128+
import { concatLatestFrom, createEffect, ofType } from '@ngrx/effects';
129+
130+
class SomeEffects {}
131+
`;
132+
const output = tags.stripIndent`
133+
import { Actions } from '@ngrx/effects';
134+
import { createEffect, ofType } from '@ngrx/effects';
135+
import { concatLatestFrom } from '@ngrx/operators';
136+
137+
class SomeEffects {}
138+
`;
139+
await verifySchematic(input, output);
140+
});
141+
142+
it('should operate on multiple files', async () => {
143+
const inputMainOne = tags.stripIndent`
144+
import { Actions, concatLatestFrom, createEffect, ofType } from '@ngrx/effects';
145+
import { tap } from 'rxjs/operators';
146+
147+
class SomeEffects {}
148+
`;
149+
150+
const outputMainOne = tags.stripIndent`
151+
import { Actions, createEffect, ofType } from '@ngrx/effects';
152+
import { concatLatestFrom } from '@ngrx/operators';
153+
import { tap } from 'rxjs/operators';
154+
155+
class SomeEffects {}
156+
`;
157+
158+
const inputMainTwo = tags.stripIndent`
159+
import { provideEffects } from '@ngrx/effects';
160+
import { Actions, concatLatestFrom, createEffect, ofType } from '@ngrx/effects';
161+
162+
class SomeEffects {}
163+
`;
164+
165+
const outputMainTwo = tags.stripIndent`
166+
import { provideEffects } from '@ngrx/effects';
167+
import { Actions, createEffect, ofType } from '@ngrx/effects';
168+
import { concatLatestFrom } from '@ngrx/operators';
169+
170+
class SomeEffects {}
171+
`;
172+
appTree.create('mainOne.ts', inputMainOne);
173+
appTree.create('mainTwo.ts', inputMainTwo);
174+
175+
const tree = await schematicRunner.runSchematic(
176+
`ngrx-effects-migration-18-beta`,
177+
{},
178+
appTree
179+
);
180+
181+
const actualMainOne = tree.readContent('mainOne.ts');
182+
const actualMainTwo = tree.readContent('mainTwo.ts');
183+
184+
expect(actualMainOne).toBe(outputMainOne);
185+
expect(actualMainTwo).toBe(outputMainTwo);
186+
});
187+
188+
it('should report an info on multiple imports of concatLatestFrom', async () => {
189+
const input = tags.stripIndent`
190+
import { concatLatestFrom } from '@ngrx/effects';
191+
import { concatLatestFrom, createEffect, ofType } from '@ngrx/effects';
192+
193+
class SomeEffects {}
194+
`;
195+
196+
appTree.create('main.ts', input);
197+
const logEntries: LogEntry[] = [];
198+
schematicRunner.logger.subscribe((logEntry) => logEntries.push(logEntry));
199+
await schematicRunner.runSchematic(
200+
`ngrx-effects-migration-18-beta`,
201+
{},
202+
appTree
203+
);
204+
205+
expect(logEntries).toHaveLength(1);
206+
expect(logEntries[0]).toMatchObject({
207+
message:
208+
'[@ngrx/effects] Skipping because of multiple `concatLatestFrom` imports',
209+
level: 'info',
210+
});
211+
});
212+
213+
it('should add @ngrx/operators if they are missing', async () => {
125214
const originalPackageJson = JSON.parse(
126215
appTree.readContent('/package.json')
127216
);

modules/effects/migrations/18_0_0-beta/index.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,43 @@ import { createRemoveChange } from '../../schematics-core/utility/change';
1717

1818
export function migrateConcatLatestFromImport(): Rule {
1919
return (tree: Tree, ctx: SchematicContext) => {
20-
const changes: Change[] = [];
2120
addPackageToPackageJson(tree, 'dependencies', '@ngrx/operators', '^18.0.0');
2221

2322
visitTSSourceFiles(tree, (sourceFile) => {
2423
const importDeclarations = new Array<ts.ImportDeclaration>();
24+
2525
getImportDeclarations(sourceFile, importDeclarations);
2626

27-
const effectsImportsAndDeclaration = importDeclarations
27+
const effectsImportsAndDeclarations = importDeclarations
2828
.map((effectsImportDeclaration) => {
2929
const effectsImports = getEffectsNamedBinding(
3030
effectsImportDeclaration
3131
);
3232
if (effectsImports) {
33-
return { effectsImports, effectsImportDeclaration };
33+
if (
34+
effectsImports.elements.some(
35+
(element) => element.name.getText() === 'concatLatestFrom'
36+
)
37+
) {
38+
return { effectsImports, effectsImportDeclaration };
39+
}
40+
return undefined;
3441
} else {
3542
return undefined;
3643
}
3744
})
38-
.find(Boolean);
45+
.filter(Boolean);
46+
47+
if (effectsImportsAndDeclarations.length === 0) {
48+
return;
49+
} else if (effectsImportsAndDeclarations.length > 1) {
50+
ctx.logger.info(
51+
'[@ngrx/effects] Skipping because of multiple `concatLatestFrom` imports'
52+
);
53+
return;
54+
}
3955

56+
const [effectsImportsAndDeclaration] = effectsImportsAndDeclarations;
4057
if (!effectsImportsAndDeclaration) {
4158
return;
4259
}
@@ -53,6 +70,7 @@ export function migrateConcatLatestFromImport(): Rule {
5370
.map((element) => element.name.getText())
5471
.join(', ');
5572

73+
const changes: Change[] = [];
5674
// Remove `concatLatestFrom` from @ngrx/effects and leave the other imports
5775
if (otherEffectsImports) {
5876
changes.push(
@@ -107,7 +125,7 @@ export function migrateConcatLatestFromImport(): Rule {
107125
new InsertChange(
108126
sourceFile.fileName,
109127
effectsImportDeclaration.getEnd() + 1,
110-
`${newOperatorsImport}\n`
128+
`${newOperatorsImport}\n` // not os-independent for snapshot tests
111129
)
112130
);
113131
}

0 commit comments

Comments
 (0)