Skip to content

Commit

Permalink
feat(material/schematics): add paginator 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 53b6b52 commit d1bbcd6
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 0 deletions.
2 changes: 2 additions & 0 deletions integration/mdc-migration/golden/src/styles.scss
Expand Up @@ -42,6 +42,8 @@ $sample-project-theme: mat.define-light-theme((
@include mat.mdc-progress-spinner-typography($sample-project-theme);
@include mat.mdc-progress-bar-theme($sample-project-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-checkbox-theme($sample-project-theme);
@include mat.mdc-checkbox-typography($sample-project-theme);
@include mat.mdc-button-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('paginator styles', () => {
let runner: SchematicTestRunner;
let cliAppTree: UnitTestTree;

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

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

it('should handle updating multiple themes', async () => {
await runMigrationTest(
`
@use '@angular/material' as mat;
$light-theme: ();
$dark-theme: ();
@include mat.paginator-theme($light-theme);
@include mat.paginator-theme($dark-theme);
`,
`
@use '@angular/material' as mat;
$light-theme: ();
$dark-theme: ();
@include mat.mdc-paginator-theme($light-theme);
@include mat.mdc-paginator-typography($light-theme);
@include mat.mdc-paginator-theme($dark-theme);
@include mat.mdc-paginator-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-paginator-theme($theme);
@include mat.mdc-paginator-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-paginator-theme($light-theme);
@include mat.mdc-paginator-typography($light-theme);
@include mat.all-component-themes($dark-theme);
@include mat.mdc-paginator-theme($dark-theme);
@include mat.mdc-paginator-typography($dark-theme);
`,
);
});

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

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

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

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

it('should preserve the whitespace of multiple selectors', async () => {
await runMigrationTest(
`
.some-class,
.mat-paginator-container,
.another-class { background: red; }
`,
`
.some-class,
.mat-mdc-paginator-container,
.another-class { background: red; }
`,
);
});

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

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

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

deprecatedPrefix = 'mat-paginator';

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

classChanges: ClassNameChange[] = [
{old: '.mat-paginator', new: '.mat-mdc-paginator'},
{old: '.mat-paginator-outer-container', new: '.mat-mdc-paginator-outer-container'},
{old: '.mat-paginator-container', new: '.mat-mdc-paginator-container'},
{old: '.mat-paginator-page-size', new: '.mat-mdc-paginator-page-size'},
{old: '.mat-paginator-page-size-label', new: '.mat-mdc-paginator-page-size-label'},
{old: '.mat-paginator-page-size-value', new: '.mat-mdc-paginator-page-size-value'},
{old: '.mat-paginator-range-actions', new: '.mat-mdc-paginator-range-actions'},
{old: '.mat-paginator-range-label', new: '.mat-mdc-paginator-range-label'},
{old: '.mat-paginator-navigation-first', new: '.mat-mdc-paginator-navigation-first'},
{old: '.mat-paginator-navigation-previous', new: '.mat-mdc-paginator-navigation-previous'},
{old: '.mat-paginator-navigation-next', new: '.mat-mdc-paginator-navigation-next'},
{old: '.mat-paginator-navigation-last', new: '.mat-mdc-paginator-navigation-last'},
{old: '.mat-paginator-icon', new: '.mat-mdc-paginator-icon'},
];
}
Expand Up @@ -8,6 +8,7 @@

import {ButtonStylesMigrator} from './components/button/button-styles';
import {CheckboxStylesMigrator} from './components/checkbox/checkbox-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';
import {RadioStylesMigrator} from './components/radio/radio-styles';
Expand All @@ -18,6 +19,7 @@ import {StyleMigrator} from './style-migrator';
export const MIGRATORS: StyleMigrator[] = [
new ButtonStylesMigrator(),
new CheckboxStylesMigrator(),
new PaginatorStylesMigrator(),
new ProgressBarStylesMigrator(),
new ProgressSpinnerStylesMigrator(),
new RadioStylesMigrator(),
Expand Down

0 comments on commit d1bbcd6

Please sign in to comment.