Skip to content

Commit

Permalink
fix: cannot combine @input decorators with query decorators
Browse files Browse the repository at this point in the history
closes #181

Signed-off-by: MG <m@sudo.eu>
  • Loading branch information
satanTime committed Sep 5, 2020
1 parent 587119e commit 7cda85d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/common/decorate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import { Type } from './lib';
*
* @internal
*/
export function decorateInputs(cls: Type<any>, inputs?: string[]) {
export function decorateInputs(cls: Type<any>, inputs?: string[], exclude?: string[]) {
if (inputs) {
for (const input of inputs) {
const [key, alias] = input.split(': ');
if (exclude && exclude.indexOf(key) !== -1) {
continue;
}
Input(alias)(cls.prototype, key);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/mock-component/mock-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export function MockComponent<TComponent>(
}
}

decorateInputs(ComponentMock, inputs);
decorateInputs(ComponentMock, inputs, queries ? Object.keys(queries) : undefined);
decorateOutputs(ComponentMock, outputs);
decorateQueries(ComponentMock, queries);

Expand Down
2 changes: 1 addition & 1 deletion lib/mock-directive/mock-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export function MockDirective<TDirective>(directive: Type<TDirective>): Type<Moc
}
}

decorateInputs(DirectiveMock, inputs);
decorateInputs(DirectiveMock, inputs, queries ? Object.keys(queries) : undefined);
decorateOutputs(DirectiveMock, outputs);
decorateQueries(DirectiveMock, queries);

Expand Down
63 changes: 63 additions & 0 deletions tests/issue-181/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// tslint:disable: no-parameter-properties

import { CommonModule } from '@angular/common';
import { ContentChild, Directive, NgModule } from '@angular/core';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

@Directive({
inputs: ['header'],
selector: 'app-target',
})
class TargetComponent {
@ContentChild('header', { static: false } as any)
public header: any;
}

@Directive({
inputs: ['header'],
selector: '[appTarget]',
})
class TargetDirective {
@ContentChild('header', { static: false } as any)
public header: any;
}

@NgModule({
declarations: [TargetComponent, TargetDirective],
exports: [TargetComponent, TargetDirective],
imports: [CommonModule],
})
class TargetModule {}

describe('issue-181:real', () => {
beforeEach(() => MockBuilder().keep(TargetModule));

it('should create the component', () => {
const fixture = MockRender(`
<app-target appTarget>
<ng-template #header>
header
</ng-template>
</app-target>
`);
expect(ngMocks.findInstance(fixture.debugElement, TargetComponent).header).toBeTruthy();
expect(ngMocks.findInstance(fixture.debugElement, TargetDirective).header).toBeTruthy();
});
});

describe('issue-181:mock', () => {
beforeEach(() => MockBuilder().mock(TargetModule));

// it fails in e2e with enabled ivy only.
it('should create the component', () => {
const fixture = MockRender(`
<app-target appTarget>
<ng-template #header>
header
</ng-template>
</app-target>
`);
expect(ngMocks.findInstance(fixture.debugElement, TargetComponent).header).toBeTruthy();
expect(ngMocks.findInstance(fixture.debugElement, TargetDirective).header).toBeTruthy();
});
});

0 comments on commit 7cda85d

Please sign in to comment.