fix(ivy): ensure eventListeners added outside angular context are not called... (#34514)
by DebugElement.triggerEventHandler. ZoneJS tracks the eventListeners on a node but we need to be able to differentiate between those added by Angular and those that were added outside the Angular context. This fix aligns with the behavior that was present in View Engine (not calling those listeners). If we decide later that we want to call those listeners, we still need a way to differentiate between those that we have wrapped in dom_renderer and those that were not (because they were added outside the Angular context). PR Close #34514
This commit is contained in:

committed by
Andrew Kushnir

parent
d15cf60c49
commit
32b72f39f0
@ -12,7 +12,7 @@ import {Component, DebugElement, DebugNode, Directive, ElementRef, EmbeddedViewR
|
||||
import {NgZone} from '@angular/core/src/zone';
|
||||
import {ComponentFixture, TestBed, async} from '@angular/core/testing';
|
||||
import {By} from '@angular/platform-browser/src/dom/debug/by';
|
||||
import {hasClass} from '@angular/platform-browser/testing/src/browser_util';
|
||||
import {createMouseEvent, hasClass} from '@angular/platform-browser/testing/src/browser_util';
|
||||
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
||||
import {ivyEnabled, onlyInIvy} from '@angular/private/testing';
|
||||
|
||||
@ -1253,4 +1253,23 @@ class TestCmptWithPropInterpolation {
|
||||
expect(fixture.debugElement.query(e => e.name === 'myComponent')).toBeTruthy();
|
||||
expect(fixture.debugElement.query(e => e.name === 'div')).toBeTruthy();
|
||||
});
|
||||
|
||||
it('does not call event listeners added outside angular context', () => {
|
||||
let listenerCalled = false;
|
||||
const eventToTrigger = createMouseEvent('mouseenter');
|
||||
function listener() { listenerCalled = true; }
|
||||
@Component({template: ''})
|
||||
class MyComp {
|
||||
constructor(private readonly zone: NgZone, private readonly element: ElementRef) {}
|
||||
ngOnInit() {
|
||||
this.zone.runOutsideAngular(
|
||||
() => { this.element.nativeElement.addEventListener('mouseenter', listener); });
|
||||
}
|
||||
}
|
||||
const fixture =
|
||||
TestBed.configureTestingModule({declarations: [MyComp]}).createComponent(MyComp);
|
||||
fixture.detectChanges();
|
||||
fixture.debugElement.triggerEventHandler('mouseenter', eventToTrigger);
|
||||
expect(listenerCalled).toBe(false);
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user