/
missing_injectable_migration_spec.ts
711 lines (544 loc) Β· 22.8 KB
/
missing_injectable_migration_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {getSystemPath, normalize, virtualFs} from '@angular-devkit/core';
import {TempScopedNodeJsSyncHost} from '@angular-devkit/core/node/testing';
import {HostTree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
import * as shx from 'shelljs';
describe('Missing injectable migration', () => {
let runner: SchematicTestRunner;
let host: TempScopedNodeJsSyncHost;
let tree: UnitTestTree;
let tmpDirPath: string;
let previousWorkingDir: string;
let warnOutput: string[];
beforeEach(() => {
runner = new SchematicTestRunner('test', require.resolve('../migrations.json'));
host = new TempScopedNodeJsSyncHost();
tree = new UnitTestTree(new HostTree(host));
writeFile('/tsconfig.json', JSON.stringify({
compilerOptions: {
experimentalDecorators: true,
lib: ['es2015'],
},
}));
writeFile('/angular.json', JSON.stringify({
projects: {t: {architect: {build: {options: {tsConfig: './tsconfig.json'}}}}}
}));
warnOutput = [];
runner.logger.subscribe(logEntry => {
if (logEntry.level === 'warn') {
warnOutput.push(logEntry.message);
}
});
previousWorkingDir = shx.pwd();
tmpDirPath = getSystemPath(host.root);
// Switch into the temporary directory path. This allows us to run
// the schematic against our custom unit test tree.
shx.cd(tmpDirPath);
writeFile('/node_modules/@angular/core/index.d.ts', `
export declare function forwardRef(fn: Function);
`);
});
afterEach(() => {
shx.cd(previousWorkingDir);
shx.rm('-r', tmpDirPath);
});
function writeFile(filePath: string, contents: string) {
host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents));
}
async function runMigration() {
await runner.runSchematicAsync('migration-v9-missing-injectable', {}, tree).toPromise();
}
describe('NgModule', () => createTests('NgModule', 'providers'));
describe('Directive', () => createTests('Directive', 'providers'));
describe('Component', () => {
createTests('Component', 'providers');
createTests('Component', 'viewProviders');
it('should migrate all providers defined in "viewProviders" and "providers" in the ' +
'same component',
async() => {
writeFile('/index.ts', `
import {Component} from '@angular/core';
export class MyService {}
export class MySecondService {}
@Component({
providers: [MyService],
viewProviders: [MySecondService],
})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class MyService/);
expect(tree.readContent('/index.ts'))
.toMatch(/@Injectable\(\)\s+export class MySecondService/);
expect(tree.readContent('/index.ts'))
.toContain(`{ Component, Injectable } from '@angular/core`);
});
});
function createTests(
type: 'NgModule' | 'Directive' | 'Component', propName: 'providers' | 'viewProviders') {
it(`should migrate type provider in ${type}`, async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
export class MyService {}
@${type}({${propName}: [MyService]})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class MyService/);
expect(tree.readContent('/index.ts'))
.toContain(`{ ${type}, Injectable } from '@angular/core`);
});
it(`should migrate object literal provider in ${type}`, async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
export class MyService {}
@${type}({${propName}: [{provide: MyService}]})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class MyService/);
expect(tree.readContent('/index.ts'))
.toContain(`{ ${type}, Injectable } from '@angular/core`);
});
it(`should migrate object literal provider with forwardRef in ${type}`, async() => {
writeFile('/index.ts', `
import {${type}, forwardRef} from '@angular/core';
@${type}({${propName}: [{provide: forwardRef(() => MyService)}]})
export class TestClass {}
export class MyService {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class MyService/);
expect(tree.readContent('/index.ts'))
.toContain(`{ ${type}, forwardRef, Injectable } from '@angular/core`);
});
it(`should not migrate object literal provider with "useValue" in ${type}`, async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
export class MyService {}
@${type}({${propName}: [{provide: MyService, useValue: null }]})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).not.toContain('@Injectable');
});
it(`should not migrate object literal provider with "useFactory" in ${type}`, async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
export class MyService {}
@${type}({${propName}: [{provide: MyService, useFactory: () => null }]})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).not.toContain('@Injectable');
});
it(`should not migrate object literal provider with "useExisting" in ${type}`, async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
export class MyService {}
export class MyToken {}
@${type}({${propName}: [
{provide: MyService: useValue: null},
{provide: MyToken, useExisting: MyService},
]})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).not.toContain('@Injectable');
});
it(`should migrate object literal provider with "useClass" in ${type}`, async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
export class MyService {}
export class MyToken {}
@${type}({${propName}: [{provide: MyToken, useClass: MyService}]})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class MyService/);
expect(tree.readContent('/index.ts')).toMatch(/MyService {}\s+export class MyToken/);
expect(tree.readContent('/index.ts'))
.toContain(`{ ${type}, Injectable } from '@angular/core`);
});
it(`should not migrate references for providers with "useExisting" in ${type}, but migrate ` +
`existing token if declared in other ${type}`,
async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
export class MyService {}
export class MyToken {}
@${type}({
${propName}: [
{provide: MyToken, useExisting: MyService},
],
})
export class TestClass {}
`);
writeFile('/other.ts', `
import {${type} from '@angular/core';
import {MyService} from './index';
export @${type}({
${propName}: [{provide: MyService}],
})
export class OtherClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts'))
.toMatch(/@angular\/core';\s+@Injectable\(\)\s+export class MyService/);
expect(tree.readContent('/index.ts')).toMatch(/MyService {}\s+export class MyToken/);
});
it('should not migrate provider which is already decorated with @Injectable', async() => {
writeFile('/index.ts', `
import {Injectable, ${type}} from '@angular/core';
@Injectable()
export class MyService {}
@${type}({${propName}: [MyService]})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts'))
.toMatch(/@angular\/core';\s+@Injectable\(\)\s+export class MyService/);
});
it('should not migrate provider which is already decorated with @Directive', async() => {
writeFile('/index.ts', `
import {Directive, ${type}} from '@angular/core';
@Directive()
export class MyService {}
@${type}({${propName}: [MyService]})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).not.toContain('@Injectable');
});
it('should not migrate provider which is already decorated with @Component', async() => {
writeFile('/index.ts', `
import {Component, ${type}} from '@angular/core';
@Component()
export class MyService {}
@${type}({${propName}: [MyService]})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).not.toContain('@Injectable');
});
it('should not migrate provider which is already decorated with @Pipe', async() => {
writeFile('/index.ts', `
import {Pipe, ${type}} from '@angular/core';
@Pipe()
export class MyService {}
@${type}({${propName}: [MyService]})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).not.toContain('@Injectable');
});
it(`should migrate multiple providers in same ${type}`, async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
export class ServiceA {}
export class ServiceB {}
@${type}({${propName}: [ServiceA, ServiceB]})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class ServiceA/);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class ServiceB/);
expect(tree.readContent('/index.ts'))
.toContain(`{ ${type}, Injectable } from '@angular/core`);
});
it(`should migrate multiple mixed providers in same ${type}`, async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
export class ServiceA {}
export class ServiceB {}
export class ServiceC {}
@${type}({
${propName}: [
ServiceA,
{provide: ServiceB},
{provide: SomeToken, useClass: ServiceC},
]})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class ServiceA/);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class ServiceB/);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class ServiceC/);
expect(tree.readContent('/index.ts'))
.toContain(`{ ${type}, Injectable } from '@angular/core`);
});
it(`should migrate multiple nested providers in same ${type}`, async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
export class ServiceA {}
export class ServiceB {}
export class ServiceC {}
@${type}({
${propName}: [
ServiceA,
[
{provide: ServiceB},
ServiceC,
],
]})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class ServiceA/);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class ServiceB/);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class ServiceC/);
expect(tree.readContent('/index.ts'))
.toContain(`{ ${type}, Injectable } from '@angular/core`);
});
it('should migrate providers referenced through identifier', async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
export class ServiceA {}
export class ServiceB {}
const PROVIDERS = [ServiceA, ServiceB];
@${type}({
${propName}: PROVIDERS,
})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class ServiceA/);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class ServiceB/);
expect(tree.readContent('/index.ts'))
.toContain(`{ ${type}, Injectable } from '@angular/core`);
});
it('should migrate providers created through static analyzable function call', async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
export class ServiceA {}
export class ServiceB {}
export function createProviders(x: any) {
return [ServiceA, x]
}
@${type}({
${propName}: createProviders(ServiceB),
})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class ServiceA/);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class ServiceB/);
expect(tree.readContent('/index.ts'))
.toContain(`{ ${type}, Injectable } from '@angular/core`);
});
it('should migrate providers which are computed through spread operator', async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
export class ServiceA {}
export class ServiceB {}
const otherServices = [ServiceB];
@${type}({
${propName}: [ServiceA, ...otherServices],
})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class ServiceA/);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class ServiceB/);
expect(tree.readContent('/index.ts'))
.toContain(`{ ${type}, Injectable } from '@angular/core`);
});
it(`should migrate provider once if referenced in multiple ${type} definitions`, async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
export class ServiceA {}
@${type}({${propName}: [ServiceA]})
export class TestClassA {}
`);
writeFile('/second.ts', `
import {${type}} from '@angular/core';
import {ServiceA} from './index';
export class ServiceB {}
@${type}({${propName}: [ServiceA, ServiceB]})
export class TestClassB {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts'))
.toMatch(/@angular\/core';\s+@Injectable\(\)\s+export class ServiceA/);
expect(tree.readContent('/index.ts'))
.toContain(`{ ${type}, Injectable } from '@angular/core`);
expect(tree.readContent('/second.ts')).toMatch(/@Injectable\(\)\s+export class ServiceB/);
expect(tree.readContent('/second.ts'))
.toContain(`{ ${type}, Injectable } from '@angular/core`);
});
it('should create new import for @Injectable when migrating provider', async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
import {MyService, MySecondService} from './service';
@${type}({${propName}: [MyService, MySecondService]})
export class TestClass {}
`);
writeFile('/service.ts', `export class MyService {}
export class MySecondService {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/service.ts')).toMatch(/@Injectable\(\)\s+export class MyService/);
expect(tree.readContent('/service.ts'))
.toMatch(/@Injectable\(\)\s+export class MySecondService/);
expect(tree.readContent('/service.ts'))
.toMatch(/import { Injectable } from "@angular\/core";/);
});
it('should re-use existing namespace import for importing @Injectable when migrating provider',
async() => {
writeFile('/index.ts', `
import * as core from '@angular/core';
export class MyService {
constructor() {
console.log(core.isDevMode());
}
}
`);
writeFile('/app.module.ts', `
import {${type}} from '@angular/core';
import {MyService} from './index';
@${type}({${propName}: [MyService]})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts'))
.toMatch(/@core.Injectable\(\)\s+export class MyService/);
});
it('should warn if a referenced individual provider could not be resolved', async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
@${type}({${propName}: [NotPresent]})
export class TestClass {}
`);
await runMigration();
const providerSourceTextColumn = 15 + type.length + propName.length;
expect(warnOutput.length).toBe(1);
expect(warnOutput[0]).toMatch(/\s+index\.ts@4:.+Provider is not statically analyzable./);
expect(warnOutput[0]).toContain(`4:${providerSourceTextColumn}:`);
});
it(`should warn if ${propName} value could not be resolved`, async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
@${type}({${propName}: NOT_ANALYZABLE)
export class TestClass {}
`);
await runMigration();
const propValueSourceTextColumn = 14 + type.length + propName.length;
expect(warnOutput.length).toBe(1);
expect(warnOutput[0]).toMatch(/\s+index\.ts@4:.+Providers.*not statically analyzable./);
expect(warnOutput[0]).toContain(`4:${propValueSourceTextColumn}:`);
});
it(`should not throw if an empty @${type} is analyzed`, async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
@${type}()
export class MyModule {}
`);
try {
await runMigration();
} catch (e) {
fail(e);
}
expect(warnOutput.length).toBe(0);
});
it('should create new import for injectable after full end of last import statement',
async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
import {MyService} from './service';
@${type}({${propName}: [MyService]})
export class TestClass {}
`);
writeFile('/service.ts', `
import * as a from 'a';
import * as a from 'b'; // some comment
export class MyService {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/service.ts'))
.toMatch(/@Injectable\(\)\s+export class MyService/);
expect(tree.readContent('/service.ts'))
.toMatch(/'b'; \/\/ some comment\s+import { Injectable } from "@angular\/core";/);
});
it('should create new import at source file start with trailing new-line', async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
import {MyService} from './service';
@${type}({${propName}: [MyService]})
export class TestClass {}
`);
writeFile('/service.ts', `/* @license */
export class MyService {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/service.ts'))
.toMatch(
/^import { Injectable } from "@angular\/core";\s+\/\* @license \*\/\s+@Injectable\(\)\s+export class MyService/);
});
it('should remove @Inject decorator for providers which are migrated', async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
import {MyService} from './service';
@${type}({${propName}: [MyService]})
export class TestClass {}
`);
writeFile('/service.ts', `
import {Inject} from '@angular/core';
@Inject()
export class MyService {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/service.ts'))
.toMatch(/core';\s+@Injectable\(\)\s+export class MyService/);
// "inject" import will be preserved. We don't want to bother with detecting
// if the import is unused or not. We leave this up to the developers.
expect(tree.readContent('/service.ts'))
.toMatch(/import { Inject, Injectable } from '@angular\/core';/);
});
it('should not migrate provider classes in library type definitions', async() => {
writeFile('/node_modules/my-lib/index.d.ts', `
export declare class MyService {}
`);
writeFile('/index.ts', `
import {MyService} from 'my-lib';
import {Pipe, ${type}} from '@angular/core';
@${type}({${propName}: [MyService]})
export class TestClass {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/node_modules/my-lib/index.d.ts')).not.toContain('@Injectable');
});
}
});