From 0108de5afff1dc3ec5b8f7a8b5cba2f2b4dd1436 Mon Sep 17 00:00:00 2001 From: Amy Sorto <8575252+amysorto@users.noreply.github.com> Date: Thu, 10 Mar 2022 00:13:08 +0000 Subject: [PATCH] feat(material/schematics): add chips styles migrator and tests --- .../mdc-migration/golden/src/styles.scss | 2 + .../components/chips/chips-styles.spec.ts | 274 ++++++++++++++++++ .../rules/components/chips/chips-styles.ts | 31 ++ .../ng-generate/mdc-migration/rules/index.ts | 2 + 4 files changed, 309 insertions(+) create mode 100644 src/material/schematics/ng-generate/mdc-migration/rules/components/chips/chips-styles.spec.ts create mode 100644 src/material/schematics/ng-generate/mdc-migration/rules/components/chips/chips-styles.ts diff --git a/integration/mdc-migration/golden/src/styles.scss b/integration/mdc-migration/golden/src/styles.scss index baaee73b528e..1f2234162fca 100644 --- a/integration/mdc-migration/golden/src/styles.scss +++ b/integration/mdc-migration/golden/src/styles.scss @@ -48,6 +48,8 @@ $sample-project-theme: mat.define-light-theme(( @include mat.mdc-paginator-typography($sample-project-theme); @include mat.mdc-dialog-theme($sample-project-theme); @include mat.mdc-dialog-typography($sample-project-theme); +@include mat.mdc-chips-theme($sample-project-theme); +@include mat.mdc-chips-typography($sample-project-theme); @include mat.mdc-checkbox-theme($sample-project-theme); @include mat.mdc-checkbox-typography($sample-project-theme); @include mat.mdc-card-theme($sample-project-theme); diff --git a/src/material/schematics/ng-generate/mdc-migration/rules/components/chips/chips-styles.spec.ts b/src/material/schematics/ng-generate/mdc-migration/rules/components/chips/chips-styles.spec.ts new file mode 100644 index 000000000000..cfc6bb48b39a --- /dev/null +++ b/src/material/schematics/ng-generate/mdc-migration/rules/components/chips/chips-styles.spec.ts @@ -0,0 +1,274 @@ +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('chips styles', () => { + let runner: SchematicTestRunner; + let cliAppTree: UnitTestTree; + + async function runMigrationTest(oldFileContent: string, newFileContent: string) { + cliAppTree.create(THEME_FILE, oldFileContent); + const tree = await migrateComponents(['chips'], 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.chips-theme($theme); + `, + ` + @use '@angular/material' as mat; + $theme: (); + @include mat.mdc-chips-theme($theme); + @include mat.mdc-chips-typography($theme); + `, + ); + }); + + it('should use the correct namespace', async () => { + await runMigrationTest( + ` + @use '@angular/material' as arbitrary; + $theme: (); + @include arbitrary.chips-theme($theme); + `, + ` + @use '@angular/material' as arbitrary; + $theme: (); + @include arbitrary.mdc-chips-theme($theme); + @include arbitrary.mdc-chips-typography($theme); + `, + ); + }); + + it('should handle updating multiple themes', async () => { + await runMigrationTest( + ` + @use '@angular/material' as mat; + $light-theme: (); + $dark-theme: (); + @include mat.chips-theme($light-theme); + @include mat.chips-theme($dark-theme); + `, + ` + @use '@angular/material' as mat; + $light-theme: (); + $dark-theme: (); + @include mat.mdc-chips-theme($light-theme); + @include mat.mdc-chips-typography($light-theme); + @include mat.mdc-chips-theme($dark-theme); + @include mat.mdc-chips-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-chips-theme($theme); + @include mat.mdc-chips-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-chips-theme($light-theme); + @include mat.mdc-chips-typography($light-theme); + @include mat.all-component-themes($dark-theme); + @include mat.mdc-chips-theme($dark-theme); + @include mat.mdc-chips-typography($dark-theme); + `, + ); + }); + + it('should preserve whitespace', async () => { + await runMigrationTest( + ` + @use '@angular/material' as mat; + $theme: (); + + + @include mat.chips-theme($theme); + + + `, + ` + @use '@angular/material' as mat; + $theme: (); + + + @include mat.mdc-chips-theme($theme); + @include mat.mdc-chips-typography($theme); + + + `, + ); + }); + }); + + describe('selector migrations', () => { + it('should update the legacy mat-chip classname', async () => { + await runMigrationTest( + ` + .mat-chip { + padding: 50px; + } + `, + ` + .mat-mdc-chip { + padding: 50px; + } + `, + ); + }); + + it('should update multiple legacy classnames', async () => { + await runMigrationTest( + ` + .mat-chip { + height: 50px; + } + .mat-basic-chip { + background: red; + } + `, + ` + .mat-mdc-chip { + height: 50px; + } + .mat-mdc-basic-chip { + background: red; + } + `, + ); + }); + + it('should update a legacy classname w/ multiple selectors', async () => { + await runMigrationTest( + ` + .some-class.mat-chip, .another-class { + height: 50px; + } + `, + ` + .some-class.mat-mdc-chip, .another-class { + height: 50px; + } + `, + ); + }); + + it('should preserve the whitespace of multiple selectors', async () => { + await runMigrationTest( + ` + .some-class, + .mat-chip, + .another-class { height: 50px; } + `, + ` + .some-class, + .mat-mdc-chip, + .another-class { height: 50px; } + `, + ); + }); + + it('should add comment for potentially deprecated selector', async () => { + await runMigrationTest( + ` + .mat-chip-avatar { + border-radius: 4px; + } + `, + ` + /* TODO: The following rule targets internal classes of chips that may no longer apply for the MDC version. */ + + .mat-chip-avatar { + border-radius: 4px; + } + `, + ); + }); + + it('should not add comment for legacy selector that also starts with deprecated prefix', async () => { + await runMigrationTest( + ` + .mat-chip-grid { + padding: 12px; + } + `, + ` + .mat-mdc-chip-grid { + padding: 12px; + } + `, + ); + }); + + it('should add comment for potentially deprecated multi-line selector', async () => { + await runMigrationTest( + ` + .some-class + .mat-chip-avatar { + border-radius: 4px; + } + `, + ` + /* TODO: The following rule targets internal classes of chips that may no longer apply for the MDC version. */ + + .some-class + .mat-chip-avatar { + border-radius: 4px; + } + `, + ); + }); + + it('should update the legacy mat-chip class and add comment for potentially deprecated selector', async () => { + await runMigrationTest( + ` + .mat-chip.some-class, .mat-chip-avatar { + border-radius: 4px; + } + `, + ` + /* TODO: The following rule targets internal classes of chips that may no longer apply for the MDC version. */ + + .mat-mdc-chip.some-class, .mat-chip-avatar { + border-radius: 4px; + } + `, + ); + }); + }); +}); diff --git a/src/material/schematics/ng-generate/mdc-migration/rules/components/chips/chips-styles.ts b/src/material/schematics/ng-generate/mdc-migration/rules/components/chips/chips-styles.ts new file mode 100644 index 000000000000..6a03ea394213 --- /dev/null +++ b/src/material/schematics/ng-generate/mdc-migration/rules/components/chips/chips-styles.ts @@ -0,0 +1,31 @@ +/** + * @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 ChipsStylesMigrator extends StyleMigrator { + component = 'chips'; + + deprecatedPrefix = 'mat-chip'; + + mixinChanges = [ + { + old: 'chips-theme', + new: ['mdc-chips-theme', 'mdc-chips-typography'], + }, + ]; + + classChanges: ClassNameChange[] = [ + {old: '.mat-chip-set', new: '.mat-mdc-chip-set'}, + {old: '.mat-chip-grid', new: '.mat-mdc-chip-grid'}, + {old: '.mat-chip', new: '.mat-mdc-chip'}, + {old: '.mat-basic-chip', new: '.mat-mdc-basic-chip'}, + {old: '.mat-standard-chip', new: '.mat-mdc-standard-chip'}, + {old: '.mat-chip-input', new: '.mat-mdc-chip-input'}, + ]; +} diff --git a/src/material/schematics/ng-generate/mdc-migration/rules/index.ts b/src/material/schematics/ng-generate/mdc-migration/rules/index.ts index a262fd2e7f09..e5916604b6da 100644 --- a/src/material/schematics/ng-generate/mdc-migration/rules/index.ts +++ b/src/material/schematics/ng-generate/mdc-migration/rules/index.ts @@ -9,6 +9,7 @@ import {ButtonStylesMigrator} from './components/button/button-styles'; import {CardStylesMigrator} from './components/card/card-styles'; import {CheckboxStylesMigrator} from './components/checkbox/checkbox-styles'; +import {ChipsStylesMigrator} from './components/chips/chips-styles'; import {DialogStylesMigrator} from './components/dialog/dialog-styles'; import {PaginatorStylesMigrator} from './components/paginator/paginator-styles'; import {ProgressBarStylesMigrator} from './components/progress-bar/progress-bar-styles'; @@ -23,6 +24,7 @@ export const MIGRATORS: StyleMigrator[] = [ new ButtonStylesMigrator(), new CardStylesMigrator(), new CheckboxStylesMigrator(), + new ChipsStylesMigrator(), new DialogStylesMigrator(), new PaginatorStylesMigrator(), new ProgressBarStylesMigrator(),