fix(angular): add missing skipImport option to the component generator (#10167)

This commit is contained in:
Leosvel Pérez Espinosa 2022-05-06 08:57:20 +01:00 committed by GitHub
parent 7da1913812
commit 5c94d6222f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 1 deletions

View File

@ -319,6 +319,11 @@
"description": "Create the new files at the top level of the current project.",
"default": false
},
"skipImport": {
"type": "boolean",
"description": "Do not import this component into the owning NgModule.",
"default": false
},
"selector": {
"type": "string",
"format": "html-selector",

View File

@ -100,6 +100,25 @@ export class ExampleComponent implements OnInit {
"
`;
exports[`component Generator should create the component correctly and not export it when "--skip-import=true" 1`] = `
"import { Component, OnInit } from '@angular/core';
@Component({
selector: 'example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
"
`;
exports[`component Generator should create the component correctly but not export it when no entry point exists 1`] = `
"import { Component, OnInit } from '@angular/core';

View File

@ -83,6 +83,47 @@ describe('component Generator', () => {
);
});
it('should create the component correctly and not export it when "--skip-import=true"', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace(2);
addProjectConfiguration(tree, 'lib1', {
projectType: 'library',
sourceRoot: 'libs/lib1/src',
root: 'libs/lib1',
});
tree.write(
'libs/lib1/src/lib/lib.module.ts',
`
import { NgModule } from '@angular/core';
@NgModule({
declarations: [],
exports: []
})
export class LibModule {}`
);
tree.write('libs/lib1/src/index.ts', '');
// ACT
await componentGenerator(tree, {
name: 'example',
project: 'lib1',
skipImport: true,
});
// ASSERT
const componentSource = tree.read(
'libs/lib1/src/lib/example/example.component.ts',
'utf-8'
);
expect(componentSource).toMatchSnapshot();
const indexSource = tree.read('libs/lib1/src/index.ts', 'utf-8');
expect(indexSource).not.toContain(
`export * from "./lib/example/example.component"`
);
});
it('should create the component correctly but not export it when no entry point exists', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace(2);

View File

@ -47,7 +47,7 @@ function checkPathUnderProjectRoot(tree: Tree, schema: Partial<Schema>) {
}
function exportComponent(tree: Tree, schema: Schema) {
if (!schema.export) {
if (!schema.export || schema.skipImport) {
return;
}

View File

@ -11,6 +11,7 @@ export interface Schema {
skipTests?: boolean;
type?: string;
flat?: boolean;
skipImport?: boolean;
selector?: string;
module?: string;
skipSelector?: boolean;

View File

@ -81,6 +81,11 @@
"description": "Create the new files at the top level of the current project.",
"default": false
},
"skipImport": {
"type": "boolean",
"description": "Do not import this component into the owning NgModule.",
"default": false
},
"selector": {
"type": "string",
"format": "html-selector",