Skip to content

Commit

Permalink
feat(material/schematics): add dialog 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 8f8f06f commit 98d09ff
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 0 deletions.
2 changes: 2 additions & 0 deletions integration/mdc-migration/golden/src/styles.scss
Expand Up @@ -44,6 +44,8 @@ $sample-project-theme: mat.define-light-theme((
@include mat.mdc-progress-bar-typography($sample-project-theme);
@include mat.mdc-paginator-theme($sample-project-theme);
@include mat.mdc-paginator-typography($sample-project-theme);
@include mat.mdc-dialog-theme($sample-project-theme);
@include mat.mdc-dialog-typography($sample-project-theme);
@include mat.mdc-checkbox-theme($sample-project-theme);
@include mat.mdc-checkbox-typography($sample-project-theme);
@include mat.mdc-card-theme($sample-project-theme);
Expand Down
@@ -0,0 +1,259 @@
import {createTestApp, patchDevkitTreeToExposeTypeScript} from '@angular/cdk/schematics/testing';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
import {createNewTestRunner, migrateComponents, THEME_FILE} from '../test-setup-helper';

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

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

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

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

it('should add correct theme if all-component-themes mixin included', async () => {
await runMigrationTest(
`
@use '@angular/material' as mat;
$theme: ();
@include mat.all-component-themes($theme);
`,
`
@use '@angular/material' as mat;
$theme: ();
@include mat.all-component-themes($theme);
@include mat.mdc-dialog-theme($theme);
@include mat.mdc-dialog-typography($theme);
`,
);
});

it('should add multiple themes for multiple all-component-themes mixins', async () => {
await runMigrationTest(
`
@use '@angular/material' as mat;
$light-theme: ();
$dark-theme: ();
@include mat.all-component-themes($light-theme);
@include mat.all-component-themes($dark-theme);
`,
`
@use '@angular/material' as mat;
$light-theme: ();
$dark-theme: ();
@include mat.all-component-themes($light-theme);
@include mat.mdc-dialog-theme($light-theme);
@include mat.mdc-dialog-typography($light-theme);
@include mat.all-component-themes($dark-theme);
@include mat.mdc-dialog-theme($dark-theme);
@include mat.mdc-dialog-typography($dark-theme);
`,
);
});

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

describe('selector migrations', () => {
it('should update the legacy mat-dialog classname', async () => {
await runMigrationTest(
`
.mat-dialog {
background: red;
}
`,
`
.mat-mdc-dialog {
background: red;
}
`,
);
});

it('should update multiple legacy classnames', async () => {
await runMigrationTest(
`
.mat-dialog {
background: red;
}
.mat-dialog-container {
padding: 25px;
}
`,
`
.mat-mdc-dialog {
background: red;
}
.mat-mdc-dialog-container {
padding: 25px;
}
`,
);
});

it('should update a legacy classname w/ multiple selectors', async () => {
await runMigrationTest(
`
.some-class.mat-dialog, .another-class {
background: red;
}
`,
`
.some-class.mat-mdc-dialog, .another-class {
background: red;
}
`,
);
});

it('should preserve the whitespace of multiple selectors', async () => {
await runMigrationTest(
`
.some-class,
.mat-dialog-content,
.another-class { padding: 16px; }
`,
`
.some-class,
.mat-mdc-dialog-content,
.another-class { padding: 16px; }
`,
);
});

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

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

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

deprecatedPrefix = 'mat-dialog';

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

classChanges: ClassNameChange[] = [
{old: '.mat-dialog', new: '.mat-mdc-dialog'},
{old: '.mat-dialog-title', new: '.mat-mdc-dialog-title'},
{old: '.mat-dialog-container', new: '.mat-mdc-dialog-container'},
{old: '.mat-dialog-content', new: '.mat-mdc-dialog-content'},
{old: '.mat-dialog-actions', new: '.mat-mdc-dialog-actions'},
];
}
Expand Up @@ -9,6 +9,7 @@
import {ButtonStylesMigrator} from './components/button/button-styles';
import {CardStylesMigrator} from './components/card/card-styles';
import {CheckboxStylesMigrator} from './components/checkbox/checkbox-styles';
import {DialogStylesMigrator} from './components/dialog/dialog-styles';
import {PaginatorStylesMigrator} from './components/paginator/paginator-styles';
import {ProgressBarStylesMigrator} from './components/progress-bar/progress-bar-styles';
import {ProgressSpinnerStylesMigrator} from './components/progress-spinner/progress-spinner-styles';
Expand All @@ -21,6 +22,7 @@ export const MIGRATORS: StyleMigrator[] = [
new ButtonStylesMigrator(),
new CardStylesMigrator(),
new CheckboxStylesMigrator(),
new DialogStylesMigrator(),
new PaginatorStylesMigrator(),
new ProgressBarStylesMigrator(),
new ProgressSpinnerStylesMigrator(),
Expand Down

0 comments on commit 98d09ff

Please sign in to comment.