Skip to content

Commit ee5a836

Browse files
amysortommalerba
authored andcommitted
feat(material/schematics): add checkbox styles migrator and tests
1 parent 7205fb4 commit ee5a836

File tree

6 files changed

+147
-6
lines changed

6 files changed

+147
-6
lines changed

src/material/schematics/ng-generate/mdc-migration/BUILD.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ ts_library(
1111
name = "mdc_migration_lib",
1212
srcs = glob(
1313
["**/*.ts"],
14-
exclude = ["**/*.spec.ts"],
14+
exclude = ["**/*.spec.ts"] + ["rules/components/test-setup-helper.ts"],
1515
),
1616
deps = [
1717
"//src/cdk/schematics",
1818
"@npm//@angular-devkit/schematics",
19-
"@npm//@bazel/runfiles",
2019
"@npm//@types/node",
2120
"@npm//postcss",
2221
"@npm//postcss-scss",
@@ -51,12 +50,13 @@ pkg_npm(
5150
ts_library(
5251
name = "unit_tests_lib",
5352
testonly = True,
54-
srcs = glob(["**/*.spec.ts"]),
53+
srcs = glob(["**/*.spec.ts"] + ["rules/components/test-setup-helper.ts"]),
5554
deps = [
5655
":mdc_migration_lib",
5756
"//src/cdk/schematics/testing",
5857
"@npm//@angular-devkit/core",
5958
"@npm//@angular-devkit/schematics",
59+
"@npm//@bazel/runfiles",
6060
"@npm//@types/jasmine",
6161
"@npm//@types/node",
6262
],

src/material/schematics/ng-generate/mdc-migration/rules/components/button/button-styles.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {createTestApp, patchDevkitTreeToExposeTypeScript} from '@angular/cdk/schematics/testing';
22
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
3-
import {createNewTestRunner, migrateComponent, THEME_FILE} from '../util';
3+
import {createNewTestRunner, migrateComponent, THEME_FILE} from '../test-setup-helper';
44

55
describe('button styles', () => {
66
let runner: SchematicTestRunner;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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('checkbox 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('checkbox', 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.checkbox-theme($theme);
27+
`,
28+
`
29+
@use '@angular/material' as mat;
30+
$theme: ();
31+
@include mat.mdc-checkbox-theme($theme);
32+
@include mat.mdc-checkbox-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.checkbox-theme($theme);
43+
`,
44+
`
45+
@use '@angular/material' as arbitrary;
46+
$theme: ();
47+
@include arbitrary.mdc-checkbox-theme($theme);
48+
@include arbitrary.mdc-checkbox-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.checkbox-theme($light-theme);
60+
@include mat.checkbox-theme($dark-theme);
61+
`,
62+
`
63+
@use '@angular/material' as mat;
64+
$light-theme: ();
65+
$dark-theme: ();
66+
@include mat.mdc-checkbox-theme($light-theme);
67+
@include mat.mdc-checkbox-typography($light-theme);
68+
@include mat.mdc-checkbox-theme($dark-theme);
69+
@include mat.mdc-checkbox-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.checkbox-theme($theme);
82+
83+
84+
`,
85+
`
86+
@use '@angular/material' as mat;
87+
$theme: ();
88+
89+
90+
@include mat.mdc-checkbox-theme($theme);
91+
@include mat.mdc-checkbox-typography($theme);
92+
93+
94+
`,
95+
);
96+
});
97+
});
98+
99+
describe('selector migrations', () => {
100+
it('should update the legacy mat-checkbox classname', async () => {
101+
await runMigrationTest(
102+
`
103+
.mat-checkbox {
104+
padding-right: 4px;
105+
}
106+
`,
107+
`
108+
.mat-mdc-checkbox {
109+
padding-right: 4px;
110+
}
111+
`,
112+
);
113+
});
114+
});
115+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 CheckboxStylesMigrator extends StyleMigrator {
12+
component = 'checkbox';
13+
14+
mixinChanges = [
15+
{
16+
old: 'checkbox-theme',
17+
new: ['mdc-checkbox-theme', 'mdc-checkbox-typography'],
18+
},
19+
];
20+
21+
classChanges: ClassNameChange[] = [{old: '.mat-checkbox', new: '.mat-mdc-checkbox'}];
22+
}

src/material/schematics/ng-generate/mdc-migration/rules/components/util.ts renamed to src/material/schematics/ng-generate/mdc-migration/rules/components/test-setup-helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ export async function migrateComponent(
2626
tree: UnitTestTree,
2727
): Promise<UnitTestTree> {
2828
return await runner
29-
.runSchematicAsync('mdcMigration', {TS_CONFIG, components: [component]}, tree)
29+
.runSchematicAsync('mdcMigration', {tsconfig: TS_CONFIG, components: [component]}, tree)
3030
.toPromise();
3131
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
*/
88

99
import {ButtonStylesMigrator} from './components/button/button-styles';
10+
import {CheckboxStylesMigrator} from './components/checkbox/checkbox-styles';
1011
import {StyleMigrator} from './style-migrator';
1112

12-
export const MIGRATORS: StyleMigrator[] = [new ButtonStylesMigrator()];
13+
export const MIGRATORS: StyleMigrator[] = [
14+
new ButtonStylesMigrator(),
15+
new CheckboxStylesMigrator(),
16+
];

0 commit comments

Comments
 (0)