-
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 table styles migrator and tests
- Loading branch information
Showing
4 changed files
with
253 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
206 changes: 206 additions & 0 deletions
206
...material/schematics/ng-generate/mdc-migration/rules/components/table/table-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,206 @@ | ||
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('table styles', () => { | ||
let runner: SchematicTestRunner; | ||
let cliAppTree: UnitTestTree; | ||
|
||
async function runMigrationTest(oldFileContent: string, newFileContent: string) { | ||
cliAppTree.create(THEME_FILE, oldFileContent); | ||
const tree = await migrateComponents(['table'], 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.table-theme($theme); | ||
`, | ||
` | ||
@use '@angular/material' as mat; | ||
$theme: (); | ||
@include mat.mdc-table-theme($theme); | ||
@include mat.mdc-table-typography($theme); | ||
`, | ||
); | ||
}); | ||
|
||
it('should use the correct namespace', async () => { | ||
await runMigrationTest( | ||
` | ||
@use '@angular/material' as arbitrary; | ||
$theme: (); | ||
@include arbitrary.table-theme($theme); | ||
`, | ||
` | ||
@use '@angular/material' as arbitrary; | ||
$theme: (); | ||
@include arbitrary.mdc-table-theme($theme); | ||
@include arbitrary.mdc-table-typography($theme); | ||
`, | ||
); | ||
}); | ||
|
||
it('should handle updating multiple themes', async () => { | ||
await runMigrationTest( | ||
` | ||
@use '@angular/material' as mat; | ||
$light-theme: (); | ||
$dark-theme: (); | ||
@include mat.table-theme($light-theme); | ||
@include mat.table-theme($dark-theme); | ||
`, | ||
` | ||
@use '@angular/material' as mat; | ||
$light-theme: (); | ||
$dark-theme: (); | ||
@include mat.mdc-table-theme($light-theme); | ||
@include mat.mdc-table-typography($light-theme); | ||
@include mat.mdc-table-theme($dark-theme); | ||
@include mat.mdc-table-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-table-theme($theme); | ||
@include mat.mdc-table-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-table-theme($light-theme); | ||
@include mat.mdc-table-typography($light-theme); | ||
@include mat.all-component-themes($dark-theme); | ||
@include mat.mdc-table-theme($dark-theme); | ||
@include mat.mdc-table-typography($dark-theme); | ||
`, | ||
); | ||
}); | ||
|
||
it('should preserve whitespace', async () => { | ||
await runMigrationTest( | ||
` | ||
@use '@angular/material' as mat; | ||
$theme: (); | ||
@include mat.table-theme($theme); | ||
`, | ||
` | ||
@use '@angular/material' as mat; | ||
$theme: (); | ||
@include mat.mdc-table-theme($theme); | ||
@include mat.mdc-table-typography($theme); | ||
`, | ||
); | ||
}); | ||
}); | ||
|
||
describe('selector migrations', () => { | ||
it('should update the legacy mat-table classname', async () => { | ||
await runMigrationTest( | ||
` | ||
.mat-table { | ||
padding: 4px; | ||
} | ||
`, | ||
` | ||
.mat-mdc-table { | ||
padding: 4px; | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it('should update multiple legacy classnames', async () => { | ||
await runMigrationTest( | ||
` | ||
.mat-header-row { | ||
background: red; | ||
} | ||
.mat-footer-row { | ||
background: red; | ||
} | ||
`, | ||
` | ||
.mat-mdc-header-row { | ||
background: red; | ||
} | ||
.mat-mdc-footer-row { | ||
background: red; | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it('should update a legacy classname w/ multiple selectors', async () => { | ||
await runMigrationTest( | ||
` | ||
.some-class.mat-table-sticky-border-elem-left, .another-class { | ||
border-right: 2px solid red; | ||
} | ||
`, | ||
` | ||
.some-class.mat-mdc-table-sticky-border-elem-left, .another-class { | ||
border-right: 2px solid red; | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it('should preserve the whitespace of multiple selectors', async () => { | ||
await runMigrationTest( | ||
` | ||
.some-class, | ||
.mat-table, | ||
.another-class { padding: 4px; } | ||
`, | ||
` | ||
.some-class, | ||
.mat-mdc-table, | ||
.another-class { padding: 4px; } | ||
`, | ||
); | ||
}); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
src/material/schematics/ng-generate/mdc-migration/rules/components/table/table-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,43 @@ | ||
/** | ||
* @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 TableStylesMigrator extends StyleMigrator { | ||
component = 'table'; | ||
|
||
// There are no other table selectors available aside from the specified | ||
// changes below | ||
deprecatedPrefix = null; | ||
|
||
mixinChanges = [ | ||
{ | ||
old: 'table-theme', | ||
new: ['mdc-table-theme', 'mdc-table-typography'], | ||
}, | ||
]; | ||
|
||
classChanges: ClassNameChange[] = [ | ||
{old: '.mat-table', new: '.mat-mdc-table'}, | ||
{old: '.mat-table-sticky', new: '.mat-mdc-table-sticky'}, | ||
{old: '.mat-header-cell', new: '.mat-mdc-header-cell'}, | ||
{old: '.mat-footer-cell', new: '.mat-mdc-footer-cell'}, | ||
{old: '.mat-cell', new: '.mat-mdc-cell'}, | ||
{old: '.mat-header-row', new: '.mat-mdc-header-row'}, | ||
{old: '.mat-footer-row', new: '.mat-mdc-footer-row'}, | ||
{old: '.mat-row', new: '.mat-mdc-row'}, | ||
{ | ||
old: '.mat-table-sticky-border-elem-left', | ||
new: '.mat-mdc-table-sticky-border-elem-left', | ||
}, | ||
{ | ||
old: '.mat-table-sticky-border-elem-right', | ||
new: '.mat-mdc-table-sticky-border-elem-right', | ||
}, | ||
]; | ||
} |
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