@@ -6,20 +6,22 @@ import {
6
6
import * as path from 'path' ;
7
7
import { createWorkspace } from '@ngrx/schematics-core/testing' ;
8
8
9
- describe ( 'Creator migration' , ( ) => {
10
- const schematicRunner = new SchematicTestRunner (
11
- '@ngrx/schematics' ,
12
- path . join ( __dirname , '../../collection.json' )
13
- ) ;
14
-
15
- let appTree : UnitTestTree ;
16
-
17
- beforeEach ( async ( ) => {
18
- appTree = await createWorkspace ( schematicRunner , appTree ) ;
19
- } ) ;
20
-
21
- it ( 'should use createEffect for non-dispatching effects' , async ( ) => {
22
- const input = tags . stripIndent `
9
+ describe ( 'Effects Migration 13_0_0' , ( ) => {
10
+ describe ( '@Effect to createEffect' , ( ) => {
11
+ const collectionPath = path . join ( __dirname , '../migration.json' ) ;
12
+ const schematicRunner = new SchematicTestRunner (
13
+ 'schematics' ,
14
+ collectionPath
15
+ ) ;
16
+
17
+ let appTree : UnitTestTree ;
18
+
19
+ beforeEach ( async ( ) => {
20
+ appTree = await createWorkspace ( schematicRunner , appTree ) ;
21
+ } ) ;
22
+
23
+ it ( 'migrates to createEffect for dispatching effects' , async ( ) => {
24
+ const input = tags . stripIndent `
23
25
@Injectable()
24
26
export class SomeEffectsClass {
25
27
constructor(private actions$: Actions) {}
@@ -31,7 +33,7 @@ describe('Creator migration', () => {
31
33
}
32
34
` ;
33
35
34
- const output = tags . stripIndent `
36
+ const output = tags . stripIndent `
35
37
@Injectable()
36
38
export class SomeEffectsClass {
37
39
constructor(private actions$: Actions) {}
@@ -43,11 +45,11 @@ describe('Creator migration', () => {
43
45
}
44
46
` ;
45
47
46
- await runTest ( input , output ) ;
47
- } ) ;
48
+ await runTest ( input , output ) ;
49
+ } ) ;
48
50
49
- it ( 'should use createEffect for non-dispatching effects' , async ( ) => {
50
- const input = tags . stripIndent `
51
+ it ( 'migrates to createEffect for non-dispatching effects' , async ( ) => {
52
+ const input = tags . stripIndent `
51
53
@Injectable()
52
54
export class SomeEffectsClass {
53
55
constructor(private actions$: Actions) {}
@@ -59,7 +61,7 @@ describe('Creator migration', () => {
59
61
}
60
62
` ;
61
63
62
- const output = tags . stripIndent `
64
+ const output = tags . stripIndent `
63
65
@Injectable()
64
66
export class SomeEffectsClass {
65
67
constructor(private actions$: Actions) {}
@@ -71,11 +73,11 @@ describe('Creator migration', () => {
71
73
}
72
74
` ;
73
75
74
- await runTest ( input , output ) ;
75
- } ) ;
76
+ await runTest ( input , output ) ;
77
+ } ) ;
76
78
77
- it ( 'should use createEffect for effects as functions' , async ( ) => {
78
- const input = tags . stripIndent `
79
+ it ( 'migrates to createEffect for effects as functions' , async ( ) => {
80
+ const input = tags . stripIndent `
79
81
@Injectable()
80
82
export class SomeEffectsClass {
81
83
constructor(private actions$: Actions) {}
@@ -87,7 +89,7 @@ describe('Creator migration', () => {
87
89
}
88
90
` ;
89
91
90
- const output = tags . stripIndent `
92
+ const output = tags . stripIndent `
91
93
@Injectable()
92
94
export class SomeEffectsClass {
93
95
constructor(private actions$: Actions) {}
@@ -99,11 +101,11 @@ describe('Creator migration', () => {
99
101
}
100
102
` ;
101
103
102
- await runTest ( input , output ) ;
103
- } ) ;
104
+ await runTest ( input , output ) ;
105
+ } ) ;
104
106
105
- it ( 'should stay off of other decorators' , async ( ) => {
106
- const input = tags . stripIndent `
107
+ it ( 'keeps other decorators' , async ( ) => {
108
+ const input = tags . stripIndent `
107
109
@Injectable()
108
110
export class SomeEffectsClass {
109
111
constructor(private actions$: Actions) {}
@@ -122,7 +124,7 @@ describe('Creator migration', () => {
122
124
}
123
125
` ;
124
126
125
- const output = tags . stripIndent `
127
+ const output = tags . stripIndent `
126
128
@Injectable()
127
129
export class SomeEffectsClass {
128
130
constructor(private actions$: Actions) {}
@@ -141,31 +143,31 @@ describe('Creator migration', () => {
141
143
}
142
144
` ;
143
145
144
- await runTest ( input , output ) ;
145
- } ) ;
146
+ await runTest ( input , output ) ;
147
+ } ) ;
146
148
147
- it ( 'should import createEffect' , async ( ) => {
148
- const input = tags . stripIndent `
149
+ it ( 'imports createEffect from effects ' , async ( ) => {
150
+ const input = tags . stripIndent `
149
151
import { Actions, ofType, Effect } from '@ngrx/effects';
150
152
@Injectable()
151
153
export class SomeEffectsClass {
152
154
constructor(private actions$: Actions) {}
153
155
}
154
156
` ;
155
157
156
- const output = tags . stripIndent `
158
+ const output = tags . stripIndent `
157
159
import { Actions, ofType, createEffect } from '@ngrx/effects';
158
160
@Injectable()
159
161
export class SomeEffectsClass {
160
162
constructor(private actions$: Actions) {}
161
163
}
162
164
` ;
163
165
164
- await runTest ( input , output ) ;
165
- } ) ;
166
+ await runTest ( input , output ) ;
167
+ } ) ;
166
168
167
- it ( 'should not import createEffect if already imported' , async ( ) => {
168
- const input = tags . stripIndent `
169
+ it ( 'does not import createEffect if already imported' , async ( ) => {
170
+ const input = tags . stripIndent `
169
171
import { Actions, Effect, createEffect, ofType } from '@ngrx/effects';
170
172
@Injectable()
171
173
export class SomeEffectsClass {
@@ -178,7 +180,7 @@ describe('Creator migration', () => {
178
180
}
179
181
` ;
180
182
181
- const output = tags . stripIndent `
183
+ const output = tags . stripIndent `
182
184
import { Actions, createEffect, ofType } from '@ngrx/effects';
183
185
@Injectable()
184
186
export class SomeEffectsClass {
@@ -191,11 +193,11 @@ describe('Creator migration', () => {
191
193
}
192
194
` ;
193
195
194
- await runTest ( input , output ) ;
195
- } ) ;
196
+ await runTest ( input , output ) ;
197
+ } ) ;
196
198
197
- it ( 'should not run the schematic if the createEffect syntax is already used' , async ( ) => {
198
- const input = tags . stripIndent `
199
+ it ( 'does not migrate if the createEffect syntax is already used' , async ( ) => {
200
+ const input = tags . stripIndent `
199
201
import { Actions, createEffect, ofType } from '@ngrx/effects';
200
202
@Injectable()
201
203
export class SomeEffectsClass {
@@ -207,23 +209,50 @@ describe('Creator migration', () => {
207
209
}
208
210
` ;
209
211
210
- await runTest ( input , input ) ;
212
+ await runTest ( input , input ) ;
213
+ } ) ;
214
+
215
+ it ( 'removes the @Effect decorator' , async ( ) => {
216
+ const input = tags . stripIndent `
217
+ import { Actions, createEffect, ofType } from '@ngrx/effects';
218
+ @Injectable()
219
+ export class SomeEffectsClass {
220
+ @Effect()
221
+ logout$ = createEffect(() => this.actions$.pipe(
222
+ ofType('LOGOUT'),
223
+ map(() => ({ type: 'LOGGED_OUT' }))
224
+ ));
225
+ constructor(private actions$: Actions) {}
226
+ }
227
+ ` ;
228
+ const output = tags . stripIndent `
229
+ import { Actions, createEffect, ofType } from '@ngrx/effects';
230
+ @Injectable()
231
+ export class SomeEffectsClass {
232
+ logout$ = createEffect(() => this.actions$.pipe(
233
+ ofType('LOGOUT'),
234
+ map(() => ({ type: 'LOGGED_OUT' }))
235
+ ));
236
+ constructor(private actions$: Actions) {}
237
+ }
238
+ ` ;
239
+ await runTest ( input , output ) ;
240
+ } ) ;
241
+
242
+ async function runTest ( input : string , expected : string ) {
243
+ const effectPath = '/some.effects.ts' ;
244
+ appTree . create ( effectPath , input ) ;
245
+
246
+ const tree = await schematicRunner
247
+ . runSchematicAsync ( `ngrx-effects-migration-03` , { } , appTree )
248
+ . toPromise ( ) ;
249
+
250
+ const actual = tree . readContent ( effectPath ) ;
251
+
252
+ const removeEmptyLines = ( value : string ) =>
253
+ value . replace ( / ^ \s * $ (?: \r \n ? | \n ) / gm, '' ) ;
254
+
255
+ expect ( removeEmptyLines ( actual ) ) . toBe ( removeEmptyLines ( expected ) ) ;
256
+ }
211
257
} ) ;
212
-
213
- async function runTest ( input : string , expected : string ) {
214
- const options = { } ;
215
- const effectPath = '/some.effects.ts' ;
216
- appTree . create ( effectPath , input ) ;
217
-
218
- const tree = await schematicRunner
219
- . runSchematicAsync ( 'create-effect-migration' , options , appTree )
220
- . toPromise ( ) ;
221
-
222
- const actual = tree . readContent ( effectPath ) ;
223
-
224
- const removeEmptyLines = ( value : string ) =>
225
- value . replace ( / ^ \s * $ (?: \r \n ? | \n ) / gm, '' ) ;
226
-
227
- expect ( removeEmptyLines ( actual ) ) . toBe ( removeEmptyLines ( expected ) ) ;
228
- }
229
258
} ) ;
0 commit comments