Skip to content

Commit bcacdde

Browse files
amysortommalerba
authored andcommitted
feat(material/schematics): add list styles migrator and tests
1 parent 0afbecf commit bcacdde

File tree

4 files changed

+312
-0
lines changed

4 files changed

+312
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ $sample-project-theme: mat.define-light-theme((
4646
@include mat.mdc-progress-bar-typography($sample-project-theme);
4747
@include mat.mdc-paginator-theme($sample-project-theme);
4848
@include mat.mdc-paginator-typography($sample-project-theme);
49+
@include mat.mdc-list-theme($sample-project-theme);
50+
@include mat.mdc-list-typography($sample-project-theme);
4951
@include mat.mdc-dialog-theme($sample-project-theme);
5052
@include mat.mdc-dialog-typography($sample-project-theme);
5153
@include mat.mdc-chips-theme($sample-project-theme);
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
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('list 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(['list'], 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.list-theme($theme);
27+
`,
28+
`
29+
@use '@angular/material' as mat;
30+
$theme: ();
31+
@include mat.mdc-list-theme($theme);
32+
@include mat.mdc-list-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.list-theme($theme);
43+
`,
44+
`
45+
@use '@angular/material' as arbitrary;
46+
$theme: ();
47+
@include arbitrary.mdc-list-theme($theme);
48+
@include arbitrary.mdc-list-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.list-theme($light-theme);
60+
@include mat.list-theme($dark-theme);
61+
`,
62+
`
63+
@use '@angular/material' as mat;
64+
$light-theme: ();
65+
$dark-theme: ();
66+
@include mat.mdc-list-theme($light-theme);
67+
@include mat.mdc-list-typography($light-theme);
68+
@include mat.mdc-list-theme($dark-theme);
69+
@include mat.mdc-list-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-list-theme($theme);
86+
@include mat.mdc-list-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-list-theme($light-theme);
106+
@include mat.mdc-list-typography($light-theme);
107+
@include mat.all-component-themes($dark-theme);
108+
@include mat.mdc-list-theme($dark-theme);
109+
@include mat.mdc-list-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.list-theme($theme);
122+
123+
124+
`,
125+
`
126+
@use '@angular/material' as mat;
127+
$theme: ();
128+
129+
130+
@include mat.mdc-list-theme($theme);
131+
@include mat.mdc-list-typography($theme);
132+
133+
134+
`,
135+
);
136+
});
137+
});
138+
139+
describe('selector migrations', () => {
140+
it('should update the legacy mat-list classname', async () => {
141+
await runMigrationTest(
142+
`
143+
.mat-list {
144+
padding: 25px;
145+
}
146+
`,
147+
`
148+
.mat-mdc-list {
149+
padding: 25px;
150+
}
151+
`,
152+
);
153+
});
154+
155+
it('should update multiple legacy classnames', async () => {
156+
await runMigrationTest(
157+
`
158+
.mat-list {
159+
padding: 25px;
160+
}
161+
.mat-subheader {
162+
color: red;
163+
}
164+
`,
165+
`
166+
.mat-mdc-list {
167+
padding: 25px;
168+
}
169+
.mat-mdc-subheader {
170+
color: 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-list, .another-class {
180+
padding: 25px;
181+
}
182+
`,
183+
`
184+
.some-class.mat-mdc-list, .another-class {
185+
padding: 25px;
186+
}
187+
`,
188+
);
189+
});
190+
191+
it('should preserve the whitespace of multiple selectors', async () => {
192+
await runMigrationTest(
193+
`
194+
.some-class,
195+
.mat-list,
196+
.another-class { padding: 25px; }
197+
`,
198+
`
199+
.some-class,
200+
.mat-mdc-list,
201+
.another-class { padding: 25px; }
202+
`,
203+
);
204+
});
205+
206+
it('should add comment for potentially deprecated selector', async () => {
207+
await runMigrationTest(
208+
`
209+
.mat-list-item-content {
210+
padding: 16px;
211+
}
212+
`,
213+
`
214+
/* TODO: The following rule targets internal classes of list that may no longer apply for the MDC version. */
215+
216+
.mat-list-item-content {
217+
padding: 16px;
218+
}
219+
`,
220+
);
221+
});
222+
223+
it('should not add comment for legacy selector that also starts with deprecated prefix', async () => {
224+
await runMigrationTest(
225+
`
226+
.mat-list-icon {
227+
color: red;
228+
}
229+
`,
230+
`
231+
.mat-mdc-list-item-icon {
232+
color: red;
233+
}
234+
`,
235+
);
236+
});
237+
238+
it('should add comment for potentially deprecated multi-line selector', async () => {
239+
await runMigrationTest(
240+
`
241+
.some-class
242+
.mat-list-item-content {
243+
padding: 16px;
244+
}
245+
`,
246+
`
247+
/* TODO: The following rule targets internal classes of list that may no longer apply for the MDC version. */
248+
249+
.some-class
250+
.mat-list-item-content {
251+
padding: 16px;
252+
}
253+
`,
254+
);
255+
});
256+
257+
it('should update the legacy mat-list class and add comment for potentially deprecated selector', async () => {
258+
await runMigrationTest(
259+
`
260+
.mat-list.some-class, .mat-list-item-content {
261+
padding: 16px;
262+
}
263+
`,
264+
`
265+
/* TODO: The following rule targets internal classes of list that may no longer apply for the MDC version. */
266+
267+
.mat-mdc-list.some-class, .mat-list-item-content {
268+
padding: 16px;
269+
}
270+
`,
271+
);
272+
});
273+
});
274+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 ListStylesMigrator extends StyleMigrator {
12+
component = 'list';
13+
14+
deprecatedPrefix = 'mat-list';
15+
16+
mixinChanges = [
17+
{
18+
old: 'list-theme',
19+
new: ['mdc-list-theme', 'mdc-list-typography'],
20+
},
21+
];
22+
23+
classChanges: ClassNameChange[] = [
24+
{old: `.mat-list-base`, new: `.mat-mdc-list-base`},
25+
{old: `.mat-list`, new: `.mat-mdc-list`},
26+
{old: `.mat-list-avatar`, new: `.mat-mdc-list-item-avatar`},
27+
{old: `.mat-list-icon`, new: `.mat-mdc-list-item-icon`},
28+
{old: `.mat-subheader`, new: `.mat-mdc-subheader`},
29+
{old: `.mat-list-item`, new: `.mat-mdc-list-item`},
30+
];
31+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {CardTemplateMigrator} from './components/card/card-template';
1212
import {CheckboxStylesMigrator} from './components/checkbox/checkbox-styles';
1313
import {ChipsStylesMigrator} from './components/chips/chips-styles';
1414
import {DialogStylesMigrator} from './components/dialog/dialog-styles';
15+
import {ListStylesMigrator} from './components/list/list-styles';
1516
import {PaginatorStylesMigrator} from './components/paginator/paginator-styles';
1617
import {ProgressBarStylesMigrator} from './components/progress-bar/progress-bar-styles';
1718
import {ProgressSpinnerStylesMigrator} from './components/progress-spinner/progress-spinner-styles';
@@ -51,6 +52,10 @@ export const MIGRATORS: ComponentMigrator[] = [
5152
component: 'dialog',
5253
styles: new DialogStylesMigrator(),
5354
},
55+
{
56+
component: 'list',
57+
styles: new ListStylesMigrator(),
58+
},
5459
{
5560
component: 'paginator',
5661
styles: new PaginatorStylesMigrator(),

0 commit comments

Comments
 (0)