Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(angular): add component import path correctly to NgModule when flat=false #16364

Merged
merged 1 commit into from
Apr 19, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ export class ExampleComponent {}
"
`;

exports[`component Generator --module should import the component correctly to the module file when flat is false 1`] = `
"import { NgModule } from '@angular/core';
import { ExampleComponent } from './example/example.component';

@NgModule({
declarations: [ExampleComponent],
exports: [ExampleComponent],
})
export class LibModule {}
"
`;

exports[`component Generator --path should create the component correctly and export it in the entry point 1`] = `
"import { Component } from '@angular/core';

Expand Down
40 changes: 40 additions & 0 deletions packages/angular/src/generators/component/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,46 @@ describe('component Generator', () => {
}
);

it('should import the component correctly to the module file when flat is false', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
addProjectConfiguration(tree, 'shared-ui', {
projectType: 'library',
sourceRoot: 'libs/shared/ui/src',
root: 'libs/shared/ui',
});
tree.write(
'libs/shared/ui/src/lib/lib.module.ts',
`
import { NgModule } from '@angular/core';

@NgModule({
declarations: [],
exports: []
})
export class LibModule {}`
);
tree.write(
'libs/shared/ui/src/index.ts',
'export * from "./lib/lib.module";'
);

// ACT
await componentGenerator(tree, {
name: 'example',
project: 'shared-ui',
export: true,
flat: false,
});

// ASSERT
const moduleSource = tree.read(
'libs/shared/ui/src/lib/lib.module.ts',
'utf-8'
);
expect(moduleSource).toMatchSnapshot();
});

it('should not export it in the entry point when the module it belong to is not exported', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
Expand Down
7 changes: 1 addition & 6 deletions packages/angular/src/generators/component/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,7 @@ export async function componentGenerator(tree: Tree, rawOptions: Schema) {
modulePath,
componentNames.fileName,
`${componentNames.className}${typeNames.className}`,
options.flat
? `${componentNames.fileName}.${typeNames.fileName}`
: joinPathFragments(
componentNames.fileName,
`${componentNames.fileName}.${typeNames.fileName}`
),
`${componentNames.fileName}.${typeNames.fileName}`,
'declarations',
options.flat,
options.export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function normalizeOptions(
name,
changeDetection: options.changeDetection ?? 'Default',
style: options.style ?? 'css',
flat: options.flat ?? false,
directory,
filePath,
path,
Expand Down