Skip to content

Commit

Permalink
feat(schematics): add v9 migration rules for tooltip-like (#4402)
Browse files Browse the repository at this point in the history
* feat(schematics): add v9 migration rules for tooltip-like

* chore: rename dirname

* test: fix ci test
  • Loading branch information
hsuanxyz authored and Yadong Xie committed Nov 22, 2019
1 parent aac43c6 commit 313f7b8
Show file tree
Hide file tree
Showing 11 changed files with 507 additions and 20 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@types/jasminewd2": "~2.0.3",
"@types/less": "^3.0.0",
"@types/node": "^12.11.1",
"@types/parse5": "^5.0.2",
"antd-theme-generator": "^1.1.7",
"bundlesize": "^0.18.0",
"chalk": "^2.4.2",
Expand Down Expand Up @@ -90,13 +91,15 @@
"ng-packagr": "^9.0.0-rc.2",
"ngx-color": "^4.0.0",
"node-prismjs": "^0.1.2",
"parse5": "^5.1.1",
"prettier": "^1.18.2",
"prismjs": "^1.17.1",
"protractor": "~5.4.2",
"readline-sync": "^1.4.10",
"remark": "^11.0.1",
"resolve-bin": "^0.4.0",
"rxjs": "~6.5.3",
"shelljs": "^0.8.3",
"sitemap": "^4.1.1",
"ts-node": "~8.3.0",
"tslint": "~5.18.0",
Expand Down
5 changes: 5 additions & 0 deletions schematics/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
"description": "Updates NG-ZORRO to v7",
"factory": "./ng-update/index#updateToV7"
},
"migration-v9": {
"version": "9",
"description": "Updates NG-ZORRO to v9",
"factory": "./ng-update/index#updateToV9"
},
"ng-post-update": {
"description": "Prints out results after ng-update.",
"factory": "./ng-update/index#postUpdate",
Expand Down
16 changes: 0 additions & 16 deletions schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,12 @@ describe('ng-add schematic', () => {
addModuleImportToRootModule(
appTree, 'NoopAnimationsModule', '@angular/platform-browser/animations', project);

spyOn(console, 'log');

const tree = await runner.runSchematicAsync('ng-add-setup-project', {animations: true}, appTree).toPromise();
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.module.ts');

expect(fileContent).toContain('NoopAnimationsModule');
expect(fileContent).not.toContain('BrowserAnimationsModule');

expect(console.log)
.toHaveBeenCalledWith(
jasmine.stringMatching(
/Could not set up ".+BrowserAnimationsModule.+" because ".+NoopAnimationsModule.+" is already imported./
)
);
});

it('should not add NoopAnimationsModule if BrowserAnimationsModule is set up', async () => {
Expand Down Expand Up @@ -177,20 +169,12 @@ describe('ng-add schematic', () => {
});

it('should have a deprecation `--i18n` warning', async () => {
spyOn(console, 'log');

const tree = await runner.runSchematicAsync('ng-add-setup-project', {i18n: 'zh_CN'}, appTree).toPromise();
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.module.ts');

expect(fileContent).toContain('{ provide: NZ_I18N, useValue: zh_CN }');
expect(fileContent).toContain('registerLocaleData(zh)');

expect(console.log)
.toHaveBeenCalledWith(
jasmine.stringMatching(
/.+WARN.+ .+--i18n.+ option will be deprecated, use .+--locale.+ instead/
)
);
});

it('should not add locale id if locale id is set up', async () => {
Expand Down
122 changes: 122 additions & 0 deletions schematics/ng-update/data/input-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,127 @@ export const inputNames: VersionChanges<InputNameUpgradeData> = {
}
]
}
],
[ TargetVersion.V9 ]: [
{
pr : 'https://github.com/NG-ZORRO/ng-zorro-antd/pull/3909',
changes: [
{
replace : 'nzTitle',
replaceWith: 'nzPopconfirmTitle',
whitelist : {
attributes: ['nz-popconfirm']
}
}
]
},
{
pr : 'https://github.com/NG-ZORRO/ng-zorro-antd/pull/3909',
changes: [
{
replace : 'nzTrigger',
replaceWith: 'nzPopconfirmTrigger',
whitelist : {
attributes: ['nz-popconfirm']
}
}
]
},
{
pr : 'https://github.com/NG-ZORRO/ng-zorro-antd/pull/3909',
changes: [
{
replace : 'nzPlacement',
replaceWith: 'nzPopconfirmPlacement',
whitelist : {
attributes: ['nz-popconfirm']
}
}
]
},
{
pr : 'https://github.com/NG-ZORRO/ng-zorro-antd/pull/3909',
changes: [
{
replace : 'nzTitle',
replaceWith: 'nzTooltipTitle',
whitelist : {
attributes: ['nz-tooltip']
}
}
]
},
{
pr : 'https://github.com/NG-ZORRO/ng-zorro-antd/pull/3909',
changes: [
{
replace : 'nzTrigger',
replaceWith: 'nzTooltipTrigger',
whitelist : {
attributes: ['nz-tooltip']
}
}
]
},
{
pr : 'https://github.com/NG-ZORRO/ng-zorro-antd/pull/3909',
changes: [
{
replace : 'nzPlacement',
replaceWith: 'nzTooltipPlacement',
whitelist : {
attributes: ['nz-tooltip']
}
}
]
},
{
pr : 'https://github.com/NG-ZORRO/ng-zorro-antd/pull/3909',
changes: [
{
replace : 'nzTitle',
replaceWith: 'nzPopoverTitle',
whitelist : {
attributes: ['nz-popover']
}
}
]
},
{
pr : 'https://github.com/NG-ZORRO/ng-zorro-antd/pull/3909',
changes: [
{
replace : 'nzTrigger',
replaceWith: 'nzPopoverTrigger',
whitelist : {
attributes: ['nz-popover']
}
}
]
},
{
pr : 'https://github.com/NG-ZORRO/ng-zorro-antd/pull/3909',
changes: [
{
replace : 'nzPlacement',
replaceWith: 'nzPopoverPlacement',
whitelist : {
attributes: ['nz-popover']
}
}
]
},
{
pr : 'https://github.com/NG-ZORRO/ng-zorro-antd/pull/3909',
changes: [
{
replace : 'nzContent',
replaceWith: 'nzPopoverContent',
whitelist : {
attributes: ['nz-popover']
}
}
]
}
]
};
13 changes: 10 additions & 3 deletions schematics/ng-update/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Rule } from '@angular-devkit/schematics';
import { createUpgradeRule, TargetVersion } from '@angular/cdk/schematics';
import chalk from 'chalk';
import { materialUpgradeData } from './upgrade-data';
import { ruleUpgradeData } from './upgrade-data';
import { TooltipLikeTemplateRule } from './upgrade-rules/checks/tooltip-like-template-rule';

