|  | 
|  | 1 | +/** | 
|  | 2 | + * @license | 
|  | 3 | + * Copyright Google LLC All Rights Reserved. | 
|  | 4 | + * | 
|  | 5 | + * Use of this source code is governed by an MIT-style license that can be | 
|  | 6 | + * found in the LICENSE file at https://angular.io/license | 
|  | 7 | + */ | 
|  | 8 | + | 
|  | 9 | +import {getSystemPath, normalize, virtualFs} from '@angular-devkit/core'; | 
|  | 10 | +import {TempScopedNodeJsSyncHost} from '@angular-devkit/core/node/testing'; | 
|  | 11 | +import {HostTree} from '@angular-devkit/schematics'; | 
|  | 12 | +import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing'; | 
|  | 13 | +import {runfiles} from '@bazel/runfiles'; | 
|  | 14 | +import shx from 'shelljs'; | 
|  | 15 | + | 
|  | 16 | +describe('experimental pending tasks migration', () => { | 
|  | 17 | + let runner: SchematicTestRunner; | 
|  | 18 | + let host: TempScopedNodeJsSyncHost; | 
|  | 19 | + let tree: UnitTestTree; | 
|  | 20 | + let tmpDirPath: string; | 
|  | 21 | + | 
|  | 22 | + function writeFile(filePath: string, contents: string) { | 
|  | 23 | + host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents)); | 
|  | 24 | + } | 
|  | 25 | + | 
|  | 26 | + function runMigration() { | 
|  | 27 | + return runner.runSchematic('pending-tasks', {}, tree); | 
|  | 28 | + } | 
|  | 29 | + | 
|  | 30 | + beforeEach(() => { | 
|  | 31 | + runner = new SchematicTestRunner('test', runfiles.resolvePackageRelative('../migrations.json')); | 
|  | 32 | + host = new TempScopedNodeJsSyncHost(); | 
|  | 33 | + tree = new UnitTestTree(new HostTree(host)); | 
|  | 34 | + | 
|  | 35 | + writeFile( | 
|  | 36 | + '/tsconfig.json', | 
|  | 37 | + JSON.stringify({ | 
|  | 38 | + compilerOptions: { | 
|  | 39 | + lib: ['es2015'], | 
|  | 40 | + strictNullChecks: true, | 
|  | 41 | + }, | 
|  | 42 | + }), | 
|  | 43 | + ); | 
|  | 44 | + | 
|  | 45 | + writeFile( | 
|  | 46 | + '/angular.json', | 
|  | 47 | + JSON.stringify({ | 
|  | 48 | + version: 1, | 
|  | 49 | + projects: {t: {root: '', architect: {build: {options: {tsConfig: './tsconfig.json'}}}}}, | 
|  | 50 | + }), | 
|  | 51 | + ); | 
|  | 52 | + | 
|  | 53 | + tmpDirPath = getSystemPath(host.root); | 
|  | 54 | + | 
|  | 55 | + // Switch into the temporary directory path. This allows us to run | 
|  | 56 | + // the schematic against our custom unit test tree. | 
|  | 57 | + shx.cd(tmpDirPath); | 
|  | 58 | + }); | 
|  | 59 | + | 
|  | 60 | + it('should update ExperimentalPendingTasks', async () => { | 
|  | 61 | + writeFile( | 
|  | 62 | + '/index.ts', | 
|  | 63 | + ` | 
|  | 64 | + import {ExperimentalPendingTasks, Directive} from '@angular/core'; | 
|  | 65 | +
 | 
|  | 66 | + @Directive({ | 
|  | 67 | + selector: '[someDirective]' | 
|  | 68 | + }) | 
|  | 69 | + export class SomeDirective { | 
|  | 70 | + x = inject(ExperimentalPendingTasks); | 
|  | 71 | + }`, | 
|  | 72 | + ); | 
|  | 73 | + | 
|  | 74 | + await runMigration(); | 
|  | 75 | + | 
|  | 76 | + const content = tree.readContent('/index.ts').replace(/\s+/g, ' '); | 
|  | 77 | + expect(content).toContain("import {PendingTasks, Directive} from '@angular/core';"); | 
|  | 78 | + expect(content).toContain('x = inject(PendingTasks);'); | 
|  | 79 | + }); | 
|  | 80 | + | 
|  | 81 | + it('should update import alias', async () => { | 
|  | 82 | + writeFile( | 
|  | 83 | + '/index.ts', | 
|  | 84 | + ` | 
|  | 85 | + import {ExperimentalPendingTasks as Y, Directive} from '@angular/core'; | 
|  | 86 | +
 | 
|  | 87 | + @Directive({ | 
|  | 88 | + selector: '[someDirective]' | 
|  | 89 | + }) | 
|  | 90 | + export class SomeDirective { | 
|  | 91 | + x = inject(Y); | 
|  | 92 | + }`, | 
|  | 93 | + ); | 
|  | 94 | + | 
|  | 95 | + await runMigration(); | 
|  | 96 | + | 
|  | 97 | + const content = tree.readContent('/index.ts').replace(/\s+/g, ' '); | 
|  | 98 | + expect(content).toContain("import {PendingTasks as Y, Directive} from '@angular/core';"); | 
|  | 99 | + expect(content).toContain('x = inject(Y);'); | 
|  | 100 | + }); | 
|  | 101 | +}); | 
0 commit comments