fix(core): fix tests for uninitialized @Output error (#19116)
PR Close #19116
This commit is contained in:
parent
adf510f986
commit
183757daa2
|
@ -21,6 +21,7 @@ export function isPromise(obj: any): obj is Promise<any> {
|
|||
* Determine if the argument is an Observable
|
||||
*/
|
||||
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';
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ import {TemplateRef} from '../linker/template_ref';
|
|||
import {ViewContainerRef} from '../linker/view_container_ref';
|
||||
import {Renderer as RendererV1, Renderer2} from '../render/api';
|
||||
import {stringify} from '../util';
|
||||
import {isObservable} from '../util/lang';
|
||||
|
||||
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';
|
||||
|
@ -138,7 +139,7 @@ export function createDirectiveInstance(view: ViewData, def: NodeDef): any {
|
|||
for (let i = 0; i < def.outputs.length; i++) {
|
||||
const output = def.outputs[i];
|
||||
const outputObservable = instance[output.propName !];
|
||||
if (typeof outputObservable === 'object') {
|
||||
if (isObservable(outputObservable)) {
|
||||
const subscription = outputObservable.subscribe(
|
||||
eventHandlerClosure(view, def.parent !.nodeIndex, output.eventName));
|
||||
view.disposables ![def.outputIndex + i] = subscription.unsubscribe.bind(subscription);
|
||||
|
|
|
@ -347,17 +347,18 @@ function declareTests({useJit}: {useJit: boolean}) {
|
|||
});
|
||||
|
||||
it('should display correct error message for uninitialized @Output', () => {
|
||||
@Directive({selector: '[uninitializedOuput]'})
|
||||
class UninitializedOuput {
|
||||
@Output() customEvent;
|
||||
doSomething() {
|
||||
}
|
||||
@Component({selector: 'my-uninitialized-output', template: '<p>It works!</p>'})
|
||||
class UninitializedOutputComp {
|
||||
@Output() customEvent !: EventEmitter<any>;
|
||||
}
|
||||
|
||||
TestBed.configureTestingModule({declarations: [MyComp, UninitializedOuput]});
|
||||
const template = '<p (customEvent)="doNothing()"></p>';
|
||||
const template =
|
||||
'<my-uninitialized-output (customEvent)="doNothing()"></my-uninitialized-output>';
|
||||
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', () => {
|
||||
|
|
Loading…
Reference in New Issue