Skip to content

Commit

Permalink
chore: update schematics with v14
Browse files Browse the repository at this point in the history
  • Loading branch information
simplejason committed Jul 31, 2022
1 parent 8e5572d commit 9d50d27
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"dependencies": {
"@angular/cdk": "^14.1.0",
"@ant-design/icons-angular": "^13.1.0",
"@ant-design/icons-angular": "^14.1.0",
"@ctrl/tinycolor": "^3.4.1",
"date-fns": "^2.16.1",
"ngx-hover-preload": "0.0.3"
Expand Down
78 changes: 51 additions & 27 deletions schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ import { WorkspaceDefinition } from '@angular-devkit/core/src/workspace';
import { Tree } from '@angular-devkit/schematics';
import { NodePackageName } from '@angular-devkit/schematics/tasks/package-manager/options';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import { getFileContent } from '@schematics/angular/utility/test';
import { getFileContent } from '@schematics/angular/utility/test/get-file-content';
import { getWorkspace } from '@schematics/angular/utility/workspace';

import { join } from "path";

import { createTestApp } from '../testing/test-app';
import { createCustomTheme } from '../utils/create-custom-theme';
import { Schema as NzOptions } from './schema';

describe('ng-add schematic', () => {
const defaultOptions: NzOptions = {
project: 'ng-zorro',
};

let runner: SchematicTestRunner;
let appTree: Tree;

Expand All @@ -24,8 +29,8 @@ describe('ng-add schematic', () => {
});

it('should update package.json', async () => {
const tree = await runner.runSchematicAsync('ng-add', {}, appTree).toPromise();

const options = {...defaultOptions};
const tree = await runner.runSchematicAsync('ng-add', options, appTree).toPromise();
const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
const dependencies = packageJson.dependencies;

Expand All @@ -35,7 +40,8 @@ describe('ng-add schematic', () => {
});

it('should add hammerjs to package.json', async () => {
const tree = await runner.runSchematicAsync('ng-add', {gestures: true}, appTree).toPromise();
const options = {...defaultOptions, gestures: true};
const tree = await runner.runSchematicAsync('ng-add', options, appTree).toPromise();

const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
const dependencies = packageJson.dependencies;
Expand All @@ -44,7 +50,8 @@ describe('ng-add schematic', () => {
});

it('should skip package.json', async () => {
const tree = await runner.runSchematicAsync('ng-add', {skipPackageJson: true}, appTree).toPromise();
const options = {...defaultOptions, skipPackageJson: true};
const tree = await runner.runSchematicAsync('ng-add', options, appTree).toPromise();

const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
const dependencies = packageJson.dependencies;
Expand All @@ -53,34 +60,39 @@ describe('ng-add schematic', () => {
});

it('should skip install dependency package', async () => {
await runner.runSchematicAsync('ng-add', {skipInstall: true}, appTree).toPromise();
const options = {...defaultOptions, skipInstall: true};
await runner.runSchematicAsync('ng-add', options, appTree).toPromise();

expect(runner.tasks.some(task => task.name === NodePackageName)).toBe(false);
});

it('should add hammerjs import to project main file', async () => {
const tree = await runner.runSchematicAsync('ng-add-setup-project', {gestures: true}, appTree).toPromise();
const options = {...defaultOptions, gestures: true};
const tree = await runner.runSchematicAsync('ng-add-setup-project', options, appTree).toPromise();
const workspace = await getWorkspace(tree);
const project = getProjectFromWorkspace(workspace as unknown as WorkspaceDefinition);
const project = getProjectFromWorkspace(workspace as unknown as WorkspaceDefinition, defaultOptions.project);
const fileContent = getFileContent(tree, normalize(join(project.sourceRoot, 'main.ts')));

expect(fileContent).toContain(`import 'hammerjs';`);
});

it('should add default theme', async () => {
const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise();
const options = {...defaultOptions};
const tree = await runner.runSchematicAsync('ng-add-setup-project', options, appTree).toPromise();
const workspace = await getWorkspace(tree);
const project = getProjectFromWorkspace(workspace as unknown as WorkspaceDefinition);
const project = getProjectFromWorkspace(workspace as unknown as WorkspaceDefinition, defaultOptions.project);

expect(getProjectTargetOptions(project, 'build').styles)
.toContain('./node_modules/ng-zorro-antd/ng-zorro-antd.min.css');
});

it('should add custom theme', async () => {
const options = {...defaultOptions, theme: true};

appTree = await createTestApp(runner, {style: 'less'});
const tree = await runner.runSchematicAsync('ng-add-setup-project', {theme: true}, appTree).toPromise();
const tree = await runner.runSchematicAsync('ng-add-setup-project', options, appTree).toPromise();
const workspace = await getWorkspace(tree);
const project = getProjectFromWorkspace(workspace as unknown as WorkspaceDefinition);
const project = getProjectFromWorkspace(workspace as unknown as WorkspaceDefinition, defaultOptions.project);

const customThemePath = normalize(join(project.sourceRoot, 'styles.less'));
const buffer = tree.read(customThemePath);
Expand All @@ -93,18 +105,20 @@ describe('ng-add schematic', () => {
});

it('should add custom theme file when no LESS file in project', async () => {
const tree = await runner.runSchematicAsync('ng-add-setup-project', {theme: true}, appTree).toPromise();
const options = {...defaultOptions, theme: true};
const tree = await runner.runSchematicAsync('ng-add-setup-project', options, appTree).toPromise();
const workspace = await getWorkspace(tree);
const project = getProjectFromWorkspace(workspace as unknown as WorkspaceDefinition);
const project = getProjectFromWorkspace(workspace as unknown as WorkspaceDefinition, defaultOptions.project);

expect(getProjectTargetOptions(project, 'build').styles)
.toContain('projects/ng-zorro/src/theme.less');
});

it('should add icon assets', async () => {
const tree = await runner.runSchematicAsync('ng-add-setup-project', {dynamicIcon: true}, appTree).toPromise();
const options = {...defaultOptions, dynamicIcon: true};
const tree = await runner.runSchematicAsync('ng-add-setup-project', options, appTree).toPromise();
const workspace = await getWorkspace(tree);
const project = getProjectFromWorkspace(workspace as unknown as WorkspaceDefinition);
const project = getProjectFromWorkspace(workspace as unknown as WorkspaceDefinition, defaultOptions.project);
const assets = getProjectTargetOptions(project, 'build').assets;

const assetsString = JSON.stringify(assets);
Expand All @@ -114,35 +128,40 @@ describe('ng-add schematic', () => {
});

it('should required modules', async () => {
const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise();
const options = {...defaultOptions};
const tree = await runner.runSchematicAsync('ng-add-setup-project', options, appTree).toPromise();
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.module.ts');

expect(fileContent).toContain('FormsModule');
expect(fileContent).toContain('HttpClientModule');
});

it('should add browserAnimationsModuleName if animations is enable', async () => {
const tree = await runner.runSchematicAsync('ng-add-setup-project', {animations: true}, appTree).toPromise();
const options = {...defaultOptions, animations: true};
const tree = await runner.runSchematicAsync('ng-add-setup-project', options, appTree).toPromise();
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.module.ts');

expect(fileContent).toContain('BrowserAnimationsModule');
});

it('should add noopAnimationsModuleName if animations is disable', async () => {
const tree = await runner.runSchematicAsync('ng-add-setup-project', {animations: false}, appTree).toPromise();
const options = {...defaultOptions, animations: false};
const tree = await runner.runSchematicAsync('ng-add-setup-project', options, appTree).toPromise();
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.module.ts');

expect(fileContent).toContain('NoopAnimationsModule');
});

it('should not add BrowserAnimationsModule if NoopAnimationsModule is set up', async () => {
const options = {...defaultOptions, animations: true};

const workspace = await getWorkspace(appTree);
const project = getProjectFromWorkspace(workspace as unknown as WorkspaceDefinition);
const project = getProjectFromWorkspace(workspace as unknown as WorkspaceDefinition, defaultOptions.project);

addModuleImportToRootModule(
appTree, 'NoopAnimationsModule', '@angular/platform-browser/animations', project);

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

expect(fileContent).toContain('NoopAnimationsModule');
Expand All @@ -151,41 +170,46 @@ describe('ng-add schematic', () => {
});

it('should not add NoopAnimationsModule if BrowserAnimationsModule is set up', async () => {
const options = {...defaultOptions, animations: false};

const workspace = await getWorkspace(appTree);
const project = getProjectFromWorkspace(workspace as unknown as WorkspaceDefinition);
const project = getProjectFromWorkspace(workspace as unknown as WorkspaceDefinition, defaultOptions.project);

addModuleImportToRootModule(
appTree, 'BrowserAnimationsModule', '@angular/platform-browser/animations', project);

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

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

it('should register default locale id', async () => {
const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise();
const options = {...defaultOptions};
const tree = await runner.runSchematicAsync('ng-add-setup-project', options, appTree).toPromise();
const fileContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.module.ts');

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

it('should register specified locale id', async () => {
const tree = await runner.runSchematicAsync('ng-add-setup-project', {locale: 'zh_CN'}, appTree).toPromise();
const options = {...defaultOptions, locale: 'zh_CN'};
const tree = await runner.runSchematicAsync('ng-add-setup-project', options, 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)');
});

it('should not add locale id if locale id is set up', async () => {
await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise();
const options = {...defaultOptions, i18n: 'zh_CN'};
await runner.runSchematicAsync('ng-add-setup-project', { ...defaultOptions }, appTree).toPromise();

spyOn(console, 'log');

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

expect(fileContent).toContain('{ provide: NZ_I18N, useValue: en_US }');
Expand Down
7 changes: 6 additions & 1 deletion schematics/ng-generate/blank/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Tree } from '@angular-devkit/schematics';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';

import { Schema as NzOptions } from '../../ng-add/schema';
import { createTestApp } from '../../testing/test-app';

describe('ng-component schematic', () => {
const defaultOptions: NzOptions = {
project: 'ng-zorro',
};
let runner: SchematicTestRunner;
let appTree: Tree;

Expand All @@ -13,8 +17,9 @@ describe('ng-component schematic', () => {
});

it('should update app.component.html', async () => {
const options = {...defaultOptions};
const appComponentHTMLPath = '/projects/ng-zorro/src/app/app.component.html';
const tree = await runner.runSchematicAsync('blank', {}, appTree).toPromise();
const tree = await runner.runSchematicAsync('blank', options, appTree).toPromise();
const appComponentHTML = tree.readContent(appComponentHTMLPath);
const files = tree.files;

Expand Down
25 changes: 18 additions & 7 deletions schematics/ng-generate/side-menu/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Tree } from '@angular-devkit/schematics';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import { Style } from '@schematics/angular/ng-new/schema';
import { getFileContent } from '@schematics/angular/utility/test';
import { getFileContent } from '@schematics/angular/utility/test/get-file-content';

import { Schema as NzOptions } from '../../ng-add/schema';
import { createTestApp } from '../../testing/test-app';

describe('side-menu schematic', () => {
const defaultOptions: NzOptions = {
project: 'ng-zorro',
};

let runner: SchematicTestRunner;
let appTree: Tree;

Expand All @@ -15,7 +20,8 @@ describe('side-menu schematic', () => {
});

it('should create side-menu files', async () => {
const tree = await runner.runSchematicAsync('sidemenu', {}, appTree).toPromise();
const options = {...defaultOptions};
const tree = await runner.runSchematicAsync('sidemenu', options, appTree).toPromise();
const files = tree.files;
expect(files).toEqual(
jasmine.arrayContaining([
Expand All @@ -34,7 +40,8 @@ describe('side-menu schematic', () => {
});

it('should set the style preprocessor correctly', async () => {
const tree = await runner.runSchematicAsync('sidemenu', { style: Style.Less }, appTree).toPromise();
const options = {...defaultOptions, style: Style.Less};
const tree = await runner.runSchematicAsync('sidemenu', options, appTree).toPromise();
const files = tree.files;
const appContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.component.ts');
const welcomeContent = getFileContent(tree, '/projects/ng-zorro/src/app/pages/welcome/welcome.component.ts');
Expand All @@ -51,10 +58,11 @@ describe('side-menu schematic', () => {
});

it('should fall back to the @schematics/angular:component option value', async () => {
const options = {...defaultOptions, template: 'sidemenu'};
appTree = await createTestApp(runner, {style: Style.Less});
const tree = await runner.runSchematicAsync(
'ng-add',
{template: 'sidemenu'},
options,
appTree).toPromise();

expect(tree.files).toEqual(
Expand All @@ -66,27 +74,30 @@ describe('side-menu schematic', () => {
});

it('should fall back to the @schematics/angular:component option value', async () => {
const options = {...defaultOptions, template: 'sidemenu'};
appTree = await createTestApp(runner, {inlineStyle: true});
const tree = await runner.runSchematicAsync(
'ng-add',
{template: 'sidemenu'},
options,
appTree).toPromise();

expect(tree.files).not.toEqual('/projects/ng-zorro/src/app/pages/welcome/welcome.component.css');
});

it('should fall back to the @schematics/angular:component option value', async () => {
const options = {...defaultOptions, template: 'sidemenu'};
appTree = await createTestApp(runner, {inlineTemplate: true});
const tree = await runner.runSchematicAsync(
'ng-add',
{template: 'sidemenu'},
options,
appTree).toPromise();

expect(tree.files).not.toEqual('/projects/ng-zorro/src/app/pages/welcome/welcome.component.html');
});

it('should set the prefix correctly', async () => {
const tree = await runner.runSchematicAsync('sidemenu', {prefix: 'nz'}, appTree).toPromise();
const options = {...defaultOptions, prefix: 'nz'};
const tree = await runner.runSchematicAsync('sidemenu', options, appTree).toPromise();
const appContent = getFileContent(tree, '/projects/ng-zorro/src/app/app.component.ts');
const welcomeContent = getFileContent(tree, '/projects/ng-zorro/src/app/pages/welcome/welcome.component.ts');

Expand Down
17 changes: 13 additions & 4 deletions schematics/ng-generate/topnav/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Tree } from '@angular-devkit/schematics';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import { Style } from '@schematics/angular/ng-new/schema';
import { getFileContent } from '@schematics/angular/utility/test';
import { getFileContent } from '@schematics/angular/utility/test/get-file-content';

import { Schema as NzOptions } from '../../ng-add/schema';
import { createTestApp } from '../../testing/test-app';

describe('top-nav schematic', () => {
const defaultOptions: NzOptions = {
project: 'ng-zorro-top-nav',
};
let runner: SchematicTestRunner;
let appTree: Tree;

Expand All @@ -15,7 +19,9 @@ describe('top-nav schematic', () => {
});

it('should create top-nav files', async () => {
const tree = await runner.runSchematicAsync('topnav', {}, appTree).toPromise();
const options = {...defaultOptions};

const tree = await runner.runSchematicAsync('topnav', options, appTree).toPromise();
const files = tree.files;
expect(files).toEqual(
jasmine.arrayContaining([
Expand All @@ -33,7 +39,9 @@ describe('top-nav schematic', () => {
});

it('should set the style preprocessor correctly', async () => {
const tree = await runner.runSchematicAsync('topnav', {style: Style.Less}, appTree).toPromise();
const options = {...defaultOptions, style: Style.Less};

const tree = await runner.runSchematicAsync('topnav', options, appTree).toPromise();
const files = tree.files;
const appContent = getFileContent(tree, '/projects/ng-zorro-top-nav/src/app/app.component.ts');
const welcomeContent = getFileContent(tree, '/projects/ng-zorro-top-nav/src/app/pages/welcome/welcome.component.ts');
Expand All @@ -50,7 +58,8 @@ describe('top-nav schematic', () => {
});

it('should set the prefix correctly', async () => {
const tree = await runner.runSchematicAsync('topnav', {prefix: 'nz'}, appTree).toPromise();
const options = {...defaultOptions, prefix: 'nz'};
const tree = await runner.runSchematicAsync('topnav', options, appTree).toPromise();
const appContent = getFileContent(tree, '/projects/ng-zorro-top-nav/src/app/app.component.ts');
const welcomeContent = getFileContent(tree, '/projects/ng-zorro-top-nav/src/app/pages/welcome/welcome.component.ts');

Expand Down

0 comments on commit 9d50d27

Please sign in to comment.