|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. 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 * as shx from 'shelljs'; |
| 14 | + |
| 15 | +describe('move-document migration', () => { |
| 16 | + let runner: SchematicTestRunner; |
| 17 | + let host: TempScopedNodeJsSyncHost; |
| 18 | + let tree: UnitTestTree; |
| 19 | + let tmpDirPath: string; |
| 20 | + let previousWorkingDir: string; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + runner = new SchematicTestRunner('test', require.resolve('../migrations.json')); |
| 24 | + host = new TempScopedNodeJsSyncHost(); |
| 25 | + tree = new UnitTestTree(new HostTree(host)); |
| 26 | + |
| 27 | + writeFile('/tsconfig.json', JSON.stringify({ |
| 28 | + compilerOptions: { |
| 29 | + lib: ['es2015'], |
| 30 | + } |
| 31 | + })); |
| 32 | + |
| 33 | + previousWorkingDir = shx.pwd(); |
| 34 | + tmpDirPath = getSystemPath(host.root); |
| 35 | + |
| 36 | + // Switch into the temporary directory path. This allows us to run |
| 37 | + // the schematic against our custom unit test tree. |
| 38 | + shx.cd(tmpDirPath); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + shx.cd(previousWorkingDir); |
| 43 | + shx.rm('-r', tmpDirPath); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('move-document', () => { |
| 47 | + it('should properly apply import replacement', () => { |
| 48 | + writeFile('/index.ts', ` |
| 49 | + import {DOCUMENT} from '@angular/platform-browser'; |
| 50 | + `); |
| 51 | + |
| 52 | + runMigration(); |
| 53 | + |
| 54 | + const content = tree.readContent('/index.ts'); |
| 55 | + |
| 56 | + expect(content).toContain(`import { DOCUMENT } from "@angular/common";`); |
| 57 | + expect(content).not.toContain(`import {DOCUMENT} from '@angular/platform-browser';`); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should properly apply import replacement with existing import', () => { |
| 61 | + writeFile('/index.ts', ` |
| 62 | + import {DOCUMENT} from '@angular/platform-browser'; |
| 63 | + import {someImport} from '@angular/common'; |
| 64 | + `); |
| 65 | + |
| 66 | + writeFile('/reverse.ts', ` |
| 67 | + import {someImport} from '@angular/common'; |
| 68 | + import {DOCUMENT} from '@angular/platform-browser'; |
| 69 | + `); |
| 70 | + |
| 71 | + runMigration(); |
| 72 | + |
| 73 | + const content = tree.readContent('/index.ts'); |
| 74 | + const contentReverse = tree.readContent('/reverse.ts'); |
| 75 | + |
| 76 | + expect(content).toContain(`import { someImport, DOCUMENT } from '@angular/common';`); |
| 77 | + expect(content).not.toContain(`import {DOCUMENT} from '@angular/platform-browser';`); |
| 78 | + |
| 79 | + expect(contentReverse).toContain(`import { someImport, DOCUMENT } from '@angular/common';`); |
| 80 | + expect(contentReverse).not.toContain(`import {DOCUMENT} from '@angular/platform-browser';`); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should properly apply import replacement with existing import w/ comments', () => { |
| 84 | + writeFile('/index.ts', ` |
| 85 | + /** |
| 86 | + * this is a comment |
| 87 | + */ |
| 88 | + import {someImport} from '@angular/common'; |
| 89 | + import {DOCUMENT} from '@angular/platform-browser'; |
| 90 | + `); |
| 91 | + |
| 92 | + runMigration(); |
| 93 | + |
| 94 | + const content = tree.readContent('/index.ts'); |
| 95 | + |
| 96 | + expect(content).toContain(`import { someImport, DOCUMENT } from '@angular/common';`); |
| 97 | + expect(content).not.toContain(`import {DOCUMENT} from '@angular/platform-browser';`); |
| 98 | + |
| 99 | + expect(content).toMatch(/.*this is a comment.*/); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should properly apply import replacement with existing and redundant imports', () => { |
| 103 | + writeFile('/index.ts', ` |
| 104 | + import {DOCUMENT} from '@angular/platform-browser'; |
| 105 | + import {anotherImport} from '@angular/platform-browser-dynamic'; |
| 106 | + import {someImport} from '@angular/common'; |
| 107 | + `); |
| 108 | + |
| 109 | + runMigration(); |
| 110 | + |
| 111 | + const content = tree.readContent('/index.ts'); |
| 112 | + |
| 113 | + expect(content).toContain(`import { someImport, DOCUMENT } from '@angular/common';`); |
| 114 | + expect(content).not.toContain(`import {DOCUMENT} from '@angular/platform-browser';`); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should properly apply import replacement with existing import and leave original import', |
| 118 | + () => { |
| 119 | + writeFile('/index.ts', ` |
| 120 | + import {DOCUMENT, anotherImport} from '@angular/platform-browser'; |
| 121 | + import {someImport} from '@angular/common'; |
| 122 | + `); |
| 123 | + |
| 124 | + runMigration(); |
| 125 | + |
| 126 | + const content = tree.readContent('/index.ts'); |
| 127 | + |
| 128 | + expect(content).toContain(`import { someImport, DOCUMENT } from '@angular/common';`); |
| 129 | + expect(content).toContain(`import { anotherImport } from '@angular/platform-browser';`); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should properly apply import replacement with existing import and alias', () => { |
| 133 | + writeFile('/index.ts', ` |
| 134 | + import {DOCUMENT as doc, anotherImport} from '@angular/platform-browser'; |
| 135 | + import {someImport} from '@angular/common'; |
| 136 | + `); |
| 137 | + |
| 138 | + runMigration(); |
| 139 | + |
| 140 | + const content = tree.readContent('/index.ts'); |
| 141 | + |
| 142 | + expect(content).toContain(`import { someImport, DOCUMENT as doc } from '@angular/common';`); |
| 143 | + expect(content).toContain(`import { anotherImport } from '@angular/platform-browser';`); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + function writeFile(filePath: string, contents: string) { |
| 148 | + host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents)); |
| 149 | + } |
| 150 | + |
| 151 | + function runMigration() { runner.runSchematic('migration-v8-move-document', {}, tree); } |
| 152 | +}); |
0 commit comments