Skip to content

Commit

Permalink
feat(material/schematics): add slide toggle styles migrator and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amysorto authored and mmalerba committed Jul 15, 2022
1 parent e856da1 commit 7646c73
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
@@ -0,0 +1,168 @@
import {createTestApp, patchDevkitTreeToExposeTypeScript} from '@angular/cdk/schematics/testing';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
import {createNewTestRunner, migrateComponent, THEME_FILE} from '../test-setup-helper';

describe('slide-toggle styles', () => {
let runner: SchematicTestRunner;
let cliAppTree: UnitTestTree;

async function runMigrationTest(oldFileContent: string, newFileContent: string) {
cliAppTree.create(THEME_FILE, oldFileContent);
const tree = await migrateComponent('slide-toggle', 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.slide-toggle-theme($theme);
`,
`
@use '@angular/material' as mat;
$theme: ();
@include mat.mdc-slide-toggle-theme($theme);
@include mat.mdc-slide-toggle-typography($theme);
`,
);
});

it('should use the correct namespace', async () => {
await runMigrationTest(
`
@use '@angular/material' as arbitrary;
$theme: ();
@include arbitrary.slide-toggle-theme($theme);
`,
`
@use '@angular/material' as arbitrary;
$theme: ();
@include arbitrary.mdc-slide-toggle-theme($theme);
@include arbitrary.mdc-slide-toggle-typography($theme);
`,
);
});

it('should handle updating multiple themes', async () => {
await runMigrationTest(
`
@use '@angular/material' as mat;
$light-theme: ();
$dark-theme: ();
@include mat.slide-toggle-theme($light-theme);
@include mat.slide-toggle-theme($dark-theme);
`,
`
@use '@angular/material' as mat;
$light-theme: ();
$dark-theme: ();
@include mat.mdc-slide-toggle-theme($light-theme);
@include mat.mdc-slide-toggle-typography($light-theme);
@include mat.mdc-slide-toggle-theme($dark-theme);
@include mat.mdc-slide-toggle-typography($dark-theme);
`,
);
});

it('should preserve whitespace', async () => {
await runMigrationTest(
`
@use '@angular/material' as mat;
$theme: ();
@include mat.slide-toggle-theme($theme);
`,
`
@use '@angular/material' as mat;
$theme: ();
@include mat.mdc-slide-toggle-theme($theme);
@include mat.mdc-slide-toggle-typography($theme);
`,
);
});
});

describe('selector migrations', () => {
it('should update the legacy mat-slide-toggle classname', async () => {
await runMigrationTest(
`
.mat-slide-toggle {
padding: 4px;
}
`,
`
.mat-mdc-slide-toggle {
padding: 4px;
}
`,
);
});

it('should add comment for potentially deprecated selector', async () => {
await runMigrationTest(
`
.mat-slide-toggle-thumb {
height: 16px;
}
`,
`
/* TODO: The following rule targets internal classes of slide-toggle that may no longer apply for the MDC version. */
.mat-slide-toggle-thumb {
height: 16px;
}
`,
);
});

it('should add comment for potentially deprecated multi-line selector', async () => {
await runMigrationTest(
`
.some-class
.mat-slide-toggle-thumb {
height: 16px;
}
`,
`
/* TODO: The following rule targets internal classes of slide-toggle that may no longer apply for the MDC version. */
.some-class
.mat-slide-toggle-thumb {
height: 16px;
}
`,
);
});

it('should update the legacy mat-slide-toggle class and add comment for potentially deprecated selector', async () => {
await runMigrationTest(
`
.mat-slide-toggle.some-class, .mat-slide-toggle-thumb {
background-color: transparent;
}
`,
`
/* TODO: The following rule targets internal classes of slide-toggle that may no longer apply for the MDC version. */
.mat-mdc-slide-toggle.some-class, .mat-slide-toggle-thumb {
background-color: transparent;
}
`,
);
});
});
});
@@ -0,0 +1,24 @@
/**
* @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 SlideToggleStylesMigrator extends StyleMigrator {
component = 'slide-toggle';

deprecatedPrefix = 'mat-slide-toggle';

mixinChanges = [
{
old: 'slide-toggle-theme',
new: ['mdc-slide-toggle-theme', 'mdc-slide-toggle-typography'],
},
];

classChanges: ClassNameChange[] = [{old: '.mat-slide-toggle', new: '.mat-mdc-slide-toggle'}];
}
Expand Up @@ -9,10 +9,12 @@
import {ButtonStylesMigrator} from './components/button/button-styles';
import {CheckboxStylesMigrator} from './components/checkbox/checkbox-styles';
import {ProgressBarStylesMigrator} from './components/progress-bar/progress-bar-styles';
import {SlideToggleStylesMigrator} from './components/slide-toggle/slide-toggle-styles';
import {StyleMigrator} from './style-migrator';

export const MIGRATORS: StyleMigrator[] = [
new ButtonStylesMigrator(),
new CheckboxStylesMigrator(),
new ProgressBarStylesMigrator(),
new SlideToggleStylesMigrator(),
];

0 comments on commit 7646c73

Please sign in to comment.