Skip to content

Commit 84398f4

Browse files
amysortommalerba
authored andcommitted
feat(material/schematics): add table styles migrator and tests
1 parent 801c23c commit 84398f4

File tree

4 files changed

+253
-0
lines changed

4 files changed

+253
-0
lines changed

integration/mdc-migration/golden/src/styles.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ $sample-project-theme: mat.define-light-theme((
3232
// Alternatively, you can import and @include the theme mixins for each component
3333
// that you are using.
3434
@include mat.all-component-themes($sample-project-theme);
35+
@include mat.mdc-table-theme($sample-project-theme);
36+
@include mat.mdc-table-typography($sample-project-theme);
3537
@include mat.mdc-slider-theme($sample-project-theme);
3638
@include mat.mdc-slider-typography($sample-project-theme);
3739
@include mat.mdc-slide-toggle-theme($sample-project-theme);
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
import {createTestApp, patchDevkitTreeToExposeTypeScript} from '@angular/cdk/schematics/testing';
2+
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
3+
import {createNewTestRunner, migrateComponents, THEME_FILE} from '../test-setup-helper';
4+
5+
describe('table styles', () => {
6+
let runner: SchematicTestRunner;
7+
let cliAppTree: UnitTestTree;
8+
9+
async function runMigrationTest(oldFileContent: string, newFileContent: string) {
10+
cliAppTree.create(THEME_FILE, oldFileContent);
11+
const tree = await migrateComponents(['table'], runner, cliAppTree);
12+
expect(tree.readContent(THEME_FILE)).toBe(newFileContent);
13+
}
14+
15+
beforeEach(async () => {
16+
runner = createNewTestRunner();
17+
cliAppTree = patchDevkitTreeToExposeTypeScript(await createTestApp(runner));
18+
});
19+
20+
describe('mixin migrations', () => {
21+
it('should replace the old theme with the new ones', async () => {
22+
await runMigrationTest(
23+
`
24+
@use '@angular/material' as mat;
25+
$theme: ();
26+
@include mat.table-theme($theme);
27+
`,
28+
`
29+
@use '@angular/material' as mat;
30+
$theme: ();
31+
@include mat.mdc-table-theme($theme);
32+
@include mat.mdc-table-typography($theme);
33+
`,
34+
);
35+
});
36+
37+
it('should use the correct namespace', async () => {
38+
await runMigrationTest(
39+
`
40+
@use '@angular/material' as arbitrary;
41+
$theme: ();
42+
@include arbitrary.table-theme($theme);
43+
`,
44+
`
45+
@use '@angular/material' as arbitrary;
46+
$theme: ();
47+
@include arbitrary.mdc-table-theme($theme);
48+
@include arbitrary.mdc-table-typography($theme);
49+
`,
50+
);
51+
});
52+
53+
it('should handle updating multiple themes', async () => {
54+
await runMigrationTest(
55+
`
56+
@use '@angular/material' as mat;
57+
$light-theme: ();
58+
$dark-theme: ();
59+
@include mat.table-theme($light-theme);
60+
@include mat.table-theme($dark-theme);
61+
`,
62+
`
63+
@use '@angular/material' as mat;
64+
$light-theme: ();
65+
$dark-theme: ();
66+
@include mat.mdc-table-theme($light-theme);
67+
@include mat.mdc-table-typography($light-theme);
68+
@include mat.mdc-table-theme($dark-theme);
69+
@include mat.mdc-table-typography($dark-theme);
70+
`,
71+
);
72+
});
73+
74+
it('should add correct theme if all-component-themes mixin included', async () => {
75+
await runMigrationTest(
76+
`
77+
@use '@angular/material' as mat;
78+
$theme: ();
79+
@include mat.all-component-themes($theme);
80+
`,
81+
`
82+
@use '@angular/material' as mat;
83+
$theme: ();
84+
@include mat.all-component-themes($theme);
85+
@include mat.mdc-table-theme($theme);
86+
@include mat.mdc-table-typography($theme);
87+
`,
88+
);
89+
});
90+
91+
it('should add multiple themes for multiple all-component-themes mixins', async () => {
92+
await runMigrationTest(
93+
`
94+
@use '@angular/material' as mat;
95+
$light-theme: ();
96+
$dark-theme: ();
97+
@include mat.all-component-themes($light-theme);
98+
@include mat.all-component-themes($dark-theme);
99+
`,
100+
`
101+
@use '@angular/material' as mat;
102+
$light-theme: ();
103+
$dark-theme: ();
104+
@include mat.all-component-themes($light-theme);
105+
@include mat.mdc-table-theme($light-theme);
106+
@include mat.mdc-table-typography($light-theme);
107+
@include mat.all-component-themes($dark-theme);
108+
@include mat.mdc-table-theme($dark-theme);
109+
@include mat.mdc-table-typography($dark-theme);
110+
`,
111+
);
112+
});
113+
114+
it('should preserve whitespace', async () => {
115+
await runMigrationTest(
116+
`
117+
@use '@angular/material' as mat;
118+
$theme: ();
119+
120+
121+
@include mat.table-theme($theme);
122+
123+
124+
`,
125+
`
126+
@use '@angular/material' as mat;
127+
$theme: ();
128+
129+
130+
@include mat.mdc-table-theme($theme);
131+
@include mat.mdc-table-typography($theme);
132+
133+
134+
`,
135+
);
136+
});
137+
});
138+
139+
describe('selector migrations', () => {
140+
it('should update the legacy mat-table classname', async () => {
141+
await runMigrationTest(
142+
`
143+
.mat-table {
144+
padding: 4px;
145+
}
146+
`,
147+
`
148+
.mat-mdc-table {
149+
padding: 4px;
150+
}
151+
`,
152+
);
153+
});
154+
155+
it('should update multiple legacy classnames', async () => {
156+
await runMigrationTest(
157+
`
158+
.mat-header-row {
159+
background: red;
160+
}
161+
.mat-footer-row {
162+
background: red;
163+
}
164+
`,
165+
`
166+
.mat-mdc-header-row {
167+
background: red;
168+
}
169+
.mat-mdc-footer-row {
170+
background: red;
171+
}
172+
`,
173+
);
174+
});
175+
176+
it('should update a legacy classname w/ multiple selectors', async () => {
177+
await runMigrationTest(
178+
`
179+
.some-class.mat-table-sticky-border-elem-left, .another-class {
180+
border-right: 2px solid red;
181+
}
182+
`,
183+
`
184+
.some-class.mat-mdc-table-sticky-border-elem-left, .another-class {
185+
border-right: 2px solid red;
186+
}
187+
`,
188+
);
189+
});
190+
191+
it('should preserve the whitespace of multiple selectors', async () => {
192+
await runMigrationTest(
193+
`
194+
.some-class,
195+
.mat-table,
196+
.another-class { padding: 4px; }
197+
`,
198+
`
199+
.some-class,
200+
.mat-mdc-table,
201+
.another-class { padding: 4px; }
202+
`,
203+
);
204+
});
205+
});
206+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {ClassNameChange, StyleMigrator} from '../../style-migrator';
10+
11+
export class TableStylesMigrator extends StyleMigrator {
12+
component = 'table';
13+
14+
// There are no other table selectors available aside from the specified
15+
// changes below
16+
deprecatedPrefix = null;
17+
18+
mixinChanges = [
19+
{
20+
old: 'table-theme',
21+
new: ['mdc-table-theme', 'mdc-table-typography'],
22+
},
23+
];
24+
25+
classChanges: ClassNameChange[] = [
26+
{old: '.mat-table', new: '.mat-mdc-table'},
27+
{old: '.mat-table-sticky', new: '.mat-mdc-table-sticky'},
28+
{old: '.mat-header-cell', new: '.mat-mdc-header-cell'},
29+
{old: '.mat-footer-cell', new: '.mat-mdc-footer-cell'},
30+
{old: '.mat-cell', new: '.mat-mdc-cell'},
31+
{old: '.mat-header-row', new: '.mat-mdc-header-row'},
32+
{old: '.mat-footer-row', new: '.mat-mdc-footer-row'},
33+
{old: '.mat-row', new: '.mat-mdc-row'},
34+
{
35+
old: '.mat-table-sticky-border-elem-left',
36+
new: '.mat-mdc-table-sticky-border-elem-left',
37+
},
38+
{
39+
old: '.mat-table-sticky-border-elem-right',
40+
new: '.mat-mdc-table-sticky-border-elem-right',
41+
},
42+
];
43+
}

src/material/schematics/ng-generate/mdc-migration/rules/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {ProgressSpinnerStylesMigrator} from './components/progress-spinner/progr
1616
import {RadioStylesMigrator} from './components/radio/radio-styles';
1717
import {SlideToggleStylesMigrator} from './components/slide-toggle/slide-toggle-styles';
1818
import {SliderStylesMigrator} from './components/slider/slider-styles';
19+
import {TableStylesMigrator} from './components/table/table-styles';
1920
import {StyleMigrator} from './style-migrator';
2021

2122
export const MIGRATORS: StyleMigrator[] = [
@@ -29,4 +30,5 @@ export const MIGRATORS: StyleMigrator[] = [
2930
new RadioStylesMigrator(),
3031
new SlideToggleStylesMigrator(),
3132
new SliderStylesMigrator(),
33+
new TableStylesMigrator(),
3234
];

0 commit comments

Comments
 (0)