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 slide toggle styles migrator and tests
- Loading branch information
Showing
3 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
...atics/ng-generate/mdc-migration/rules/components/slide-toggle/slide-toggle-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,168 @@ | ||
import {createTestApp, patchDevkitTreeToExposeTypeScript} from '@angular/cdk/schematics/testing'; | ||
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing'; | ||
import {createNewTestRunner, migrateComponent, THEME_FILE} from '../test-setup-helper'; | ||
|
||
describe('slide-toggle styles', () => { | ||
let runner: SchematicTestRunner; | ||
let cliAppTree: UnitTestTree; | ||
|
||
async function runMigrationTest(oldFileContent: string, newFileContent: string) { | ||
cliAppTree.create(THEME_FILE, oldFileContent); | ||
const tree = await migrateComponent('slide-toggle', 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.slide-toggle-theme($theme); | ||
`, | ||
` | ||
@use '@angular/material' as mat; | ||
$theme: (); | ||
@include mat.mdc-slide-toggle-theme($theme); | ||
@include mat.mdc-slide-toggle-typography($theme); | ||
`, | ||
); | ||
}); | ||
|
||
it('should use the correct namespace', async () => { | ||
await runMigrationTest( | ||
` | ||
@use '@angular/material' as arbitrary; | ||
$theme: (); | ||
@include arbitrary.slide-toggle-theme($theme); | ||
`, | ||
` | ||
@use '@angular/material' as arbitrary; | ||
$theme: (); | ||
@include arbitrary.mdc-slide-toggle-theme($theme); | ||
@include arbitrary.mdc-slide-toggle-typography($theme); | ||
`, | ||
); | ||
}); | ||
|
||
it('should handle updating multiple themes', async () => { | ||
await runMigrationTest( | ||
` | ||
@use '@angular/material' as mat; | ||
$light-theme: (); | ||
$dark-theme: (); | ||
@include mat.slide-toggle-theme($light-theme); | ||
@include mat.slide-toggle-theme($dark-theme); | ||
`, | ||
` | ||
@use '@angular/material' as mat; | ||
$light-theme: (); | ||
$dark-theme: (); | ||
@include mat.mdc-slide-toggle-theme($light-theme); | ||
@include mat.mdc-slide-toggle-typography($light-theme); | ||
@include mat.mdc-slide-toggle-theme($dark-theme); | ||
@include mat.mdc-slide-toggle-typography($dark-theme); | ||
`, | ||
); | ||
}); | ||
|
||
it('should preserve whitespace', async () => { | ||
await runMigrationTest( | ||
` | ||
@use '@angular/material' as mat; | ||
$theme: (); | ||
@include mat.slide-toggle-theme($theme); | ||
`, | ||
` | ||
@use '@angular/material' as mat; | ||
$theme: (); | ||
@include mat.mdc-slide-toggle-theme($theme); | ||
@include mat.mdc-slide-toggle-typography($theme); | ||
`, | ||
); | ||
}); | ||
}); | ||
|
||
describe('selector migrations', () => { | ||
it('should update the legacy mat-slide-toggle classname', async () => { | ||
await runMigrationTest( | ||
` | ||
.mat-slide-toggle { | ||
padding: 4px; | ||
} | ||
`, | ||
` | ||
.mat-mdc-slide-toggle { | ||
padding: 4px; | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it('should add comment for potentially deprecated selector', async () => { | ||
await runMigrationTest( | ||
` | ||
.mat-slide-toggle-thumb { | ||
height: 16px; | ||
} | ||
`, | ||
` | ||
/* TODO: The following rule targets internal classes of slide-toggle that may no longer apply for the MDC version. */ | ||
.mat-slide-toggle-thumb { | ||
height: 16px; | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it('should add comment for potentially deprecated multi-line selector', async () => { | ||
await runMigrationTest( | ||
` | ||
.some-class | ||
.mat-slide-toggle-thumb { | ||
height: 16px; | ||
} | ||
`, | ||
` | ||
/* TODO: The following rule targets internal classes of slide-toggle that may no longer apply for the MDC version. */ | ||
.some-class | ||
.mat-slide-toggle-thumb { | ||
height: 16px; | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it('should update the legacy mat-slide-toggle class and add comment for potentially deprecated selector', async () => { | ||
await runMigrationTest( | ||
` | ||
.mat-slide-toggle.some-class, .mat-slide-toggle-thumb { | ||
background-color: transparent; | ||
} | ||
`, | ||
` | ||
/* TODO: The following rule targets internal classes of slide-toggle that may no longer apply for the MDC version. */ | ||
.mat-mdc-slide-toggle.some-class, .mat-slide-toggle-thumb { | ||
background-color: transparent; | ||
} | ||
`, | ||
); | ||
}); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
...schematics/ng-generate/mdc-migration/rules/components/slide-toggle/slide-toggle-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,24 @@ | ||
/** | ||
* @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 SlideToggleStylesMigrator extends StyleMigrator { | ||
component = 'slide-toggle'; | ||
|
||
deprecatedPrefix = 'mat-slide-toggle'; | ||
|
||
mixinChanges = [ | ||
{ | ||
old: 'slide-toggle-theme', | ||
new: ['mdc-slide-toggle-theme', 'mdc-slide-toggle-typography'], | ||
}, | ||
]; | ||
|
||
classChanges: ClassNameChange[] = [{old: '.mat-slide-toggle', new: '.mat-mdc-slide-toggle'}]; | ||
} |
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