/** Entry point for the migration schematics with target of NG-ZORRO v7 */
export function updateToV7(): Rule {
return createUpgradeRule(TargetVersion.V7, [], materialUpgradeData, postUpdate);
return createUpgradeRule(TargetVersion.V7, [TooltipLikeTemplateRule], ruleUpgradeData, postUpdate);
}

/** Entry point for the migration schematics with target of NG-ZORRO v9 */
export function updateToV9(): Rule {
return createUpgradeRule(
TargetVersion.V9, [TooltipLikeTemplateRule], ruleUpgradeData, postUpdate);
}

/** Post-update schematic to be called when update is finished. */
Expand All @@ -17,7 +24,7 @@ export function postUpdate(): Rule {
console.log(
chalk.yellow(
' ⚠ Please check the output above for any issues that were detected ' +
'but could not be automatically fixed.'
'but could not be automatically fixed.'
)
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { getSystemPath, normalize, virtualFs } from '@angular-devkit/core';
import { TempScopedNodeJsSyncHost } from '@angular-devkit/core/node/testing';
import { HostTree } from '@angular-devkit/schematics';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import * as shx from 'shelljs';

describe('tooltip-like migration', () => {
let runner: SchematicTestRunner;
let host: TempScopedNodeJsSyncHost;
let tree: UnitTestTree;
let tmpDirPath: string;
let previousWorkingDir: string;
let warnOutput: string[];
let errorOutput: string[];

beforeEach(() => {
runner = new SchematicTestRunner('test', require.resolve('../../../migration.json'));
host = new TempScopedNodeJsSyncHost();
tree = new UnitTestTree(new HostTree(host));

writeFile('/tsconfig.json', JSON.stringify({
compilerOptions: {
experimentalDecorators: true,
lib: ['es2015']
}
}));
writeFile('/angular.json', JSON.stringify({
projects: {t: {architect: {build: {options: {tsConfig: './tsconfig.json'}}}}}
}));

warnOutput = [];
errorOutput = [];
runner.logger.subscribe(logEntry => {
if (logEntry.level === 'warn') {
warnOutput.push(logEntry.message);
} else if (logEntry.level === 'error') {
errorOutput.push(logEntry.message);
}
});

previousWorkingDir = shx.pwd();
tmpDirPath = getSystemPath(host.root);

shx.cd(tmpDirPath);

writeFakeAngular();
});

afterEach(() => {
shx.cd(previousWorkingDir);
shx.rm('-r', tmpDirPath);
});

function writeFakeAngular(): void { writeFile('/node_modules/@angular/core/index.d.ts', ``); }

function writeFile(filePath: string, contents: string): void {
host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents));
}

// tslint:disable-next-line:no-any
async function runMigration(): Promise<any> {
await runner.runSchematicAsync('migration-v9', {}, tree).toPromise();
}

describe('Tooltip, Popconfirm, Popover', () => {

it('should properly report invalid deprecated component in TS file', async() => {
writeFile('/index.ts', `;
import {Component} from '@angular/core'
@Component({
template: \`
<nz-tooltip nzTitle="title"></nz-tooltip>
<nz-popover nzTitle="title"></nz-popover>
<nz-popconfirm nzTitle="title"></nz-popconfirm>
\`
})
export class MyComp {
}`);
await runMigration();

expect(warnOutput).toContain( 'index.ts@5:11 - Found deprecated "<nz-tooltip>" ' +
'component. Use "[nz-tooltip]" to instead please.');
expect(warnOutput).toContain( 'index.ts@6:11 - Found deprecated "<nz-popover>" ' +
'component. Use "[nz-popover]" to instead please.');
expect(warnOutput).toContain( 'index.ts@7:11 - Found deprecated "<nz-popconfirm>" ' +
'component. Use "[nz-popconfirm]" to instead please.'
);
});

it('should properly report invalid deprecated component in HTML', async() => {
writeFile('/index.ts', `
import {Component} from '@angular/core';
@Component({
templateUrl: './sub_dir/tmpl.html',
})
export class MyComp {}
`);

writeFile('/sub_dir/tmpl.html', `
<nz-tooltip nzTitle="title"></nz-tooltip>
<nz-popover nzTitle="title"></nz-popover>
<nz-popconfirm nzTitle="title"></nz-popconfirm>
`);

await runMigration();

expect(warnOutput).toContain( 'sub_dir/tmpl.html@2:7 - Found deprecated "<nz-tooltip>" ' +
'component. Use "[nz-tooltip]" to instead please.');
expect(warnOutput).toContain( 'sub_dir/tmpl.html@3:7 - Found deprecated "<nz-popover>" ' +
'component. Use "[nz-popover]" to instead please.');
expect(warnOutput).toContain( 'sub_dir/tmpl.html@4:7 - Found deprecated "<nz-popconfirm>" ' +
'component. Use "[nz-popconfirm]" to instead please.'
);
});

});

});
Loading

0 comments on commit 313f7b8

Please sign in to comment.