Skip to content

Commit

Permalink
feat(material/schematics): add progress bar 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 3d3656a commit e856da1
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('progress-bar styles', () => {
let runner: SchematicTestRunner;
let cliAppTree: UnitTestTree;

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

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

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

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

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

it('should add comment for potentially deprecated selector', async () => {
await runMigrationTest(
`
.mat-progress-bar-buffer {
background-color: red;
}
`,
`
/* TODO: The following rule targets internal classes of progress-bar that may no longer apply for the MDC version. */
.mat-progress-bar-buffer {
background-color: red;
}
`,
);
});

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

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

deprecatedPrefix = 'mat-progress-bar';

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

classChanges: ClassNameChange[] = [{old: '.mat-progress-bar', new: '.mat-mdc-progress-bar'}];
}
Expand Up @@ -8,9 +8,11 @@

import {ButtonStylesMigrator} from './components/button/button-styles';
import {CheckboxStylesMigrator} from './components/checkbox/checkbox-styles';
import {ProgressBarStylesMigrator} from './components/progress-bar/progress-bar-styles';
import {StyleMigrator} from './style-migrator';

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

0 comments on commit e856da1

Please sign in to comment.