2017-01-20 16:10:57 -05:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2017-01-20 16:10:57 -05:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2019-08-22 22:16:25 -04:00
|
|
|
import {ɵgetDOM as getDOM} from '@angular/common';
|
2020-04-13 19:40:21 -04:00
|
|
|
import {ErrorHandler, getDebugNode, SecurityContext} from '@angular/core';
|
2017-01-27 16:19:00 -05:00
|
|
|
import {getDebugContext} from '@angular/core/src/errors';
|
2020-04-13 19:40:21 -04:00
|
|
|
import {asElementData, BindingFlags, elementDef, NodeFlags, Services, ViewData, ViewDefinition} from '@angular/core/src/view/index';
|
2017-04-28 14:50:45 -04:00
|
|
|
import {TestBed} from '@angular/core/testing';
|
2017-01-20 16:10:57 -05:00
|
|
|
|
2017-09-22 17:29:16 -04:00
|
|
|
import {ARG_TYPE_VALUES, callMostRecentEventListenerHandler, checkNodeInlineOrDynamic, compViewDef, createAndGetRootNodes, isBrowser, recordNodeToRemove} from './helper';
|
|
|
|
|
2017-08-31 02:52:51 -04:00
|
|
|
|
2017-01-20 16:10:57 -05:00
|
|
|
|
2017-07-13 18:36:11 -04:00
|
|
|
/**
|
|
|
|
* We map addEventListener to the Zones internal name. This is because we want to be fast
|
|
|
|
* and bypass the zone bookkeeping. We know that we can do the bookkeeping faster.
|
|
|
|
*/
|
2019-11-09 11:48:07 -05:00
|
|
|
const addEventListener = 'addEventListener';
|
|
|
|
const removeEventListener = 'removeEventListener';
|
2017-07-13 18:36:11 -04:00
|
|
|
|
2017-12-15 19:28:41 -05:00
|
|
|
{
|
2017-02-03 18:20:50 -05:00
|
|
|
describe(`View Elements`, () => {
|
2017-01-20 16:10:57 -05:00
|
|
|
describe('create', () => {
|
|
|
|
it('should create elements without parents', () => {
|
2017-01-23 19:59:20 -05:00
|
|
|
const rootNodes = createAndGetRootNodes(compViewDef([
|
2017-09-22 17:29:16 -04:00
|
|
|
elementDef(0, NodeFlags.None, null, null, 0, 'span')
|
2017-01-23 19:59:20 -05:00
|
|
|
])).rootNodes;
|
2017-01-20 16:10:57 -05:00
|
|
|
expect(rootNodes.length).toBe(1);
|
2019-08-24 11:18:00 -04:00
|
|
|
expect(rootNodes[0].nodeName.toLowerCase()).toBe('span');
|
2017-01-20 16:10:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should create views with multiple root elements', () => {
|
2017-01-23 19:59:20 -05:00
|
|
|
const rootNodes = createAndGetRootNodes(compViewDef([
|
2017-09-22 17:29:16 -04:00
|
|
|
elementDef(0, NodeFlags.None, null, null, 0, 'span'),
|
|
|
|
elementDef(1, NodeFlags.None, null, null, 0, 'span'),
|
2017-01-23 19:59:20 -05:00
|
|
|
])).rootNodes;
|
2017-01-20 16:10:57 -05:00
|
|
|
expect(rootNodes.length).toBe(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create elements with parents', () => {
|
|
|
|
const rootNodes = createAndGetRootNodes(compViewDef([
|
2017-09-22 17:29:16 -04:00
|
|
|
elementDef(0, NodeFlags.None, null, null, 1, 'div'),
|
|
|
|
elementDef(1, NodeFlags.None, null, null, 0, 'span'),
|
2017-01-20 16:10:57 -05:00
|
|
|
])).rootNodes;
|
|
|
|
expect(rootNodes.length).toBe(1);
|
2019-08-24 11:01:24 -04:00
|
|
|
const spanEl = rootNodes[0].childNodes[0];
|
2019-08-24 11:18:00 -04:00
|
|
|
expect(spanEl.nodeName.toLowerCase()).toBe('span');
|
2017-01-20 16:10:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should set fixed attributes', () => {
|
|
|
|
const rootNodes = createAndGetRootNodes(compViewDef([
|
2017-09-22 17:29:16 -04:00
|
|
|
elementDef(0, NodeFlags.None, null, null, 0, 'div', [['title', 'a']]),
|
2017-01-20 16:10:57 -05:00
|
|
|
])).rootNodes;
|
|
|
|
expect(rootNodes.length).toBe(1);
|
2019-08-30 15:52:48 -04:00
|
|
|
expect(rootNodes[0].getAttribute('title')).toBe('a');
|
2017-01-20 16:10:57 -05:00
|
|
|
});
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
it('should add debug information to the renderer', () => {
|
2019-10-16 20:46:50 -04:00
|
|
|
const someContext = {};
|
2017-02-03 18:20:50 -05:00
|
|
|
const {view, rootNodes} = createAndGetRootNodes(
|
2017-09-22 17:29:16 -04:00
|
|
|
compViewDef([elementDef(0, NodeFlags.None, null, null, 0, 'div')]), someContext);
|
2020-04-13 19:40:21 -04:00
|
|
|
expect(getDebugNode(rootNodes[0])!.nativeNode).toBe(asElementData(view, 0).renderElement);
|
2017-02-03 18:20:50 -05:00
|
|
|
});
|
2017-01-20 16:10:57 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('change properties', () => {
|
2017-02-03 18:20:50 -05:00
|
|
|
ARG_TYPE_VALUES.forEach((inlineDynamic) => {
|
2017-02-27 12:14:18 -05:00
|
|
|
it(`should update via strategy ${inlineDynamic}`, () => {
|
2017-01-20 16:10:57 -05:00
|
|
|
const {view, rootNodes} = createAndGetRootNodes(compViewDef(
|
|
|
|
[
|
|
|
|
elementDef(
|
2017-09-22 17:29:16 -04:00
|
|
|
0, NodeFlags.None, null, null, 0, 'input', null,
|
2017-01-20 16:10:57 -05:00
|
|
|
[
|
2017-03-17 12:23:28 -04:00
|
|
|
[BindingFlags.TypeProperty, 'title', SecurityContext.NONE],
|
2017-09-22 17:29:16 -04:00
|
|
|
[BindingFlags.TypeProperty, 'value', SecurityContext.NONE],
|
2017-01-20 16:10:57 -05:00
|
|
|
]),
|
|
|
|
],
|
2017-09-22 17:29:16 -04:00
|
|
|
null, (check, view) => {
|
2017-02-03 18:20:50 -05:00
|
|
|
checkNodeInlineOrDynamic(check, view, 0, inlineDynamic, ['v1', 'v2']);
|
2017-01-26 20:07:37 -05:00
|
|
|
}));
|
2017-01-20 16:10:57 -05:00
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-20 16:10:57 -05:00
|
|
|
|
|
|
|
const el = rootNodes[0];
|
|
|
|
expect(getDOM().getProperty(el, 'title')).toBe('v1');
|
|
|
|
expect(getDOM().getProperty(el, 'value')).toBe('v2');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('change attributes', () => {
|
2017-02-03 18:20:50 -05:00
|
|
|
ARG_TYPE_VALUES.forEach((inlineDynamic) => {
|
2017-02-27 12:14:18 -05:00
|
|
|
it(`should update via strategy ${inlineDynamic}`, () => {
|
2017-01-20 16:10:57 -05:00
|
|
|
const {view, rootNodes} = createAndGetRootNodes(compViewDef(
|
|
|
|
[
|
|
|
|
elementDef(
|
2017-09-22 17:29:16 -04:00
|
|
|
0, NodeFlags.None, null, null, 0, 'div', null,
|
2017-01-20 16:10:57 -05:00
|
|
|
[
|
2017-03-17 12:23:28 -04:00
|
|
|
[BindingFlags.TypeElementAttribute, 'a1', SecurityContext.NONE],
|
2017-09-22 17:29:16 -04:00
|
|
|
[BindingFlags.TypeElementAttribute, 'a2', SecurityContext.NONE],
|
2017-01-20 16:10:57 -05:00
|
|
|
]),
|
|
|
|
],
|
2017-09-22 17:29:16 -04:00
|
|
|
null, (check, view) => {
|
2017-02-03 18:20:50 -05:00
|
|
|
checkNodeInlineOrDynamic(check, view, 0, inlineDynamic, ['v1', 'v2']);
|
2017-01-26 20:07:37 -05:00
|
|
|
}));
|
2017-01-20 16:10:57 -05:00
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-20 16:10:57 -05:00
|
|
|
|
|
|
|
const el = rootNodes[0];
|
2019-08-30 15:52:48 -04:00
|
|
|
expect(el.getAttribute('a1')).toBe('v1');
|
|
|
|
expect(el.getAttribute('a2')).toBe('v2');
|
2017-01-20 16:10:57 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('change classes', () => {
|
2017-02-03 18:20:50 -05:00
|
|
|
ARG_TYPE_VALUES.forEach((inlineDynamic) => {
|
2017-02-27 12:14:18 -05:00
|
|
|
it(`should update via strategy ${inlineDynamic}`, () => {
|
2017-01-20 16:10:57 -05:00
|
|
|
const {view, rootNodes} = createAndGetRootNodes(compViewDef(
|
|
|
|
[
|
|
|
|
elementDef(
|
2017-09-22 17:29:16 -04:00
|
|
|
0, NodeFlags.None, null, null, 0, 'div', null,
|
2017-03-17 12:23:28 -04:00
|
|
|
[
|
2017-09-22 17:29:16 -04:00
|
|
|
[BindingFlags.TypeElementClass, 'c1', null],
|
|
|
|
[BindingFlags.TypeElementClass, 'c2', null],
|
2017-03-17 12:23:28 -04:00
|
|
|
]),
|
2017-01-20 16:10:57 -05:00
|
|
|
],
|
2017-02-03 18:20:50 -05:00
|
|
|
(check, view) => {
|
|
|
|
checkNodeInlineOrDynamic(check, view, 0, inlineDynamic, [true, true]);
|
2017-01-26 20:07:37 -05:00
|
|
|
}));
|
2017-01-20 16:10:57 -05:00
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-20 16:10:57 -05:00
|
|
|
|
|
|
|
const el = rootNodes[0];
|
2019-08-23 15:32:00 -04:00
|
|
|
expect(el.classList.contains('c1')).toBeTruthy();
|
|
|
|
expect(el.classList.contains('c2')).toBeTruthy();
|
2017-01-20 16:10:57 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('change styles', () => {
|
2017-02-03 18:20:50 -05:00
|
|
|
ARG_TYPE_VALUES.forEach((inlineDynamic) => {
|
2017-02-27 12:14:18 -05:00
|
|
|
it(`should update via strategy ${inlineDynamic}`, () => {
|
2017-01-20 16:10:57 -05:00
|
|
|
const {view, rootNodes} = createAndGetRootNodes(compViewDef(
|
|
|
|
[
|
|
|
|
elementDef(
|
2017-09-22 17:29:16 -04:00
|
|
|
0, NodeFlags.None, null, null, 0, 'div', null,
|
2017-01-20 16:10:57 -05:00
|
|
|
[
|
2017-03-17 12:23:28 -04:00
|
|
|
[BindingFlags.TypeElementStyle, 'width', 'px'],
|
2017-09-22 17:29:16 -04:00
|
|
|
[BindingFlags.TypeElementStyle, 'color', null],
|
2017-01-20 16:10:57 -05:00
|
|
|
]),
|
|
|
|
],
|
2017-09-22 17:29:16 -04:00
|
|
|
null, (check, view) => {
|
2017-02-03 18:20:50 -05:00
|
|
|
checkNodeInlineOrDynamic(check, view, 0, inlineDynamic, [10, 'red']);
|
2017-01-26 20:07:37 -05:00
|
|
|
}));
|
2017-01-20 16:10:57 -05:00
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.checkAndUpdateView(view);
|
2017-01-20 16:10:57 -05:00
|
|
|
|
|
|
|
const el = rootNodes[0];
|
2019-08-30 13:16:20 -04:00
|
|
|
expect(el.style['width']).toBe('10px');
|
|
|
|
expect(el.style['color']).toBe('red');
|
2017-01-20 16:10:57 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-01-19 13:25:03 -05:00
|
|
|
|
2017-01-31 17:52:01 -05:00
|
|
|
if (isBrowser()) {
|
2017-01-19 13:25:03 -05:00
|
|
|
describe('listen to DOM events', () => {
|
|
|
|
function createAndAttachAndGetRootNodes(viewDef: ViewDefinition):
|
|
|
|
{rootNodes: any[], view: ViewData} {
|
|
|
|
const result = createAndGetRootNodes(viewDef);
|
|
|
|
// Note: We need to append the node to the document.body, otherwise `click` events
|
|
|
|
// won't work in IE.
|
|
|
|
result.rootNodes.forEach((node) => {
|
|
|
|
document.body.appendChild(node);
|
2017-05-16 16:29:38 -04:00
|
|
|
recordNodeToRemove(node);
|
2017-01-19 13:25:03 -05:00
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-13 05:02:59 -05:00
|
|
|
it('should listen to DOM events', () => {
|
|
|
|
const handleEventSpy = jasmine.createSpy('handleEvent');
|
|
|
|
const removeListenerSpy =
|
|
|
|
spyOn(HTMLElement.prototype, removeEventListener).and.callThrough();
|
|
|
|
const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef(
|
2020-04-13 19:40:21 -04:00
|
|
|
0, NodeFlags.None, null, null, 0, 'button', null, null, [[null!, 'click']],
|
2018-12-13 05:02:59 -05:00
|
|
|
handleEventSpy)]));
|
2017-01-19 13:25:03 -05:00
|
|
|
|
2018-12-13 05:02:59 -05:00
|
|
|
rootNodes[0].click();
|
2017-01-19 13:25:03 -05:00
|
|
|
|
2018-12-13 05:02:59 -05:00
|
|
|
expect(handleEventSpy).toHaveBeenCalled();
|
|
|
|
let handleEventArgs = handleEventSpy.calls.mostRecent().args;
|
|
|
|
expect(handleEventArgs[0]).toBe(view);
|
|
|
|
expect(handleEventArgs[1]).toBe('click');
|
|
|
|
expect(handleEventArgs[2]).toBeTruthy();
|
2017-01-19 13:25:03 -05:00
|
|
|
|
2018-12-13 05:02:59 -05:00
|
|
|
Services.destroyView(view);
|
2017-01-19 13:25:03 -05:00
|
|
|
|
2018-12-13 05:02:59 -05:00
|
|
|
expect(removeListenerSpy).toHaveBeenCalled();
|
|
|
|
});
|
2017-01-19 13:25:03 -05:00
|
|
|
|
|
|
|
it('should listen to window events', () => {
|
|
|
|
const handleEventSpy = jasmine.createSpy('handleEvent');
|
2017-07-13 18:36:11 -04:00
|
|
|
const addListenerSpy = spyOn(window, addEventListener);
|
|
|
|
const removeListenerSpy = spyOn(window, removeEventListener);
|
2017-02-20 15:15:03 -05:00
|
|
|
const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef(
|
2017-09-22 17:29:16 -04:00
|
|
|
0, NodeFlags.None, null, null, 0, 'button', null, null, [['window', 'windowClick']],
|
|
|
|
handleEventSpy)]));
|
2017-01-19 13:25:03 -05:00
|
|
|
|
|
|
|
expect(addListenerSpy).toHaveBeenCalled();
|
|
|
|
expect(addListenerSpy.calls.mostRecent().args[0]).toBe('windowClick');
|
2017-08-31 02:52:51 -04:00
|
|
|
callMostRecentEventListenerHandler(addListenerSpy, {name: 'windowClick'});
|
2017-01-19 13:25:03 -05:00
|
|
|
|
|
|
|
expect(handleEventSpy).toHaveBeenCalled();
|
|
|
|
const handleEventArgs = handleEventSpy.calls.mostRecent().args;
|
|
|
|
expect(handleEventArgs[0]).toBe(view);
|
2017-02-20 15:15:03 -05:00
|
|
|
expect(handleEventArgs[1]).toBe('window:windowClick');
|
|
|
|
expect(handleEventArgs[2]).toBeTruthy();
|
2017-01-19 13:25:03 -05:00
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.destroyView(view);
|
2017-01-19 13:25:03 -05:00
|
|
|
|
|
|
|
expect(removeListenerSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should listen to document events', () => {
|
|
|
|
const handleEventSpy = jasmine.createSpy('handleEvent');
|
2017-07-13 18:36:11 -04:00
|
|
|
const addListenerSpy = spyOn(document, addEventListener);
|
|
|
|
const removeListenerSpy = spyOn(document, removeEventListener);
|
2017-02-20 15:15:03 -05:00
|
|
|
const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef(
|
2017-09-22 17:29:16 -04:00
|
|
|
0, NodeFlags.None, null, null, 0, 'button', null, null,
|
2017-03-29 12:34:45 -04:00
|
|
|
[['document', 'documentClick']], handleEventSpy)]));
|
2017-01-19 13:25:03 -05:00
|
|
|
|
|
|
|
expect(addListenerSpy).toHaveBeenCalled();
|
|
|
|
expect(addListenerSpy.calls.mostRecent().args[0]).toBe('documentClick');
|
2017-08-31 02:52:51 -04:00
|
|
|
callMostRecentEventListenerHandler(addListenerSpy, {name: 'windowClick'});
|
2017-01-19 13:25:03 -05:00
|
|
|
|
|
|
|
expect(handleEventSpy).toHaveBeenCalled();
|
|
|
|
const handleEventArgs = handleEventSpy.calls.mostRecent().args;
|
|
|
|
expect(handleEventArgs[0]).toBe(view);
|
2017-02-20 15:15:03 -05:00
|
|
|
expect(handleEventArgs[1]).toBe('document:documentClick');
|
|
|
|
expect(handleEventArgs[2]).toBeTruthy();
|
2017-01-19 13:25:03 -05:00
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
Services.destroyView(view);
|
2017-01-19 13:25:03 -05:00
|
|
|
|
|
|
|
expect(removeListenerSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2018-12-13 05:02:59 -05:00
|
|
|
it('should preventDefault only if the handler returns false', () => {
|
|
|
|
let eventHandlerResult: any;
|
2020-04-13 19:40:21 -04:00
|
|
|
let preventDefaultSpy: jasmine.Spy = undefined!;
|
2018-12-13 05:02:59 -05:00
|
|
|
|
|
|
|
const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef(
|
2020-04-13 19:40:21 -04:00
|
|
|
0, NodeFlags.None, null, null, 0, 'button', null, null, [[null!, 'click']],
|
2018-12-13 05:02:59 -05:00
|
|
|
(view, eventName, event) => {
|
|
|
|
preventDefaultSpy = spyOn(event, 'preventDefault').and.callThrough();
|
|
|
|
return eventHandlerResult;
|
|
|
|
})]));
|
|
|
|
|
|
|
|
eventHandlerResult = undefined;
|
|
|
|
rootNodes[0].click();
|
|
|
|
expect(preventDefaultSpy).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
eventHandlerResult = true;
|
|
|
|
rootNodes[0].click();
|
|
|
|
expect(preventDefaultSpy).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
eventHandlerResult = 'someString';
|
|
|
|
rootNodes[0].click();
|
|
|
|
expect(preventDefaultSpy).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
eventHandlerResult = false;
|
|
|
|
rootNodes[0].click();
|
|
|
|
expect(preventDefaultSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report debug info on event errors', () => {
|
2019-08-28 19:22:36 -04:00
|
|
|
const handleErrorSpy = spyOn(TestBed.inject(ErrorHandler), 'handleError');
|
2018-12-13 05:02:59 -05:00
|
|
|
const addListenerSpy = spyOn(HTMLElement.prototype, addEventListener).and.callThrough();
|
|
|
|
const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef(
|
2020-04-13 19:40:21 -04:00
|
|
|
0, NodeFlags.None, null, null, 0, 'button', null, null, [[null!, 'click']], () => {
|
|
|
|
throw new Error('Test');
|
|
|
|
})]));
|
2018-12-13 05:02:59 -05:00
|
|
|
|
|
|
|
callMostRecentEventListenerHandler(addListenerSpy, 'SomeEvent');
|
|
|
|
const err = handleErrorSpy.calls.mostRecent().args[0];
|
|
|
|
expect(err).toBeTruthy();
|
|
|
|
expect(err.message).toBe('Test');
|
|
|
|
const debugCtx = getDebugContext(err);
|
|
|
|
expect(debugCtx.view).toBe(view);
|
|
|
|
expect(debugCtx.nodeIndex).toBe(0);
|
|
|
|
});
|
2017-01-19 13:25:03 -05:00
|
|
|
});
|
|
|
|
}
|
2017-01-20 16:10:57 -05:00
|
|
|
});
|
|
|
|
}
|