Skip to content

Commit

Permalink
fix(schematics): handle imports of complex typings for input decorate…
Browse files Browse the repository at this point in the history
…d fields in lazy-component schematic (#254)
  • Loading branch information
dhhyi authored May 25, 2020
1 parent 447317b commit b1b4ec3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 2 additions & 4 deletions schematics/src/lazy-component/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ export function createLazyComponent(options: Options): Rule {
}));

const importTypes = bindingNodes
.map(node => node.getChildAt(3))
.filter(node => ts.isTypeReferenceNode(node))
.map(node => node.getText());
.map(node => tsquery(node, 'TypeReference > Identifier').map(identifier => identifier.getText()))
.reduce((acc, val) => acc.concat(...val), []);

if (importTypes.length) {
const importDeclarations = tsquery(componentSource, 'ImportDeclaration') as ts.ImportDeclaration[];
Expand All @@ -82,7 +81,6 @@ export function createLazyComponent(options: Options): Rule {
: [],
}))
.filter(decl => decl.types.length);
// .forEach(t => console.log(t));
}
}

Expand Down
12 changes: 12 additions & 0 deletions schematics/src/lazy-component/factory_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,11 @@ describe('Lazy Component Schematic', () => {
appTree.overwrite(
'/src/app/extensions/ext/shared/group/dummy/dummy.component.ts',
`import { ChangeDetectionStrategy, Component, Input, Output } from '@angular/core';
import { Observable } from 'rxjs';
import { Product, ProductHelper } from 'ish-core/models/product/product.model';
import { User } from 'ish-core/models/user/user.model';
import { Customer } from 'ish-core/models/customer/customer.model';
@Component({
selector: 'ish-dummy',
Expand All @@ -202,6 +205,8 @@ export class DummyComponent {
@Input() complexTyped: 'a' | 'b';
@Input() complexTypedInitialized: 'a' | 'b' = 'a';
@Input() importTyped: Product;
@Input() importComplexTyped: User[];
@Input() importGenericTyped: Observable<Customer[]>;
}
`
);
Expand All @@ -222,6 +227,8 @@ export class DummyComponent {
expect(componentContent).toContain("@Input() complexTyped: 'a' | 'b';");
expect(componentContent).toContain("@Input() complexTypedInitialized: 'a' | 'b' = 'a';");
expect(componentContent).toContain('@Input() importTyped: Product;');
expect(componentContent).toContain('@Input() importComplexTyped: User[];');
expect(componentContent).toContain('@Input() importGenericTyped: Observable<Customer[]>;');
});

it('should transfer inputs', () => {
Expand All @@ -230,10 +237,15 @@ export class DummyComponent {
expect(componentContent).toContain('component.instance.complexTyped = this.complexTyped');
expect(componentContent).toContain('component.instance.complexTypedInitialized = this.complexTypedInitialized');
expect(componentContent).toContain('component.instance.importTyped = this.importTyped');
expect(componentContent).toContain('component.instance.importComplexTyped = this.importComplexTyped');
expect(componentContent).toContain('component.instance.importGenericTyped = this.importGenericTyped');
});

it('should copy imports', () => {
expect(componentContent).toContain("import { Product } from 'ish-core/models/product/product.model';");
expect(componentContent).toContain("import { User } from 'ish-core/models/user/user.model';");
expect(componentContent).toContain("import { Observable } from 'rxjs';");
expect(componentContent).toContain("import { Customer } from 'ish-core/models/customer/customer.model';");
});
});

Expand Down

0 comments on commit b1b4ec3

Please sign in to comment.