|
| 1 | +import {UnitTestTree} from '@angular-devkit/schematics/testing'; |
| 2 | +import {createTestCaseSetup} from '../../../../testing'; |
| 3 | +import {join} from 'path'; |
| 4 | +import {MIGRATION_PATH} from '../../../../paths'; |
| 5 | + |
| 6 | +describe('v13 tilde import migration', () => { |
| 7 | + const PROJECT_PATH = '/projects/cdk-testing'; |
| 8 | + const TEST_PATH = join(PROJECT_PATH, 'src/test.scss'); |
| 9 | + let tree: UnitTestTree; |
| 10 | + let _writeFile: (filePath: string, text: string) => void; |
| 11 | + let runMigration: () => Promise<{logOutput: string}>; |
| 12 | + |
| 13 | + beforeEach(async () => { |
| 14 | + const testSetup = await createTestCaseSetup('migration-v13', MIGRATION_PATH, []); |
| 15 | + tree = testSetup.appTree; |
| 16 | + runMigration = testSetup.runFixers; |
| 17 | + _writeFile = testSetup.writeFile; |
| 18 | + }); |
| 19 | + |
| 20 | + /** Writes an array of lines as a single file. */ |
| 21 | + function writeLines(path: string, lines: string[]): void { |
| 22 | + _writeFile(path, lines.join('\n')); |
| 23 | + } |
| 24 | + |
| 25 | + /** Reads a file and split it into an array where each item is a new line. */ |
| 26 | + function splitFile(path: string): string[] { |
| 27 | + return tree.readContent(path).split('\n'); |
| 28 | + } |
| 29 | + |
| 30 | + it('should remove the tilde from angular imports', async () => { |
| 31 | + writeLines(TEST_PATH, [ |
| 32 | + `@use '~@angular/material' as mat;`, |
| 33 | + `@import '~@angular/material/theming';`, |
| 34 | + `@import '~@angular/cdk/overlay-prebuilt.css';`, |
| 35 | + |
| 36 | + `@include mat.button-theme();`, |
| 37 | + `@include mat-core();`, |
| 38 | + ]); |
| 39 | + |
| 40 | + await runMigration(); |
| 41 | + |
| 42 | + expect(splitFile(TEST_PATH)).toEqual([ |
| 43 | + `@use '@angular/material' as mat;`, |
| 44 | + `@import '@angular/material/theming';`, |
| 45 | + `@import '@angular/cdk/overlay-prebuilt.css';`, |
| 46 | + |
| 47 | + `@include mat.button-theme();`, |
| 48 | + `@include mat-core();`, |
| 49 | + ]); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should handle an arbitrary amount of whitespace', async () => { |
| 53 | + writeLines(TEST_PATH, [ |
| 54 | + `@use '~@angular/material' as mat;`, |
| 55 | + |
| 56 | + `@include mat.core();`, |
| 57 | + ]); |
| 58 | + |
| 59 | + await runMigration(); |
| 60 | + |
| 61 | + expect(splitFile(TEST_PATH)).toEqual([ |
| 62 | + `@use '@angular/material' as mat;`, |
| 63 | + |
| 64 | + `@include mat.core();`, |
| 65 | + ]); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should preserve tilde after the start', async () => { |
| 69 | + writeLines(TEST_PATH, [ |
| 70 | + `@use '~@angular/~material' as mat;`, |
| 71 | + `@import '@angular/cdk/~overlay-prebuilt.css';`, |
| 72 | + |
| 73 | + `@include mat.core();`, |
| 74 | + ]); |
| 75 | + |
| 76 | + await runMigration(); |
| 77 | + |
| 78 | + expect(splitFile(TEST_PATH)).toEqual([ |
| 79 | + `@use '@angular/~material' as mat;`, |
| 80 | + `@import '@angular/cdk/~overlay-prebuilt.css';`, |
| 81 | + |
| 82 | + `@include mat.core();`, |
| 83 | + ]); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should handle different types of quotes', async () => { |
| 87 | + writeLines(TEST_PATH, [ |
| 88 | + `@use "~@angular/material" as mat;`, |
| 89 | + `@import '~@angular/cdk/overlay-prebuilt.css';`, |
| 90 | + |
| 91 | + `@include mat.button-theme();`, |
| 92 | + `@include mat-core();`, |
| 93 | + ]); |
| 94 | + |
| 95 | + await runMigration(); |
| 96 | + |
| 97 | + expect(splitFile(TEST_PATH)).toEqual([ |
| 98 | + `@use "@angular/material" as mat;`, |
| 99 | + `@import '@angular/cdk/overlay-prebuilt.css';`, |
| 100 | + |
| 101 | + `@include mat.button-theme();`, |
| 102 | + `@include mat-core();`, |
| 103 | + ]); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should preserve the tilde in non-angular imports', async () => { |
| 107 | + writeLines(TEST_PATH, [ |
| 108 | + `@use '~@angular-momentum/material' as mat;`, |
| 109 | + `@import '~@angular-momentum/material/theming';`, |
| 110 | + `@import '@angular/cdk/overlay-prebuilt.css';`, |
| 111 | + |
| 112 | + `@include mat.button-theme();`, |
| 113 | + `@include mat-core();`, |
| 114 | + ]); |
| 115 | + |
| 116 | + await runMigration(); |
| 117 | + |
| 118 | + expect(splitFile(TEST_PATH)).toEqual([ |
| 119 | + `@use '~@angular-momentum/material' as mat;`, |
| 120 | + `@import '~@angular-momentum/material/theming';`, |
| 121 | + `@import '@angular/cdk/overlay-prebuilt.css';`, |
| 122 | + |
| 123 | + `@include mat.button-theme();`, |
| 124 | + `@include mat-core();`, |
| 125 | + ]); |
| 126 | + }); |
| 127 | + |
| 128 | +}); |
0 commit comments