Skip to content

Commit d8f1cce

Browse files
amysortommalerba
authored andcommitted
feat(material/schematics): add slider styles migrator and tests
1 parent da07c36 commit d8f1cce

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import {createTestApp, patchDevkitTreeToExposeTypeScript} from '@angular/cdk/schematics/testing';
2+
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
3+
import {createNewTestRunner, migrateComponent, THEME_FILE} from '../test-setup-helper';
4+
5+
describe('slider 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 migrateComponent('slider', 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.slider-theme($theme);
27+
`,
28+
`
29+
@use '@angular/material' as mat;
30+
$theme: ();
31+
@include mat.mdc-slider-theme($theme);
32+
@include mat.mdc-slider-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.slider-theme($theme);
43+
`,
44+
`
45+
@use '@angular/material' as arbitrary;
46+
$theme: ();
47+
@include arbitrary.mdc-slider-theme($theme);
48+
@include arbitrary.mdc-slider-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.slider-theme($light-theme);
60+
@include mat.slider-theme($dark-theme);
61+
`,
62+
`
63+
@use '@angular/material' as mat;
64+
$light-theme: ();
65+
$dark-theme: ();
66+
@include mat.mdc-slider-theme($light-theme);
67+
@include mat.mdc-slider-typography($light-theme);
68+
@include mat.mdc-slider-theme($dark-theme);
69+
@include mat.mdc-slider-typography($dark-theme);
70+
`,
71+
);
72+
});
73+
74+
it('should preserve whitespace', async () => {
75+
await runMigrationTest(
76+
`
77+
@use '@angular/material' as mat;
78+
$theme: ();
79+
80+
81+
@include mat.slider-theme($theme);
82+
83+
84+
`,
85+
`
86+
@use '@angular/material' as mat;
87+
$theme: ();
88+
89+
90+
@include mat.mdc-slider-theme($theme);
91+
@include mat.mdc-slider-typography($theme);
92+
93+
94+
`,
95+
);
96+
});
97+
});
98+
99+
describe('selector migrations', () => {
100+
it('should update the legacy mat-slider classname', async () => {
101+
await runMigrationTest(
102+
`
103+
.mat-slider {
104+
width: 100%;
105+
}
106+
`,
107+
`
108+
.mat-mdc-slider {
109+
width: 100%;
110+
}
111+
`,
112+
);
113+
});
114+
115+
it('should add comment for potentially deprecated multi-line selector', async () => {
116+
await runMigrationTest(
117+
`
118+
.some-class
119+
.mat-slider-thumb {
120+
height: 16px;
121+
}
122+
`,
123+
`
124+
/* TODO: The following rule targets internal classes of slider that may no longer apply for the MDC version. */
125+
126+
.some-class
127+
.mat-slider-thumb {
128+
height: 16px;
129+
}
130+
`,
131+
);
132+
});
133+
134+
it('should update the legacy mat-slider class and add comment for potentially deprecated selector', async () => {
135+
await runMigrationTest(
136+
`
137+
.mat-slider.some-class, .mat-slider-thumb {
138+
background-color: transparent;
139+
}
140+
`,
141+
`
142+
/* TODO: The following rule targets internal classes of slider that may no longer apply for the MDC version. */
143+
144+
.mat-mdc-slider.some-class, .mat-slider-thumb {
145+
background-color: transparent;
146+
}
147+
`,
148+
);
149+
});
150+
});
151+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 SliderStylesMigrator extends StyleMigrator {
12+
component = 'slider';
13+
14+
deprecatedPrefix = 'mat-slider';
15+
16+
mixinChanges = [
17+
{
18+
old: 'slider-theme',
19+
new: ['mdc-slider-theme', 'mdc-slider-typography'],
20+
},
21+
];
22+
23+
classChanges: ClassNameChange[] = [{old: '.mat-slider', new: '.mat-mdc-slider'}];
24+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {CheckboxStylesMigrator} from './components/checkbox/checkbox-styles';
1111
import {ProgressBarStylesMigrator} from './components/progress-bar/progress-bar-styles';
1212
import {RadioStylesMigrator} from './components/radio/radio-styles';
1313
import {SlideToggleStylesMigrator} from './components/slide-toggle/slide-toggle-styles';
14+
import {SliderStylesMigrator} from './components/slider/slider-styles';
1415
import {StyleMigrator} from './style-migrator';
1516

1617
export const MIGRATORS: StyleMigrator[] = [
@@ -19,4 +20,5 @@ export const MIGRATORS: StyleMigrator[] = [
1920
new ProgressBarStylesMigrator(),
2021
new RadioStylesMigrator(),
2122
new SlideToggleStylesMigrator(),
23+
new SliderStylesMigrator(),
2224
];

0 commit comments

Comments
 (0)