-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(material/schematics): add select styles migrator and tests
- Loading branch information
Showing
4 changed files
with
345 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
308 changes: 308 additions & 0 deletions
308
...terial/schematics/ng-generate/mdc-migration/rules/components/select/select-styles.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,308 @@ | ||
import {createTestApp, patchDevkitTreeToExposeTypeScript} from '@angular/cdk/schematics/testing'; | ||
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing'; | ||
import {createNewTestRunner, migrateComponents, THEME_FILE} from '../test-setup-helper'; | ||
|
||
describe('select styles', () => { | ||
let runner: SchematicTestRunner; | ||
let cliAppTree: UnitTestTree; | ||
|
||
async function runMigrationTest(oldFileContent: string, newFileContent: string) { | ||
cliAppTree.create(THEME_FILE, oldFileContent); | ||
const tree = await migrateComponents(['select'], runner, cliAppTree); | ||
expect(tree.readContent(THEME_FILE)).toBe(newFileContent); | ||
} | ||
|
||
beforeEach(async () => { | ||
runner = createNewTestRunner(); | ||
cliAppTree = patchDevkitTreeToExposeTypeScript(await createTestApp(runner)); | ||
}); | ||
|
||
describe('mixin migrations', () => { | ||
it('should replace the old theme with the new ones', async () => { | ||
await runMigrationTest( | ||
` | ||
@use '@angular/material' as mat; | ||
$theme: (); | ||
@include mat.select-theme($theme); | ||
`, | ||
` | ||
@use '@angular/material' as mat; | ||
$theme: (); | ||
@include mat.mdc-select-theme($theme); | ||
@include mat.mdc-select-typography($theme); | ||
@include mat.mdc-core-theme($theme); | ||
@include mat.mdc-core-typography($theme); | ||
`, | ||
); | ||
}); | ||
|
||
it('should use the correct namespace', async () => { | ||
await runMigrationTest( | ||
` | ||
@use '@angular/material' as arbitrary; | ||
$theme: (); | ||
@include arbitrary.select-theme($theme); | ||
`, | ||
` | ||
@use '@angular/material' as arbitrary; | ||
$theme: (); | ||
@include arbitrary.mdc-select-theme($theme); | ||
@include arbitrary.mdc-select-typography($theme); | ||
@include arbitrary.mdc-core-theme($theme); | ||
@include arbitrary.mdc-core-typography($theme); | ||
`, | ||
); | ||
}); | ||
|
||
it('should handle updating multiple themes', async () => { | ||
await runMigrationTest( | ||
` | ||
@use '@angular/material' as mat; | ||
$light-theme: (); | ||
$dark-theme: (); | ||
@include mat.select-theme($light-theme); | ||
@include mat.select-theme($dark-theme); | ||
`, | ||
` | ||
@use '@angular/material' as mat; | ||
$light-theme: (); | ||
$dark-theme: (); | ||
@include mat.mdc-select-theme($light-theme); | ||
@include mat.mdc-select-typography($light-theme); | ||
@include mat.mdc-core-theme($light-theme); | ||
@include mat.mdc-core-typography($light-theme); | ||
@include mat.mdc-select-theme($dark-theme); | ||
@include mat.mdc-select-typography($dark-theme); | ||
@include mat.mdc-core-theme($dark-theme); | ||
@include mat.mdc-core-typography($dark-theme); | ||
`, | ||
); | ||
}); | ||
|
||
it('should add correct theme if all-component-themes mixin included', async () => { | ||
await runMigrationTest( | ||
` | ||
@use '@angular/material' as mat; | ||
$theme: (); | ||
@include mat.all-component-themes($theme); | ||
`, | ||
` | ||
@use '@angular/material' as mat; | ||
$theme: (); | ||
@include mat.all-component-themes($theme); | ||
@include mat.mdc-select-theme($theme); | ||
@include mat.mdc-select-typography($theme); | ||
@include mat.mdc-core-theme($theme); | ||
@include mat.mdc-core-typography($theme); | ||
@include mat.mdc-input-theme($theme); | ||
@include mat.mdc-input-typography($theme); | ||
@include mat.mdc-form-field-theme($theme); | ||
@include mat.mdc-form-field-typography($theme); | ||
@include mat.mdc-autocomplete-theme($theme); | ||
@include mat.mdc-autocomplete-typography($theme); | ||
`, | ||
); | ||
}); | ||
|
||
it('should add multiple themes for multiple all-component-themes mixins', async () => { | ||
await runMigrationTest( | ||
` | ||
@use '@angular/material' as mat; | ||
$light-theme: (); | ||
$dark-theme: (); | ||
@include mat.all-component-themes($light-theme); | ||
@include mat.all-component-themes($dark-theme); | ||
`, | ||
` | ||
@use '@angular/material' as mat; | ||
$light-theme: (); | ||
$dark-theme: (); | ||
@include mat.all-component-themes($light-theme); | ||
@include mat.mdc-select-theme($light-theme); | ||
@include mat.mdc-select-typography($light-theme); | ||
@include mat.mdc-core-theme($light-theme); | ||
@include mat.mdc-core-typography($light-theme); | ||
@include mat.mdc-input-theme($light-theme); | ||
@include mat.mdc-input-typography($light-theme); | ||
@include mat.mdc-form-field-theme($light-theme); | ||
@include mat.mdc-form-field-typography($light-theme); | ||
@include mat.mdc-autocomplete-theme($light-theme); | ||
@include mat.mdc-autocomplete-typography($light-theme); | ||
@include mat.all-component-themes($dark-theme); | ||
@include mat.mdc-select-theme($dark-theme); | ||
@include mat.mdc-select-typography($dark-theme); | ||
@include mat.mdc-core-theme($dark-theme); | ||
@include mat.mdc-core-typography($dark-theme); | ||
@include mat.mdc-input-theme($dark-theme); | ||
@include mat.mdc-input-typography($dark-theme); | ||
@include mat.mdc-form-field-theme($dark-theme); | ||
@include mat.mdc-form-field-typography($dark-theme); | ||
@include mat.mdc-autocomplete-theme($dark-theme); | ||
@include mat.mdc-autocomplete-typography($dark-theme); | ||
`, | ||
); | ||
}); | ||
|
||
it('should preserve whitespace', async () => { | ||
await runMigrationTest( | ||
` | ||
@use '@angular/material' as mat; | ||
$theme: (); | ||
@include mat.select-theme($theme); | ||
`, | ||
` | ||
@use '@angular/material' as mat; | ||
$theme: (); | ||
@include mat.mdc-select-theme($theme); | ||
@include mat.mdc-select-typography($theme); | ||
@include mat.mdc-core-theme($theme); | ||
@include mat.mdc-core-typography($theme); | ||
`, | ||
); | ||
}); | ||
}); | ||
|
||
describe('selector migrations', () => { | ||
it('should update the legacy mat-select classname', async () => { | ||
await runMigrationTest( | ||
` | ||
.mat-select { | ||
padding: 16px; | ||
} | ||
`, | ||
` | ||
.mat-mdc-select { | ||
padding: 16px; | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it('should update multiple legacy classnames', async () => { | ||
await runMigrationTest( | ||
` | ||
.mat-select { | ||
padding: 16px; | ||
} | ||
.mat-option { | ||
padding: 16px; | ||
} | ||
`, | ||
` | ||
.mat-mdc-select { | ||
padding: 16px; | ||
} | ||
.mat-mdc-option { | ||
padding: 16px; | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it('should update a legacy classname w/ multiple selectors', async () => { | ||
await runMigrationTest( | ||
` | ||
.some-class.mat-select, .another-class { | ||
padding: 16px; | ||
} | ||
`, | ||
` | ||
.some-class.mat-mdc-select, .another-class { | ||
padding: 16px; | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it('should preserve the whitespace of multiple selectors', async () => { | ||
await runMigrationTest( | ||
` | ||
.some-class, | ||
.mat-select, | ||
.another-class { padding: 16px; } | ||
`, | ||
` | ||
.some-class, | ||
.mat-mdc-select, | ||
.another-class { padding: 16px; } | ||
`, | ||
); | ||
}); | ||
|
||
it('should add comment for potentially deprecated selector', async () => { | ||
await runMigrationTest( | ||
` | ||
.mat-select-value { | ||
color: red; | ||
} | ||
`, | ||
` | ||
/* TODO: The following rule targets internal classes of select that may no longer apply for the MDC version. */ | ||
.mat-select-value { | ||
color: red; | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it('should not add comment for legacy selector that also starts with deprecated prefix', async () => { | ||
await runMigrationTest( | ||
` | ||
.mat-select-panel { | ||
padding: 16px; | ||
} | ||
`, | ||
` | ||
.mat-mdc-select-panel { | ||
padding: 16px; | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it('should add comment for potentially deprecated multi-line selector', async () => { | ||
await runMigrationTest( | ||
` | ||
.some-class | ||
.mat-select-value { | ||
color: red; | ||
} | ||
`, | ||
` | ||
/* TODO: The following rule targets internal classes of select that may no longer apply for the MDC version. */ | ||
.some-class | ||
.mat-select-value { | ||
color: red; | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it('should update the legacy mat-select class and add comment for potentially deprecated selector', async () => { | ||
await runMigrationTest( | ||
` | ||
.mat-select.some-class, .mat-select-value { | ||
padding: 16px; | ||
} | ||
`, | ||
` | ||
/* TODO: The following rule targets internal classes of select that may no longer apply for the MDC version. */ | ||
.mat-mdc-select.some-class, .mat-select-value { | ||
padding: 16px; | ||
} | ||
`, | ||
); | ||
}); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
src/material/schematics/ng-generate/mdc-migration/rules/components/select/select-styles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {ClassNameChange, StyleMigrator} from '../../style-migrator'; | ||
|
||
export class SelectStylesMigrator extends StyleMigrator { | ||
component = 'select'; | ||
|
||
deprecatedPrefixes = ['mat-select', 'mat-option']; | ||
|
||
mixinChanges = [ | ||
{ | ||
old: 'select-theme', | ||
new: ['mdc-select-theme', 'mdc-select-typography', 'mdc-core-theme', 'mdc-core-typography'], | ||
}, | ||
]; | ||
|
||
classChanges: ClassNameChange[] = [ | ||
{old: '.mat-select', new: '.mat-mdc-select'}, | ||
{old: '.mat-select-panel', new: '.mat-mdc-select-panel'}, | ||
{old: '.mat-option', new: '.mat-mdc-option'}, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters