|
| 1 | +import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; |
| 2 | + |
| 3 | +import { createTestApp } from '../../testing/scaffold'; |
| 4 | + |
| 5 | +const IMPORT = `import`; |
| 6 | + |
| 7 | +describe('fix imports', () => { |
| 8 | + const runner = new SchematicTestRunner( |
| 9 | + 'schematics', |
| 10 | + require.resolve('../migration-collection.json') |
| 11 | + ); |
| 12 | + |
| 13 | + async function setupTest() { |
| 14 | + const tree = await createTestApp(runner, { |
| 15 | + projectName: 'my-app', |
| 16 | + }); |
| 17 | + const runSchematicAsync = async () => |
| 18 | + runner.runSchematicAsync('fix-imports', {}, tree).toPromise(); |
| 19 | + return { tree, runSchematicAsync }; |
| 20 | + } |
| 21 | + |
| 22 | + it('should not change import paths that are correct', async () => { |
| 23 | + const { tree, runSchematicAsync } = await setupTest(); |
| 24 | + tree.create('/src/index.ts', `import { A } from './a';`); |
| 25 | + tree.create('/src/a.ts', `export class A {}`); |
| 26 | + tree.create('/src/empty.ts', ``); |
| 27 | + await runSchematicAsync(); |
| 28 | + expect(tree.readText('/src/index.ts')).toBe(`import { A } from './a';`); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should update import paths with backslashes', async () => { |
| 32 | + const { tree, runSchematicAsync } = await setupTest(); |
| 33 | + const angularJson = tree.readJson('angular.json') as { projects: any }; |
| 34 | + expect(angularJson).toHaveProperty('projects.my-app.sourceRoot'); |
| 35 | + const project = angularJson.projects['my-app']; |
| 36 | + delete project.sourceRoot; |
| 37 | + tree.overwrite('angular.json', JSON.stringify(angularJson)); |
| 38 | + tree.create('/src/windows.ts', "import { A } from '.\\a';"); |
| 39 | + tree.create('/src/a.ts', `export class A {}`); |
| 40 | + await runSchematicAsync(); |
| 41 | + expect(tree.readText('/src/windows.ts')).toBe(`import { A } from './a';`); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should change import paths that are incorrect', async () => { |
| 45 | + const { tree, runSchematicAsync } = await setupTest(); |
| 46 | + tree.create( |
| 47 | + '/src/index.ts', |
| 48 | + `import { A } from './A'; |
| 49 | + import { Aaa } from './Aaa'; |
| 50 | + import { C } from './path/c'; |
| 51 | + import { D } from './Path/D'; |
| 52 | + import { Main } from './path/'; |
| 53 | + import { NonExistent } from './non'; |
| 54 | + export class B extends A {}` |
| 55 | + ); |
| 56 | + tree.create('/src/a.ts', `export class A {}`); |
| 57 | + tree.create('/src/aaa.ts', `export class Aaa {}`); |
| 58 | + tree.create('/src/Path/C.ts', `export class C {}`); |
| 59 | + tree.create('/src/Path/d.ts', `export class D {}`); |
| 60 | + tree.create('/src/Path/index.ts', `export class Main {}`); |
| 61 | + tree.create( |
| 62 | + '/src/Path/With/Layers/e.ts', |
| 63 | + ` |
| 64 | + import { Main } from '../../../path/'; |
| 65 | + import { C } from '../../c'; |
| 66 | + export class E extends Main {} |
| 67 | + ` |
| 68 | + ); |
| 69 | + await runSchematicAsync(); |
| 70 | + expect(tree.readText('/src/index.ts')).toEqual( |
| 71 | + `import { A } from './a'; |
| 72 | + import { Aaa } from './aaa'; |
| 73 | + import { C } from './Path/C'; |
| 74 | + import { D } from './Path/d'; |
| 75 | + import { Main } from './Path/'; |
| 76 | + import { NonExistent } from './non'; |
| 77 | + export class B extends A {}` |
| 78 | + ); |
| 79 | + expect(tree.readText('/src/Path/With/Layers/e.ts')).toEqual(` |
| 80 | + import { Main } from '../../'; |
| 81 | + import { C } from '../../C'; |
| 82 | + export class E extends Main {} |
| 83 | + `); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should change export paths that are incorrect', async () => { |
| 87 | + const { tree, runSchematicAsync } = await setupTest(); |
| 88 | + tree.create( |
| 89 | + '/src/index.ts', |
| 90 | + `export * from './A'; |
| 91 | + export { C as Sea } from './path/c'; |
| 92 | + export * from './Path/D'; |
| 93 | + const value = 1; |
| 94 | + export { value }; |
| 95 | + export * from './non';` |
| 96 | + ); |
| 97 | + tree.create('/src/a.ts', `export class A {}`); |
| 98 | + tree.create('/src/Path/C.ts', `export class C {}`); |
| 99 | + tree.create('/src/Path/d.ts', `export class D {}`); |
| 100 | + await runSchematicAsync(); |
| 101 | + expect(tree.readText('/src/index.ts')).toEqual( |
| 102 | + `export * from './a'; |
| 103 | + export { C as Sea } from './Path/C'; |
| 104 | + export * from './Path/d'; |
| 105 | + const value = 1; |
| 106 | + export { value }; |
| 107 | + export * from './non';` |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should remove unnecessary core-js imports', async () => { |
| 112 | + const { tree, runSchematicAsync } = await setupTest(); |
| 113 | + tree.create( |
| 114 | + '/src/mod.ts', |
| 115 | + `${IMPORT} { setTimeout } from 'core-js'; |
| 116 | + ${IMPORT} values from 'core-js/features/object/values'; |
| 117 | +
|
| 118 | + const corejs = { |
| 119 | + values: values, |
| 120 | + }; |
| 121 | + const corejs2 = { |
| 122 | + values: require('core-js/library/fn/object/values'), |
| 123 | + };` |
| 124 | + ); |
| 125 | + tree.create( |
| 126 | + '/src/polyfill.ts', |
| 127 | + [ |
| 128 | + `// This is a polyfill for IE11.`, |
| 129 | + ``, |
| 130 | + `${IMPORT} 'core-js/es6';`, |
| 131 | + `${IMPORT} 'core-js/es7/reflect';`, |
| 132 | + `${IMPORT} 'zone.js/dist/zone';`, |
| 133 | + `${IMPORT} 'ts-helpers';`, |
| 134 | + ``, |
| 135 | + `// Add global to window, assigning the value of window itself.`, |
| 136 | + `(window as any).global = window;`, |
| 137 | + ].join(`\n`) |
| 138 | + ); |
| 139 | + await runSchematicAsync(); |
| 140 | + expect(tree.readText('/src/mod.ts').trim()).toEqual( |
| 141 | + `const corejs = { |
| 142 | + values: Object.values, |
| 143 | + }; |
| 144 | + const corejs2 = { |
| 145 | + values: Object.values, |
| 146 | + };` |
| 147 | + ); |
| 148 | + expect(tree.readText('/src/polyfill.ts')).toEqual( |
| 149 | + [ |
| 150 | + `// This is a polyfill for IE11.`, |
| 151 | + ``, |
| 152 | + `// ${IMPORT} 'core-js/es6';`, |
| 153 | + `// ${IMPORT} 'core-js/es7/reflect';`, |
| 154 | + `${IMPORT} 'zone.js/dist/zone';`, |
| 155 | + `${IMPORT} 'ts-helpers';`, |
| 156 | + ``, |
| 157 | + `// Add global to window, assigning the value of window itself.`, |
| 158 | + `(window as any).global = window;`, |
| 159 | + ].join(`\n`) |
| 160 | + ); |
| 161 | + }); |
| 162 | +}); |
0 commit comments