Skip to content

Commit

Permalink
feat: Support Angular propDecorators inputs and outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Domingue authored and ike18t committed Mar 25, 2018
1 parent b13ddb4 commit add374d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
16 changes: 16 additions & 0 deletions lib/common/reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,21 @@ export function getInputsOutputs(directive: Type<Component | Directive>, type: '
return Object.keys(propertyMetadata)
.filter((meta) => propertyMetadata[meta].find((m: any) => m.ngMetadataName === type))
.reduce(metaReducer(propertyMetadata), [])
.concat(getPropDecorators(directive, type))
.concat(getInputsOutputs((directive as any).__proto__, type));
}

function getPropDecorators(directive: any, type: 'Input' | 'Output') {
const decorators = directive.propDecorators;
if (!decorators) {
return [];
}
const fields: any[] = [];
Object.keys(decorators).forEach((propName) => {
(decorators[propName] as any[])
.filter((decorator: any) => decorator.type.prototype.ngMetadataName === type)
.forEach((decorator: any) =>
fields.push(`${propName}:${decorator.args[0] || propName}`));
});
return fields;
}
10 changes: 6 additions & 4 deletions lib/mock-directive/mock-directive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, Directive, EventEmitter, Input, Output } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormControlDirective } from '@angular/forms';
import { FormControl, FormControlDirective } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { MockDirective } from './mock-directive';

Expand All @@ -19,10 +19,12 @@ export class ExampleDirective {
template: `
<div [exampleDirective]="'bye'" [bah]="'hi'" #f="foo" (someOutput)="emitted = $event"></div>
<div exampleDirective></div>
<input [formControl]="fooControl"/>
`
})
export class ExampleComponentContainer {
emitted = false;
foo = new FormControl('');
} // tslint:disable-line:max-classes-per-file

describe('MockDirective', () => {
Expand All @@ -32,6 +34,7 @@ describe('MockDirective', () => {
TestBed.configureTestingModule({
declarations: [
ExampleComponentContainer,
MockDirective(FormControlDirective),
MockDirective(ExampleDirective)
]
})
Expand Down Expand Up @@ -73,8 +76,7 @@ describe('MockDirective', () => {
// I found that FormControlDirective is one of those weird directives.
// Since I don't know how they did it, I don't know how to test it except to write this
// Test around a known-odd directive.
expect(() => {
MockDirective(FormControlDirective);
}).not.toThrow();
const debugElement = fixture.debugElement.query(By.directive(MockDirective(ExampleDirective)));
expect(debugElement).not.toBeNull();
});
});

0 comments on commit add374d

Please sign in to comment.