Skip to content

Commit

Permalink
feat(material/schematics): add snack-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 b9ee368 commit 7460ccd
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 4 deletions.
Expand Up @@ -38,6 +38,28 @@ describe('button styles', () => {
);
});

it('should replace the old theme with the non-duplicated new ones', async () => {
await runMigrationTest(
`
@use '@angular/material' as mat;
$theme: ();
@include mat.mdc-button-theme($theme);
@include mat.mdc-button-typography($theme);
@include mat.button-theme($theme);
`,
`
@use '@angular/material' as mat;
$theme: ();
@include mat.mdc-button-theme($theme);
@include mat.mdc-button-typography($theme);
@include mat.mdc-fab-theme($theme);
@include mat.mdc-fab-typography($theme);
@include mat.mdc-icon-button-theme($theme);
@include mat.mdc-icon-button-typography($theme);
`,
);
});

it('should use the correct namespace', async () => {
await runMigrationTest(
`
Expand Down
Expand Up @@ -24,6 +24,7 @@ export class ButtonStylesMigrator extends StyleMigrator {
'mdc-icon-button-theme',
'mdc-icon-button-typography',
],
checkForDuplicates: true,
},
];

Expand Down
Expand Up @@ -42,6 +42,30 @@ describe('multiple component styles', () => {
);
});

it('should add theme once if both components include it', async () => {
await runMigrationTest(
['button', 'snack-bar'],
`
@use '@angular/material' as mat;
$theme: ();
@include mat.button-theme($theme);
@include mat.snack-bar-theme($theme);
`,
`
@use '@angular/material' as mat;
$theme: ();
@include mat.mdc-button-theme($theme);
@include mat.mdc-button-typography($theme);
@include mat.mdc-fab-theme($theme);
@include mat.mdc-fab-typography($theme);
@include mat.mdc-icon-button-theme($theme);
@include mat.mdc-icon-button-typography($theme);
@include mat.mdc-snack-bar-theme($theme);
@include mat.mdc-snack-bar-typography($theme);
`,
);
});

it('should add correct theme if all-component-themes mixin included', async () => {
await runMigrationTest(
['checkbox', 'radio'],
Expand Down
@@ -0,0 +1,216 @@
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('snack-bar styles', () => {
let runner: SchematicTestRunner;
let cliAppTree: UnitTestTree;

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

it('should replace the old theme with the new ones and keep the non MDC button theme', async () => {
await runMigrationTest(
`
@use '@angular/material' as mat;
$theme: ();
@include mat.button-theme($theme);
@include mat.snack-bar-theme($theme);
`,
`
@use '@angular/material' as mat;
$theme: ();
@include mat.button-theme($theme);
@include mat.mdc-snack-bar-theme($theme);
@include mat.mdc-snack-bar-typography($theme);
@include mat.mdc-button-theme($theme);
@include mat.mdc-button-typography($theme);
`,
);
});

it('should replace the old theme with the non-duplicated new ones', async () => {
await runMigrationTest(
`
@use '@angular/material' as mat;
$theme: ();
@include mat.snack-bar-theme($theme);
@include mat.mdc-button-theme($theme);
@include mat.mdc-button-typography($theme);
`,
`
@use '@angular/material' as mat;
$theme: ();
@include mat.mdc-snack-bar-theme($theme);
@include mat.mdc-snack-bar-typography($theme);
@include mat.mdc-button-theme($theme);
@include mat.mdc-button-typography($theme);
`,
);
});

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

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

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

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

it('should update multiple legacy classnames', async () => {
await runMigrationTest(
`
.mat-snack-bar-container {
padding: 24px;
}
.mat-simple-snackbar {
color: red;
}
`,
`
.mat-mdc-snack-bar-container {
padding: 24px;
}
.mat-mdc-simple-snack-bar {
color: red;
}
`,
);
});

it('should update a legacy classname w/ multiple selectors', async () => {
await runMigrationTest(
`
.some-class.mat-snack-bar-container, .another-class {
padding: 24px;
}
`,
`
.some-class.mat-mdc-snack-bar-container, .another-class {
padding: 24px;
}
`,
);
});

it('should preserve the whitespace of multiple selectors', async () => {
await runMigrationTest(
`
.some-class,
.mat-snack-bar-container,
.another-class { padding: 24px; }
`,
`
.some-class,
.mat-mdc-snack-bar-container,
.another-class { padding: 24px; }
`,
);
});
});
});
@@ -0,0 +1,36 @@
/**
* @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 SnackBarMigrator extends StyleMigrator {
component = 'snack-bar';

// There are no other selectors with the 'mat-snack-bar' prefix available
// aside from the specified changes below
deprecatedPrefixes = [];

mixinChanges = [
{
old: 'snack-bar-theme',
new: [
'mdc-snack-bar-theme',
'mdc-snack-bar-typography',
'mdc-button-theme',
'mdc-button-typography',
],
checkForDuplicates: true,
},
];

classChanges: ClassNameChange[] = [
{old: '.mat-snack-bar-container', new: '.mat-mdc-snack-bar-container'},
{old: '.mat-snack-bar-handset', new: '.mat-mdc-snack-bar-handset'},
{old: '.mat-simple-snackbar', new: '.mat-mdc-simple-snack-bar'},
];
}
Expand Up @@ -29,6 +29,7 @@ import {RuntimeMigrator} from './ts-migration/runtime-migrator';
import {SelectStylesMigrator} from './components/select/select-styles';
import {SlideToggleStylesMigrator} from './components/slide-toggle/slide-toggle-styles';
import {SliderStylesMigrator} from './components/slider/slider-styles';
import {SnackBarMigrator} from './components/snack-bar/snack-bar-styles';
import {TableStylesMigrator} from './components/table/table-styles';
import {TabsStylesMigrator} from './components/tabs/tabs-styles';
import {TooltipStylesMigrator} from './components/tooltip/tooltip-styles';
Expand Down Expand Up @@ -129,6 +130,10 @@ export const MIGRATORS: ComponentMigrator[] = [
styles: new SliderStylesMigrator(),
runtime: new RuntimeMigrator('slider'),
},
{
component: 'snack-bar',
styles: new SnackBarMigrator(),
},
{
component: 'table',
styles: new TableStylesMigrator(),
Expand Down

0 comments on commit 7460ccd

Please sign in to comment.