refactor(core): remove testing-only event utilities from DomAdapters (#32291)

PR Close #32291
This commit is contained in:
Kara Erickson 2019-08-27 16:21:39 -07:00 committed by Miško Hevery
parent cb5701f8d9
commit f3e4cb491e
8 changed files with 38 additions and 55 deletions

View File

@ -28,8 +28,8 @@
"uncompressed": {
"runtime-es5": 2932,
"runtime-es2015": 2938,
"main-es5": 554068,
"main-es2015": 495320,
"main-es5": 552068,
"main-es2015": 493320,
"polyfills-es5": 131024,
"polyfills-es2015": 52433
}

View File

@ -3,7 +3,7 @@
"master": {
"uncompressed": {
"runtime": 1497,
"main": 164387,
"main": 161490,
"polyfills": 45399
}
}
@ -21,7 +21,7 @@
"master": {
"uncompressed": {
"runtime": 1440,
"main": 130975,
"main": 128448,
"polyfills": 45340
}
}

View File

@ -19,7 +19,7 @@ import {EmbeddedViewRef} from '@angular/core/src/linker/view_ref';
import {Attribute, Component, ContentChildren, Directive, HostBinding, HostListener, Input, Output, Pipe} from '@angular/core/src/metadata';
import {TestBed, async, fakeAsync, getTestBed, tick} from '@angular/core/testing';
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {dispatchEvent, el, isCommentNode} from '@angular/platform-browser/testing/src/browser_util';
import {createMouseEvent, dispatchEvent, el, isCommentNode} from '@angular/platform-browser/testing/src/browser_util';
import {expect} from '@angular/platform-browser/testing/src/matchers';
import {modifiedInIvy, obsoleteInIvy, onlyInIvy} from '@angular/private/testing';
@ -1000,12 +1000,12 @@ function declareTests(config?: {useJit: boolean}) {
TestBed.overrideComponent(MyComp, {set: {template}});
const fixture = TestBed.createComponent(MyComp);
const dispatchedEvent = getDOM().createMouseEvent('click');
const dispatchedEvent2 = getDOM().createMouseEvent('click');
const dispatchedEvent = createMouseEvent('click');
const dispatchedEvent2 = createMouseEvent('click');
getDOM().dispatchEvent(fixture.debugElement.children[0].nativeElement, dispatchedEvent);
getDOM().dispatchEvent(fixture.debugElement.children[1].nativeElement, dispatchedEvent2);
expect(getDOM().isPrevented(dispatchedEvent)).toBe(true);
expect(getDOM().isPrevented(dispatchedEvent2)).toBe(false);
expect(isPrevented(dispatchedEvent)).toBe(true);
expect(isPrevented(dispatchedEvent2)).toBe(false);
expect(fixture.debugElement.children[0].nativeElement.checked).toBeFalsy();
expect(fixture.debugElement.children[1].nativeElement.checked).toBeTruthy();
});
@ -1493,9 +1493,8 @@ function declareTests(config?: {useJit: boolean}) {
expect(noSelectorComponentFactory.selector).toBe('ng-component');
expect(
noSelectorComponentFactory.create(Injector.NULL).location.nativeElement.nodeName
.toLowerCase())
expect(noSelectorComponentFactory.create(Injector.NULL)
.location.nativeElement.nodeName.toLowerCase())
.toEqual('ng-component');
});
});
@ -1961,7 +1960,7 @@ function declareTests(config?: {useJit: boolean}) {
fixture.detectChanges();
const dir = fixture.debugElement.children[0].injector.get(DirectiveWithPropDecorators);
const native = fixture.debugElement.children[0].nativeElement;
getDOM().dispatchEvent(native, getDOM().createMouseEvent('click'));
getDOM().dispatchEvent(native, createMouseEvent('click'));
expect(dir.target).toBe(native);
});
@ -2745,3 +2744,7 @@ export class ParentCmp {
class SomeCmpWithInput {
@Input() test$: any;
}
function isPrevented(evt: Event): boolean {
return evt.defaultPrevented || evt.returnValue != null && !evt.returnValue;
}

View File

@ -113,7 +113,6 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
contains(nodeA: any, nodeB: any): boolean { return nodeContains.call(nodeA, nodeB); }
querySelector(el: HTMLElement, selector: string): any { return el.querySelector(selector); }
querySelectorAll(el: any, selector: string): any[] { return el.querySelectorAll(selector); }
on(el: Node, evt: any, listener: any) { el.addEventListener(evt, listener, false); }
onAndCancel(el: Node, evt: any, listener: any): Function {
el.addEventListener(evt, listener, false);
// Needed to follow Dart's subscription semantic, until fix of
@ -121,23 +120,6 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
return () => { el.removeEventListener(evt, listener, false); };
}
dispatchEvent(el: Node, evt: any) { el.dispatchEvent(evt); }
createMouseEvent(eventType: string): MouseEvent {
const evt: MouseEvent = this.getDefaultDocument().createEvent('MouseEvent');
evt.initEvent(eventType, true, true);
return evt;
}
createEvent(eventType: any): Event {
const evt: Event = this.getDefaultDocument().createEvent('Event');
evt.initEvent(eventType, true, true);
return evt;
}
preventDefault(evt: Event) {
evt.preventDefault();
evt.returnValue = false;
}
isPrevented(evt: Event): boolean {
return evt.defaultPrevented || evt.returnValue != null && !evt.returnValue;
}
nextSibling(el: Node): Node|null { return el.nextSibling; }
parentElement(el: Node): Node|null { return el.parentNode; }
clearNodes(el: Node) {

View File

@ -6,7 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Type} from '@angular/core';
let _DOM: DomAdapter = null !;
@ -45,13 +44,7 @@ export abstract class DomAdapter {
abstract parse(templateHtml: string): any;
abstract querySelector(el: any, selector: string): any;
abstract querySelectorAll(el: any, selector: string): any[];
abstract on(el: any, evt: any, listener: any): any;
abstract onAndCancel(el: any, evt: any, listener: any): Function;
abstract dispatchEvent(el: any, evt: any): any;
abstract createMouseEvent(eventType: any): any;
abstract createEvent(eventType: string): any;
abstract preventDefault(evt: any): any;
abstract isPrevented(evt: any): boolean;
// Used by Meta
abstract remove(el: any): Node;
@ -95,6 +88,7 @@ export abstract class DomAdapter {
abstract getHost(el: any): any;
// Used by KeyEventsPlugin
abstract onAndCancel(el: any, evt: any, listener: any): Function;
abstract getEventKey(event: any): string;
abstract supportsDOMEvents(): boolean;

View File

@ -11,7 +11,8 @@ import {beforeEach, describe, expect, it} from '@angular/core/testing/src/testin
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {DomEventsPlugin} from '@angular/platform-browser/src/dom/events/dom_events';
import {EventManager, EventManagerPlugin} from '@angular/platform-browser/src/dom/events/event_manager';
import {el} from '../../../testing/src/browser_util';
import {createMouseEvent, el} from '../../../testing/src/browser_util';
(function() {
if (isNode) return;
@ -63,8 +64,8 @@ import {el} from '../../../testing/src/browser_util';
// Workaround for https://bugs.webkit.org/show_bug.cgi?id=122755
getDOM().appendChild(doc.body, element);
const child = element.firstChild;
const dispatchedEvent = getDOM().createMouseEvent('click');
const child = element.firstChild as Element;
const dispatchedEvent = createMouseEvent('click');
let receivedEvent: any /** TODO #9100 */ = null;
const handler = (e: any /** TODO #9100 */) => { receivedEvent = e; };
const manager = new EventManager([domEventPlugin], new FakeNgZone());
@ -77,7 +78,7 @@ import {el} from '../../../testing/src/browser_util';
it('should add and remove global event listeners', () => {
const element = el('<div><div></div></div>');
getDOM().appendChild(doc.body, element);
const dispatchedEvent = getDOM().createMouseEvent('click');
const dispatchedEvent = createMouseEvent('click');
let receivedEvent: any /** TODO #9100 */ = null;
const handler = (e: any /** TODO #9100 */) => { receivedEvent = e; };
const manager = new EventManager([domEventPlugin], new FakeNgZone());
@ -97,7 +98,7 @@ import {el} from '../../../testing/src/browser_util';
const element = el('<div><div></div></div>');
getDOM().appendChild(doc.body, element);
const dispatchedEvent = getDOM().createMouseEvent('click');
const dispatchedEvent = createMouseEvent('click');
let receivedEvent: any /** TODO #9100 */ = null;
let receivedZone: any = null;
const handler = (e: any /** TODO #9100 */) => {
@ -123,7 +124,7 @@ import {el} from '../../../testing/src/browser_util';
const element = el('<div><div></div></div>');
getDOM().appendChild(doc.body, element);
const dispatchedEvent = getDOM().createMouseEvent('click');
const dispatchedEvent = createMouseEvent('click');
let receivedEvents: any[] /** TODO #9100 */ = [];
let receivedZones: any[] = [];
const handler1 = (e: any /** TODO #9100 */) => {
@ -158,7 +159,7 @@ import {el} from '../../../testing/src/browser_util';
const element = el('<div><div></div></div>');
getDOM().appendChild(doc.body, element);
const dispatchedEvent = getDOM().createMouseEvent('click');
const dispatchedEvent = createMouseEvent('click');
let receivedEvents: any[] /** TODO #9100 */ = [];
let receivedZones: any[] = [];
const handler1 = (e: any /** TODO #9100 */) => {
@ -194,7 +195,7 @@ import {el} from '../../../testing/src/browser_util';
const element = el('<div><div></div></div>');
getDOM().appendChild(doc.body, element);
const dispatchedEvent = getDOM().createMouseEvent('click');
const dispatchedEvent = createMouseEvent('click');
let receivedEvents: any[] /** TODO #9100 */ = [];
let receivedZones: any[] = [];
let remover1: any = null;
@ -230,7 +231,7 @@ import {el} from '../../../testing/src/browser_util';
const element = el('<div><div></div></div>');
getDOM().appendChild(doc.body, element);
const dispatchedEvent = getDOM().createMouseEvent('click');
const dispatchedEvent = createMouseEvent('click');
let receivedEvents: any[] /** TODO #9100 */ = [];
let receivedZones: any[] = [];
const handler = (e: any /** TODO #9100 */) => {
@ -261,7 +262,7 @@ import {el} from '../../../testing/src/browser_util';
const element = el('<div><div></div></div>');
getDOM().appendChild(doc.body, element);
const dispatchedEvent = getDOM().createMouseEvent('click');
const dispatchedEvent = createMouseEvent('click');
let receivedEvents: any[] /** TODO #9100 */ = [];
let receivedZones: any[] = [];
const handler1 = (e: any /** TODO #9100 */) => {
@ -300,7 +301,7 @@ import {el} from '../../../testing/src/browser_util';
const Zone = (window as any)['Zone'];
const element = el('<div><div></div></div>');
getDOM().appendChild(doc.body, element);
const dispatchedEvent = getDOM().createMouseEvent('scroll');
const dispatchedEvent = createMouseEvent('scroll');
let receivedEvent: any /** TODO #9100 */ = null;
let receivedZone: any = null;
const handler = (e: any /** TODO #9100 */) => {

View File

@ -91,7 +91,15 @@ export class BrowserDetection {
BrowserDetection.setup();
export function dispatchEvent(element: any, eventType: any): void {
getDOM().dispatchEvent(element, getDOM().createEvent(eventType));
const evt: Event = getDOM().getDefaultDocument().createEvent('Event');
evt.initEvent(eventType, true, true);
getDOM().dispatchEvent(element, evt);
}
export function createMouseEvent(eventType: string): MouseEvent {
const evt: MouseEvent = getDOM().getDefaultDocument().createEvent('MouseEvent');
evt.initEvent(eventType, true, true);
return evt;
}
export function el(html: string): HTMLElement {

View File

@ -54,13 +54,8 @@ export class WorkerDomAdapter extends DomAdapter {
parse(templateHtml: string) { throw 'not implemented'; }
querySelector(el: any, selector: string): HTMLElement { throw 'not implemented'; }
querySelectorAll(el: any, selector: string): any[] { throw 'not implemented'; }
on(el: any, evt: any, listener: any) { throw 'not implemented'; }
onAndCancel(el: any, evt: any, listener: any): Function { throw 'not implemented'; }
dispatchEvent(el: any, evt: any) { throw 'not implemented'; }
createMouseEvent(eventType: any): any { throw 'not implemented'; }
createEvent(eventType: string): any { throw 'not implemented'; }
preventDefault(evt: any) { throw 'not implemented'; }
isPrevented(evt: any): boolean { throw 'not implemented'; }
nextSibling(el: any): Node { throw 'not implemented'; }
parentElement(el: any): Node { throw 'not implemented'; }
clearNodes(el: any) { throw 'not implemented'; }