Skip to content

Commit

Permalink
feat(material/schematics): add slider 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 da07c36 commit d8f1cce
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
@@ -0,0 +1,151 @@
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('slider styles', () => {
let runner: SchematicTestRunner;
let cliAppTree: UnitTestTree;

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

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

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

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

describe('selector migrations', () => {
it('should update the legacy mat-slider classname', async () => {
await runMigrationTest(
`
.mat-slider {
width: 100%;
}
`,
`
.mat-mdc-slider {
width: 100%;
}
`,
);
});

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

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

deprecatedPrefix = 'mat-slider';

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

classChanges: ClassNameChange[] = [{old: '.mat-slider', new: '.mat-mdc-slider'}];
}
Expand Up @@ -11,6 +11,7 @@ import {CheckboxStylesMigrator} from './components/checkbox/checkbox-styles';
import {ProgressBarStylesMigrator} from './components/progress-bar/progress-bar-styles';
import {RadioStylesMigrator} from './components/radio/radio-styles';
import {SlideToggleStylesMigrator} from './components/slide-toggle/slide-toggle-styles';
import {SliderStylesMigrator} from './components/slider/slider-styles';
import {StyleMigrator} from './style-migrator';

export const MIGRATORS: StyleMigrator[] = [
Expand All @@ -19,4 +20,5 @@ export const MIGRATORS: StyleMigrator[] = [
new ProgressBarStylesMigrator(),
new RadioStylesMigrator(),
new SlideToggleStylesMigrator(),
new SliderStylesMigrator(),
];

0 comments on commit d8f1cce

Please sign in to comment.