Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/schematics/angular/component/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,25 @@ describe('Component Schematic', () => {
expect(appModule).toMatch(/import { FooComponent } from '.\/foo\/foo.component'/);
});

it('should declare the class name without the type when addTypeToClassName is false', async () => {
const options = {
...defaultNonStandaloneOptions,
module: 'app-module.ts',
addTypeToClassName: false,
export: true,
};

const tree = await schematicRunner.runSchematic('component', options, appTree);
const component = tree.readContent('/projects/baz/src/app/foo/foo.component.ts');
const appModule = tree.readContent('/projects/baz/src/app/app-module.ts');

expect(component).toContain('export class Foo {');
expect(appModule).toMatch(/import { Foo } from '.\/foo\/foo.component'/);
expect(appModule).toMatch(/declarations: \[\n\s*App,\n\s*Foo\n/);
Comment thread
alan-agius4 marked this conversation as resolved.
expect(appModule).toMatch(/exports: \[\n(\s*) {2}Foo\n/);
expect(appModule).not.toContain('FooComponent');
});

it('should respect the sourceRoot value', async () => {
const config = JSON.parse(appTree.readContent('/angular.json'));
config.projects.baz.sourceRoot = 'projects/baz/custom';
Expand Down
17 changes: 17 additions & 0 deletions packages/schematics/angular/directive/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,23 @@ describe('Directive Schematic', () => {
expect(appModule).toMatch(/import { Foo } from '.\/foo'/);
});

it('should declare the class name without the type when addTypeToClassName is false', async () => {
const options = {
...defaultNonStandaloneOptions,
module: 'app-module.ts',
type: 'directive',
addTypeToClassName: false,
};

const tree = await schematicRunner.runSchematic('directive', options, appTree);
const directive = tree.readContent('/projects/baz/src/app/foo.directive.ts');
const appModule = tree.readContent('/projects/baz/src/app/app-module.ts');

expect(directive).toContain('export class Foo {');
expect(appModule).toMatch(/import { Foo } from '.\/foo.directive'/);
expect(appModule).not.toContain('FooDirective');
});

it('should fail if specified module does not exist', async () => {
const options = {
...defaultNonStandaloneOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export interface DeclarationToNgModuleOptions {
export?: boolean;
type: string;
typeSeparator?: '.' | '-';

/** Whether the type is part of the class name. Defaults to `true`. */
addTypeToClassName?: boolean;
skipImport?: boolean;
standalone?: boolean;
}
Expand All @@ -44,7 +47,8 @@ export function addDeclarationToNgModule(options: DeclarationToNgModuleOptions):

const importPath = buildRelativePath(modulePath, filePath);
const classifiedName =
strings.classify(options.name) + (options.type ? strings.classify(options.type) : '');
strings.classify(options.name) +
(options.type && options.addTypeToClassName !== false ? strings.classify(options.type) : '');
const changes = addDeclarationToModule(source, modulePath, classifiedName, importPath);

if (options.export) {
Expand Down
Loading