fix(core): fix tests for uninitialized @Output error (#19116)

PR Close #19116
This commit is contained in:
Aldo Roman Nureña 2018-01-16 12:30:28 -05:00 committed by Igor Minar
parent adf510f986
commit 183757daa2
3 changed files with 13 additions and 10 deletions

View File

@ -21,6 +21,7 @@ export function isPromise(obj: any): obj is Promise<any> {
* Determine if the argument is an Observable * Determine if the argument is an Observable
*/ */
export function isObservable(obj: any | Observable<any>): obj is Observable<any> { export function isObservable(obj: any | Observable<any>): obj is Observable<any> {
// TODO: use Symbol.observable when https://github.com/ReactiveX/rxjs/issues/2415 will be resolved // TODO: use isObservable once we update pass rxjs 6.1
// https://github.com/ReactiveX/rxjs/blob/master/CHANGELOG.md#610-2018-05-03
return !!obj && typeof obj.subscribe === 'function'; return !!obj && typeof obj.subscribe === 'function';
} }

View File

@ -13,6 +13,7 @@ import {TemplateRef} from '../linker/template_ref';
import {ViewContainerRef} from '../linker/view_container_ref'; import {ViewContainerRef} from '../linker/view_container_ref';
import {Renderer as RendererV1, Renderer2} from '../render/api'; import {Renderer as RendererV1, Renderer2} from '../render/api';
import {stringify} from '../util'; import {stringify} from '../util';
import {isObservable} from '../util/lang';
import {createChangeDetectorRef, createInjector, createRendererV1} from './refs'; import {createChangeDetectorRef, createInjector, createRendererV1} from './refs';
import {BindingDef, BindingFlags, DepDef, DepFlags, NodeDef, NodeFlags, OutputDef, OutputType, ProviderData, QueryValueType, Services, ViewData, ViewFlags, ViewState, asElementData, asProviderData, shouldCallLifecycleInitHook} from './types'; import {BindingDef, BindingFlags, DepDef, DepFlags, NodeDef, NodeFlags, OutputDef, OutputType, ProviderData, QueryValueType, Services, ViewData, ViewFlags, ViewState, asElementData, asProviderData, shouldCallLifecycleInitHook} from './types';
@ -138,7 +139,7 @@ export function createDirectiveInstance(view: ViewData, def: NodeDef): any {
for (let i = 0; i < def.outputs.length; i++) { for (let i = 0; i < def.outputs.length; i++) {
const output = def.outputs[i]; const output = def.outputs[i];
const outputObservable = instance[output.propName !]; const outputObservable = instance[output.propName !];
if (typeof outputObservable === 'object') { if (isObservable(outputObservable)) {
const subscription = outputObservable.subscribe( const subscription = outputObservable.subscribe(
eventHandlerClosure(view, def.parent !.nodeIndex, output.eventName)); eventHandlerClosure(view, def.parent !.nodeIndex, output.eventName));
view.disposables ![def.outputIndex + i] = subscription.unsubscribe.bind(subscription); view.disposables ![def.outputIndex + i] = subscription.unsubscribe.bind(subscription);

View File

@ -347,17 +347,18 @@ function declareTests({useJit}: {useJit: boolean}) {
}); });
it('should display correct error message for uninitialized @Output', () => { it('should display correct error message for uninitialized @Output', () => {
@Directive({selector: '[uninitializedOuput]'}) @Component({selector: 'my-uninitialized-output', template: '<p>It works!</p>'})
class UninitializedOuput { class UninitializedOutputComp {
@Output() customEvent; @Output() customEvent !: EventEmitter<any>;
doSomething() {
}
} }
TestBed.configureTestingModule({declarations: [MyComp, UninitializedOuput]}); const template =
const template = '<p (customEvent)="doNothing()"></p>'; '<my-uninitialized-output (customEvent)="doNothing()"></my-uninitialized-output>';
TestBed.overrideComponent(MyComp, {set: {template}}); TestBed.overrideComponent(MyComp, {set: {template}});
TestBed.createComponent(MyComp).toThrowError('@Output customEvent not initialized in \'UninitializedOuput\'.');
TestBed.configureTestingModule({declarations: [MyComp, UninitializedOutputComp]});
expect(() => TestBed.createComponent(MyComp))
.toThrowError('@Output customEvent not initialized in \'UninitializedOutputComp\'.');
}); });
it('should read directives metadata from their binding token', () => { it('should read directives metadata from their binding token', () => {