Skip to content

Commit

Permalink
fix(angular): add component import path correctly to NgModule when fl…
Browse files Browse the repository at this point in the history
…at=false (#16364)
  • Loading branch information
Coly010 authored Apr 19, 2023
1 parent 3d3cc74 commit 0ce1f37
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
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

0 comments on commit 0ce1f37

Please sign in to comment.