Skip to content

Commit 73fda59

Browse files
feat(store-devtools): add migration for connectInZone (#4106)
1 parent d3b4db0 commit 73fda59

File tree

3 files changed

+539
-0
lines changed

3 files changed

+539
-0
lines changed
Lines changed: 368 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,368 @@
1+
import { Tree } from '@angular-devkit/schematics';
2+
import {
3+
SchematicTestRunner,
4+
UnitTestTree,
5+
} from '@angular-devkit/schematics/testing';
6+
import * as path from 'path';
7+
import { createPackageJson } from '@ngrx/schematics-core/testing/create-package';
8+
import { waitForAsync } from '@angular/core/testing';
9+
10+
describe('DevTools Migration 17_0_0-beta', () => {
11+
let appTree: UnitTestTree;
12+
const collectionPath = path.join(__dirname, '../migration.json');
13+
const pkgName = 'store-devtools';
14+
const migrationname = `ngrx-${pkgName}-migration-17-0-0-beta`;
15+
16+
beforeEach(() => {
17+
appTree = new UnitTestTree(Tree.empty());
18+
appTree.create(
19+
'/tsconfig.json',
20+
`
21+
{
22+
"include": [**./*.ts"]
23+
}
24+
`
25+
);
26+
createPackageJson('', pkgName, appTree);
27+
});
28+
29+
describe('StoreDevtoolsModule.instrument', () => {
30+
it(`should add connectInZone to config properties (previous property ends with a comma)`, waitForAsync(async () => {
31+
const input = `
32+
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
33+
34+
@NgModule({
35+
imports: [
36+
StoreDevtoolsModule.instrument({
37+
name: 'DevTools Name',
38+
}),
39+
],
40+
bootstrap: [AppComponent],
41+
})
42+
export class AppModule {}
43+
`;
44+
const expected = `
45+
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
46+
47+
@NgModule({
48+
imports: [
49+
StoreDevtoolsModule.instrument({
50+
name: 'DevTools Name',
51+
connectInZone: true}),
52+
],
53+
bootstrap: [AppComponent],
54+
})
55+
export class AppModule {}
56+
`;
57+
58+
appTree.create('./app.module.ts', input);
59+
const runner = new SchematicTestRunner('schematics', collectionPath);
60+
61+
const newTree = await runner.runSchematic(migrationname, {}, appTree);
62+
const file = newTree.readContent('app.module.ts');
63+
64+
expect(file).toBe(expected);
65+
}));
66+
67+
it(`should add connectInZone to config properties (previous property doesn't end with a comma)`, waitForAsync(async () => {
68+
const input = `
69+
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
70+
71+
@NgModule({
72+
imports: [
73+
StoreDevtoolsModule.instrument({
74+
name: 'DevTools Name'
75+
}),
76+
],
77+
bootstrap: [AppComponent],
78+
})
79+
export class AppModule {}
80+
`;
81+
const expected = `
82+
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
83+
84+
@NgModule({
85+
imports: [
86+
StoreDevtoolsModule.instrument({
87+
name: 'DevTools Name'
88+
, connectInZone: true}),
89+
],
90+
bootstrap: [AppComponent],
91+
})
92+
export class AppModule {}
93+
`;
94+
95+
appTree.create('./app.module.ts', input);
96+
const runner = new SchematicTestRunner('schematics', collectionPath);
97+
98+
const newTree = await runner.runSchematic(migrationname, {}, appTree);
99+
const file = newTree.readContent('app.module.ts');
100+
101+
expect(file).toBe(expected);
102+
}));
103+
104+
it(`should add connectInZone to empty config properties`, waitForAsync(async () => {
105+
const input = `
106+
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
107+
108+
@NgModule({
109+
imports: [
110+
StoreDevtoolsModule.instrument({
111+
}),
112+
],
113+
bootstrap: [AppComponent],
114+
})
115+
export class AppModule {}
116+
`;
117+
const expected = `
118+
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
119+
120+
@NgModule({
121+
imports: [
122+
StoreDevtoolsModule.instrument({
123+
connectInZone: true}),
124+
],
125+
bootstrap: [AppComponent],
126+
})
127+
export class AppModule {}
128+
`;
129+
130+
appTree.create('./app.module.ts', input);
131+
const runner = new SchematicTestRunner('schematics', collectionPath);
132+
133+
const newTree = await runner.runSchematic(migrationname, {}, appTree);
134+
const file = newTree.readContent('app.module.ts');
135+
136+
expect(file).toBe(expected);
137+
}));
138+
139+
it(`should add connectInZone to empty config`, waitForAsync(async () => {
140+
const input = `
141+
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
142+
143+
@NgModule({
144+
imports: [
145+
StoreDevtoolsModule.instrument(),
146+
],
147+
bootstrap: [AppComponent],
148+
})
149+
export class AppModule {}
150+
`;
151+
const expected = `
152+
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
153+
154+
@NgModule({
155+
imports: [
156+
StoreDevtoolsModule.instrument({connectInZone: true}),
157+
],
158+
bootstrap: [AppComponent],
159+
})
160+
export class AppModule {}
161+
`;
162+
163+
appTree.create('./app.module.ts', input);
164+
const runner = new SchematicTestRunner('schematics', collectionPath);
165+
166+
const newTree = await runner.runSchematic(migrationname, {}, appTree);
167+
const file = newTree.readContent('app.module.ts');
168+
169+
expect(file).toBe(expected);
170+
}));
171+
172+
it(`renames connectOutsideZone to connectInZone and inverts its value`, waitForAsync(async () => {
173+
const input = `
174+
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
175+
176+
@NgModule({
177+
imports: [
178+
StoreDevtoolsModule.instrument({
179+
connectOutsideZone: true,
180+
}),
181+
],
182+
bootstrap: [AppComponent],
183+
})
184+
export class AppModule {}
185+
`;
186+
const expected = `
187+
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
188+
189+
@NgModule({
190+
imports: [
191+
StoreDevtoolsModule.instrument({
192+
connectInZone: false,
193+
}),
194+
],
195+
bootstrap: [AppComponent],
196+
})
197+
export class AppModule {}
198+
`;
199+
200+
appTree.create('./app.module.ts', input);
201+
const runner = new SchematicTestRunner('schematics', collectionPath);
202+
203+
const newTree = await runner.runSchematic(migrationname, {}, appTree);
204+
const file = newTree.readContent('app.module.ts');
205+
206+
expect(file).toBe(expected);
207+
}));
208+
});
209+
210+
describe('bootstrapApplication', () => {
211+
it(`should add connectInZone to config properties (previous property ends with a comma)`, waitForAsync(async () => {
212+
const input = `
213+
import { provideStoreDevtools } from '@ngrx/store-devtools';
214+
215+
bootstrapApplication(AppComponent, {
216+
providers: [
217+
provideStoreDevtools({
218+
maxAge: 25,
219+
logOnly: !isDevMode(),
220+
}),
221+
],
222+
});
223+
`;
224+
const expected = `
225+
import { provideStoreDevtools } from '@ngrx/store-devtools';
226+
227+
bootstrapApplication(AppComponent, {
228+
providers: [
229+
provideStoreDevtools({
230+
maxAge: 25,
231+
logOnly: !isDevMode(),
232+
connectInZone: true}),
233+
],
234+
});
235+
`;
236+
237+
appTree.create('./main.ts', input);
238+
const runner = new SchematicTestRunner('schematics', collectionPath);
239+
240+
const newTree = await runner.runSchematic(migrationname, {}, appTree);
241+
const file = newTree.readContent('main.ts');
242+
243+
expect(file).toBe(expected);
244+
}));
245+
246+
it(`should add connectInZone to config properties (previous property doesn't end with a comma)`, waitForAsync(async () => {
247+
const input = `
248+
import { provideStoreDevtools } from '@ngrx/store-devtools';
249+
250+
bootstrapApplication(AppComponent, {
251+
providers: [
252+
provideStoreDevtools({
253+
maxAge: 25,
254+
logOnly: !isDevMode()
255+
}),
256+
],
257+
});
258+
`;
259+
const expected = `
260+
import { provideStoreDevtools } from '@ngrx/store-devtools';
261+
262+
bootstrapApplication(AppComponent, {
263+
providers: [
264+
provideStoreDevtools({
265+
maxAge: 25,
266+
logOnly: !isDevMode()
267+
, connectInZone: true}),
268+
],
269+
});
270+
`;
271+
272+
appTree.create('./main.ts', input);
273+
const runner = new SchematicTestRunner('schematics', collectionPath);
274+
275+
const newTree = await runner.runSchematic(migrationname, {}, appTree);
276+
const file = newTree.readContent('main.ts');
277+
278+
expect(file).toBe(expected);
279+
}));
280+
281+
it(`should add connectInZone to empty config properties`, waitForAsync(async () => {
282+
const input = `
283+
import { provideStoreDevtools } from '@ngrx/store-devtools';
284+
285+
bootstrapApplication(AppComponent, {
286+
providers: [
287+
provideStoreDevtools({ }),
288+
],
289+
});
290+
`;
291+
const expected = `
292+
import { provideStoreDevtools } from '@ngrx/store-devtools';
293+
294+
bootstrapApplication(AppComponent, {
295+
providers: [
296+
provideStoreDevtools({ connectInZone: true}),
297+
],
298+
});
299+
`;
300+
301+
appTree.create('./main.ts', input);
302+
const runner = new SchematicTestRunner('schematics', collectionPath);
303+
304+
const newTree = await runner.runSchematic(migrationname, {}, appTree);
305+
const file = newTree.readContent('main.ts');
306+
307+
expect(file).toBe(expected);
308+
}));
309+
310+
it(`should add connectInZone to empty config`, waitForAsync(async () => {
311+
const input = `
312+
import { provideStoreDevtools } from '@ngrx/store-devtools';
313+
314+
bootstrapApplication(AppComponent, {
315+
providers: [
316+
provideStoreDevtools(),
317+
],
318+
});
319+
`;
320+
const expected = `
321+
import { provideStoreDevtools } from '@ngrx/store-devtools';
322+
323+
bootstrapApplication(AppComponent, {
324+
providers: [
325+
provideStoreDevtools({connectInZone: true}),
326+
],
327+
});
328+
`;
329+
330+
appTree.create('./main.ts', input);
331+
const runner = new SchematicTestRunner('schematics', collectionPath);
332+
333+
const newTree = await runner.runSchematic(migrationname, {}, appTree);
334+
const file = newTree.readContent('main.ts');
335+
336+
expect(file).toBe(expected);
337+
}));
338+
339+
it(`renames connectOutsideZone to connectInZone and inverts its value`, waitForAsync(async () => {
340+
const input = `
341+
import { provideStoreDevtools } from '@ngrx/store-devtools';
342+
343+
bootstrapApplication(AppComponent, {
344+
providers: [
345+
provideStoreDevtools({ connectOutsideZone: someValue }),
346+
],
347+
});
348+
`;
349+
const expected = `
350+
import { provideStoreDevtools } from '@ngrx/store-devtools';
351+
352+
bootstrapApplication(AppComponent, {
353+
providers: [
354+
provideStoreDevtools({ connectInZone: !someValue }),
355+
],
356+
});
357+
`;
358+
359+
appTree.create('./main.ts', input);
360+
const runner = new SchematicTestRunner('schematics', collectionPath);
361+
362+
const newTree = await runner.runSchematic(migrationname, {}, appTree);
363+
const file = newTree.readContent('main.ts');
364+
365+
expect(file).toBe(expected);
366+
}));
367+
});
368+
});

0 commit comments

Comments
 (0)