|
| 1 | +import {UnitTestTree} from '@angular-devkit/schematics/testing'; |
| 2 | +import {createTestCaseSetup} from '@angular/cdk/schematics/testing'; |
| 3 | +import {MIGRATION_PATH} from '../../paths'; |
| 4 | + |
| 5 | +const THEME_FILE_PATH = '/projects/cdk-testing/src/theme.scss'; |
| 6 | + |
| 7 | +describe('v19 explicit system variable prefix migration', () => { |
| 8 | + let tree: UnitTestTree; |
| 9 | + let writeFile: (filename: string, content: string) => void; |
| 10 | + let runMigration: () => Promise<unknown>; |
| 11 | + |
| 12 | + function stripWhitespace(content: string): string { |
| 13 | + return content.replace(/\s/g, ''); |
| 14 | + } |
| 15 | + |
| 16 | + beforeEach(async () => { |
| 17 | + const testSetup = await createTestCaseSetup('migration-v19', MIGRATION_PATH, []); |
| 18 | + tree = testSetup.appTree; |
| 19 | + writeFile = testSetup.writeFile; |
| 20 | + runMigration = testSetup.runFixers; |
| 21 | + }); |
| 22 | + |
| 23 | + it('should add an explicit system variables prefix', async () => { |
| 24 | + writeFile( |
| 25 | + THEME_FILE_PATH, |
| 26 | + ` |
| 27 | + @use '@angular/material' as mat; |
| 28 | +
|
| 29 | + $theme: mat.define-theme(( |
| 30 | + color: ( |
| 31 | + theme-type: 'light', |
| 32 | + primary: mat.$azure-palette, |
| 33 | + tertiary: mat.$red-palette, |
| 34 | + use-system-variables: true |
| 35 | + ), |
| 36 | + typography: ( |
| 37 | + use-system-variables: true |
| 38 | + ), |
| 39 | + density: ( |
| 40 | + scale: -1 |
| 41 | + ), |
| 42 | + )); |
| 43 | +
|
| 44 | + @include mat.all-component-themes($theme); |
| 45 | + `, |
| 46 | + ); |
| 47 | + |
| 48 | + await runMigration(); |
| 49 | + |
| 50 | + expect(stripWhitespace(tree.readText(THEME_FILE_PATH))).toBe( |
| 51 | + stripWhitespace(` |
| 52 | + @use '@angular/material' as mat; |
| 53 | +
|
| 54 | + $theme: mat.define-theme(( |
| 55 | + color: ( |
| 56 | + theme-type: 'light', |
| 57 | + primary: mat.$azure-palette, |
| 58 | + tertiary: mat.$red-palette, |
| 59 | + use-system-variables: true, |
| 60 | + system-variables-prefix: sys, |
| 61 | + ), |
| 62 | + typography: ( |
| 63 | + use-system-variables: true, |
| 64 | + system-variables-prefix: sys, |
| 65 | + ), |
| 66 | + density: ( |
| 67 | + scale: -1 |
| 68 | + ), |
| 69 | + )); |
| 70 | +
|
| 71 | + @include mat.all-component-themes($theme); |
| 72 | + `), |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should add an explicit system variables prefix if the value is using trailing commas', async () => { |
| 77 | + writeFile( |
| 78 | + THEME_FILE_PATH, |
| 79 | + ` |
| 80 | + @use '@angular/material' as mat; |
| 81 | +
|
| 82 | + $theme: mat.define-theme(( |
| 83 | + color: ( |
| 84 | + theme-type: 'light', |
| 85 | + primary: mat.$azure-palette, |
| 86 | + tertiary: mat.$red-palette, |
| 87 | + use-system-variables: true, |
| 88 | + ), |
| 89 | + typography: ( |
| 90 | + use-system-variables: true, |
| 91 | + ), |
| 92 | + density: ( |
| 93 | + scale: -1 |
| 94 | + ), |
| 95 | + )); |
| 96 | +
|
| 97 | + @include mat.all-component-themes($theme); |
| 98 | + `, |
| 99 | + ); |
| 100 | + |
| 101 | + await runMigration(); |
| 102 | + |
| 103 | + expect(stripWhitespace(tree.readText(THEME_FILE_PATH))).toBe( |
| 104 | + stripWhitespace(` |
| 105 | + @use '@angular/material' as mat; |
| 106 | +
|
| 107 | + $theme: mat.define-theme(( |
| 108 | + color: ( |
| 109 | + theme-type: 'light', |
| 110 | + primary: mat.$azure-palette, |
| 111 | + tertiary: mat.$red-palette, |
| 112 | + use-system-variables: true, |
| 113 | + system-variables-prefix: sys, |
| 114 | + ), |
| 115 | + typography: ( |
| 116 | + use-system-variables: true, |
| 117 | + system-variables-prefix: sys, |
| 118 | + ), |
| 119 | + density: ( |
| 120 | + scale: -1 |
| 121 | + ), |
| 122 | + )); |
| 123 | +
|
| 124 | + @include mat.all-component-themes($theme); |
| 125 | + `), |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should not add an explicit system variables prefix if the map has one already', async () => { |
| 130 | + writeFile( |
| 131 | + THEME_FILE_PATH, |
| 132 | + ` |
| 133 | + @use '@angular/material' as mat; |
| 134 | +
|
| 135 | + $theme: mat.define-theme(( |
| 136 | + color: ( |
| 137 | + theme-type: 'light', |
| 138 | + primary: mat.$azure-palette, |
| 139 | + tertiary: mat.$red-palette, |
| 140 | + use-system-variables: true |
| 141 | + ), |
| 142 | + typography: ( |
| 143 | + use-system-variables: true, |
| 144 | + system-variables-prefix: foo |
| 145 | + ), |
| 146 | + density: ( |
| 147 | + scale: -1 |
| 148 | + ), |
| 149 | + )); |
| 150 | +
|
| 151 | + @include mat.all-component-themes($theme); |
| 152 | + `, |
| 153 | + ); |
| 154 | + |
| 155 | + await runMigration(); |
| 156 | + |
| 157 | + expect(stripWhitespace(tree.readText(THEME_FILE_PATH))).toBe( |
| 158 | + stripWhitespace(` |
| 159 | + @use '@angular/material' as mat; |
| 160 | +
|
| 161 | + $theme: mat.define-theme(( |
| 162 | + color: ( |
| 163 | + theme-type: 'light', |
| 164 | + primary: mat.$azure-palette, |
| 165 | + tertiary: mat.$red-palette, |
| 166 | + use-system-variables: true, |
| 167 | + system-variables-prefix: sys, |
| 168 | + ), |
| 169 | + typography: ( |
| 170 | + use-system-variables: true, |
| 171 | + system-variables-prefix: foo |
| 172 | + ), |
| 173 | + density: ( |
| 174 | + scale: -1 |
| 175 | + ), |
| 176 | + )); |
| 177 | +
|
| 178 | + @include mat.all-component-themes($theme); |
| 179 | + `), |
| 180 | + ); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should handle a single-line map', async () => { |
| 184 | + writeFile( |
| 185 | + THEME_FILE_PATH, |
| 186 | + ` |
| 187 | + @use '@angular/material' as mat; |
| 188 | +
|
| 189 | + $theme: mat.define-theme(( |
| 190 | + color: (theme-type: 'light', primary: mat.$azure-palette, use-system-variables: true), |
| 191 | + typography: (use-system-variables: true), |
| 192 | + density: (scale: -1), |
| 193 | + )); |
| 194 | +
|
| 195 | + @include mat.all-component-themes($theme); |
| 196 | + `, |
| 197 | + ); |
| 198 | + |
| 199 | + await runMigration(); |
| 200 | + |
| 201 | + expect(stripWhitespace(tree.readText(THEME_FILE_PATH))).toBe( |
| 202 | + stripWhitespace(` |
| 203 | + @use '@angular/material' as mat; |
| 204 | +
|
| 205 | + $theme: mat.define-theme(( |
| 206 | + color: (theme-type: 'light', primary: mat.$azure-palette, use-system-variables: true, system-variables-prefix: sys,), |
| 207 | + typography: (use-system-variables: true, system-variables-prefix: sys,), |
| 208 | + density: (scale: -1), |
| 209 | + )); |
| 210 | +
|
| 211 | + @include mat.all-component-themes($theme); |
| 212 | + `), |
| 213 | + ); |
| 214 | + }); |
| 215 | +}); |
0 commit comments