-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(material/schematics): impl card template migration (#24566)
* implement TemplateMigrator for mat-card * add unit tests for card template migrations * implement migration updates in TemplateMigration * added CardTemplateMigrator to list of MIGRATORS
- Loading branch information
1 parent
6feaaea
commit 15906c7
Showing
4 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
...material/schematics/ng-generate/mdc-migration/rules/components/card/card-template.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import {createTestApp, patchDevkitTreeToExposeTypeScript} from '@angular/cdk/schematics/testing'; | ||
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing'; | ||
import {createNewTestRunner, migrateComponents, TEMPLATE_FILE} from '../test-setup-helper'; | ||
|
||
describe('card template migrator', () => { | ||
let runner: SchematicTestRunner; | ||
let cliAppTree: UnitTestTree; | ||
|
||
async function runMigrationTest(oldFileContent: string, newFileContent: string) { | ||
cliAppTree.overwrite(TEMPLATE_FILE, oldFileContent); | ||
const tree = await migrateComponents(['card'], runner, cliAppTree); | ||
expect(tree.readContent(TEMPLATE_FILE)).toBe(newFileContent); | ||
} | ||
|
||
beforeEach(async () => { | ||
runner = createNewTestRunner(); | ||
cliAppTree = patchDevkitTreeToExposeTypeScript(await createTestApp(runner)); | ||
}); | ||
|
||
it('should not update other elements', async () => { | ||
await runMigrationTest('<mat-button></mat-button>', '<mat-button></mat-button>'); | ||
}); | ||
|
||
it('should update single', async () => { | ||
await runMigrationTest('<mat-card></mat-card>', '<mat-card appearance="outlined"></mat-card>'); | ||
}); | ||
|
||
it('should update multiple same-line unnested', async () => { | ||
await runMigrationTest( | ||
'<mat-card></mat-card><mat-card></mat-card>', | ||
'<mat-card appearance="outlined"></mat-card><mat-card appearance="outlined"></mat-card>', | ||
); | ||
}); | ||
|
||
it('should update multiple same-line nested', async () => { | ||
await runMigrationTest( | ||
'<mat-card><mat-card></mat-card></mat-card>', | ||
'<mat-card appearance="outlined"><mat-card appearance="outlined"></mat-card></mat-card>', | ||
); | ||
}); | ||
|
||
it('should update multiple same-line nested and unnested', async () => { | ||
await runMigrationTest( | ||
'<mat-card><mat-card></mat-card><mat-card></mat-card></mat-card>', | ||
'<mat-card appearance="outlined"><mat-card appearance="outlined"></mat-card><mat-card appearance="outlined"></mat-card></mat-card>', | ||
); | ||
}); | ||
|
||
it('should update multiple multi-line unnested', async () => { | ||
await runMigrationTest( | ||
` | ||
<mat-card></mat-card> | ||
<mat-card></mat-card> | ||
`, | ||
` | ||
<mat-card appearance="outlined"></mat-card> | ||
<mat-card appearance="outlined"></mat-card> | ||
`, | ||
); | ||
}); | ||
|
||
it('should update multiple multi-line nested', async () => { | ||
await runMigrationTest( | ||
` | ||
<mat-card> | ||
<mat-card></mat-card> | ||
</mat-card> | ||
`, | ||
` | ||
<mat-card appearance="outlined"> | ||
<mat-card appearance="outlined"></mat-card> | ||
</mat-card> | ||
`, | ||
); | ||
}); | ||
|
||
it('should update multiple multi-line nested and unnested', async () => { | ||
await runMigrationTest( | ||
` | ||
<mat-card> | ||
<mat-card></mat-card> | ||
<mat-card></mat-card> | ||
</mat-card> | ||
`, | ||
` | ||
<mat-card appearance="outlined"> | ||
<mat-card appearance="outlined"></mat-card> | ||
<mat-card appearance="outlined"></mat-card> | ||
</mat-card> | ||
`, | ||
); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
src/material/schematics/ng-generate/mdc-migration/rules/components/card/card-template.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @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 {TmplAstElement} from '@angular/compiler'; | ||
import {TemplateMigrator} from '../../template-migrator'; | ||
import {addAttribute} from '../../tree-traversal'; | ||
|
||
export class CardTemplateMigrator extends TemplateMigrator { | ||
component = 'card'; | ||
tagName = 'mat-card'; | ||
|
||
override updateStartTag(template: string, node: TmplAstElement): string { | ||
if (node.name !== this.tagName) { | ||
return template; | ||
} | ||
return addAttribute(template, node, 'appearance', 'outlined'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters