|
| 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 | +import { getSystemPath, normalize, virtualFs } from '@angular-devkit/core'; |
| 9 | +import { TempScopedNodeJsSyncHost } from '@angular-devkit/core/node/testing'; |
| 10 | +import { HostTree } from '@angular-devkit/schematics'; |
| 11 | +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; |
| 12 | + |
| 13 | +describe('Migration to version 8', () => { |
| 14 | + const schematicRunner = new SchematicTestRunner( |
| 15 | + 'migrations', |
| 16 | + require.resolve('../migration-collection.json'), |
| 17 | + ); |
| 18 | + |
| 19 | + let tree: UnitTestTree; |
| 20 | + let host: TempScopedNodeJsSyncHost; |
| 21 | + |
| 22 | + const lazyRoutePath = normalize('src/lazy-route.ts'); |
| 23 | + const lazyRoute = virtualFs.stringToFileBuffer(` |
| 24 | + import { Route } from '@angular/router'; |
| 25 | + const routes: Array<Route> = [ |
| 26 | + { |
| 27 | + path: '', |
| 28 | + loadChildren: './lazy/lazy.module#LazyModule' |
| 29 | + } |
| 30 | + ]; |
| 31 | + `); |
| 32 | + |
| 33 | + const lazyChildRoute = virtualFs.stringToFileBuffer(` |
| 34 | + import { Route } from '@angular/router'; |
| 35 | + const routes: Array<Route> = [ |
| 36 | + { |
| 37 | + path: '', |
| 38 | + children: [{ |
| 39 | + path: 'child', |
| 40 | + loadChildren: './lazy/lazy.module#LazyModule' |
| 41 | + }] |
| 42 | + } |
| 43 | + ]; |
| 44 | + `); |
| 45 | + |
| 46 | + describe('Migration to import() style lazy routes', () => { |
| 47 | + beforeEach(async () => { |
| 48 | + host = new TempScopedNodeJsSyncHost(); |
| 49 | + tree = new UnitTestTree(new HostTree(host)); |
| 50 | + tree.create('/package.json', JSON.stringify({})); |
| 51 | + process.chdir(getSystemPath(host.root)); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should replace the module path string', async () => { |
| 55 | + await host.write(lazyRoutePath, lazyRoute).toPromise(); |
| 56 | + |
| 57 | + schematicRunner.runSchematic('migration-08', {}, tree); |
| 58 | + await schematicRunner.engine.executePostTasks().toPromise(); |
| 59 | + |
| 60 | + const routes = await host.read(lazyRoutePath) |
| 61 | + .toPromise() |
| 62 | + .then(virtualFs.fileBufferToString); |
| 63 | + |
| 64 | + expect(routes).not.toContain('./lazy/lazy.module#LazyModule'); |
| 65 | + expect(routes).toContain( |
| 66 | + `loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule)`); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should replace the module path string in a child path', async () => { |
| 70 | + await host.write(lazyRoutePath, lazyChildRoute).toPromise(); |
| 71 | + |
| 72 | + schematicRunner.runSchematic('migration-08', {}, tree); |
| 73 | + await schematicRunner.engine.executePostTasks().toPromise(); |
| 74 | + |
| 75 | + const routes = await host.read(lazyRoutePath) |
| 76 | + .toPromise() |
| 77 | + .then(virtualFs.fileBufferToString); |
| 78 | + |
| 79 | + expect(routes).not.toContain('./lazy/lazy.module#LazyModule'); |
| 80 | + |
| 81 | + expect(routes).toContain( |
| 82 | + `loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule)`); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments