2017-01-20 16:10:57 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2017-04-28 14:50:45 -04:00
|
|
|
import {ErrorHandler, Injector, RenderComponentType, RootRenderer, Sanitizer, SecurityContext, ViewEncapsulation, WrappedValue, getDebugNode} from '@angular/core';
|
2017-01-27 16:19:00 -05:00
|
|
|
import {getDebugContext} from '@angular/core/src/errors';
|
2017-03-17 12:23:28 -04:00
|
|
|
import {ArgumentType, BindingFlags, DebugContext, NodeDef, NodeFlags, OutputType, RootData, Services, ViewData, ViewDefinition, ViewFlags, ViewHandleEventFn, ViewUpdateFn, anchorDef, asElementData, elementDef, rootRenderNodes, textDef, viewDef} 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
|
|
|
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
|
|
|
|
2017-05-16 16:29:38 -04:00
|
|
|
import {ARG_TYPE_VALUES, checkNodeInlineOrDynamic, createRootView, isBrowser, recordNodeToRemove} from './helper';
|
2017-01-20 16:10:57 -05:00
|
|
|
|
|
|
|
export function main() {
|
2017-02-03 18:20:50 -05:00
|
|
|
describe(`View Elements`, () => {
|
2017-01-19 13:25:03 -05:00
|
|
|
function compViewDef(
|
2017-02-09 17:59:57 -05:00
|
|
|
nodes: NodeDef[], updateDirectives?: ViewUpdateFn, updateRenderer?: ViewUpdateFn,
|
2017-02-20 15:15:03 -05:00
|
|
|
viewFlags: ViewFlags = ViewFlags.None): ViewDefinition {
|
|
|
|
return viewDef(viewFlags, nodes, updateDirectives, updateRenderer);
|
2017-01-20 16:10:57 -05:00
|
|
|
}
|
|
|
|
|
2017-01-26 20:07:37 -05:00
|
|
|
function createAndGetRootNodes(
|
|
|
|
viewDef: ViewDefinition, context?: any): {rootNodes: any[], view: ViewData} {
|
2017-02-03 18:20:50 -05:00
|
|
|
const view = createRootView(viewDef, context);
|
2017-01-20 16:10:57 -05:00
|
|
|
const rootNodes = rootRenderNodes(view);
|
|
|
|
return {rootNodes, view};
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('create', () => {
|
|
|
|
it('should create elements without parents', () => {
|
2017-01-23 19:59:20 -05:00
|
|
|
const rootNodes = createAndGetRootNodes(compViewDef([
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(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);
|
|
|
|
expect(getDOM().nodeName(rootNodes[0]).toLowerCase()).toBe('span');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create views with multiple root elements', () => {
|
2017-01-23 19:59:20 -05:00
|
|
|
const rootNodes = createAndGetRootNodes(compViewDef([
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 0, 'span'),
|
|
|
|
elementDef(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-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 1, 'div'),
|
|
|
|
elementDef(NodeFlags.None, null !, null !, 0, 'span'),
|
2017-01-20 16:10:57 -05:00
|
|
|
])).rootNodes;
|
|
|
|
expect(rootNodes.length).toBe(1);
|
|
|
|
const spanEl = getDOM().childNodes(rootNodes[0])[0];
|
|
|
|
expect(getDOM().nodeName(spanEl).toLowerCase()).toBe('span');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set fixed attributes', () => {
|
|
|
|
const rootNodes = createAndGetRootNodes(compViewDef([
|
2017-03-29 12:34:45 -04:00
|
|
|
elementDef(NodeFlags.None, null !, null !, 0, 'div', [['title', 'a']]),
|
2017-01-20 16:10:57 -05:00
|
|
|
])).rootNodes;
|
|
|
|
expect(rootNodes.length).toBe(1);
|
|
|
|
expect(getDOM().getAttribute(rootNodes[0], 'title')).toBe('a');
|
|
|
|
});
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
it('should add debug information to the renderer', () => {
|
|
|
|
const someContext = new Object();
|
|
|
|
const {view, rootNodes} = createAndGetRootNodes(
|
2017-03-29 12:34:45 -04:00
|
|
|
compViewDef([elementDef(NodeFlags.None, null !, null !, 0, 'div')]), someContext);
|
|
|
|
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-03-29 12:34:45 -04:00
|
|
|
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],
|
|
|
|
[BindingFlags.TypeProperty, 'value', SecurityContext.NONE]
|
2017-01-20 16:10:57 -05:00
|
|
|
]),
|
|
|
|
],
|
2017-03-29 12:34:45 -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-03-29 12:34:45 -04:00
|
|
|
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],
|
|
|
|
[BindingFlags.TypeElementAttribute, 'a2', SecurityContext.NONE]
|
2017-01-20 16:10:57 -05:00
|
|
|
]),
|
|
|
|
],
|
2017-03-29 12:34:45 -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().getAttribute(el, 'a1')).toBe('v1');
|
|
|
|
expect(getDOM().getAttribute(el, 'a2')).toBe('v2');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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-03-29 12:34:45 -04:00
|
|
|
NodeFlags.None, null !, null !, 0, 'div', null !,
|
2017-03-17 12:23:28 -04:00
|
|
|
[
|
2017-03-29 12:34:45 -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];
|
|
|
|
expect(getDOM().hasClass(el, 'c1')).toBeTruthy();
|
|
|
|
expect(getDOM().hasClass(el, 'c2')).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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-03-29 12:34:45 -04:00
|
|
|
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-03-29 12:34:45 -04:00
|
|
|
[BindingFlags.TypeElementStyle, 'color', null !]
|
2017-01-20 16:10:57 -05:00
|
|
|
]),
|
|
|
|
],
|
2017-03-29 12:34:45 -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];
|
|
|
|
expect(getDOM().getStyle(el, 'width')).toBe('10px');
|
|
|
|
expect(getDOM().getStyle(el, 'color')).toBe('red');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should listen to DOM events', () => {
|
|
|
|
const handleEventSpy = jasmine.createSpy('handleEvent');
|
|
|
|
const removeListenerSpy =
|
|
|
|
spyOn(HTMLElement.prototype, 'removeEventListener').and.callThrough();
|
2017-02-20 15:15:03 -05:00
|
|
|
const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef(
|
2017-03-29 12:34:45 -04:00
|
|
|
NodeFlags.None, null !, null !, 0, 'button', null !, null !, [[null !, 'click']],
|
2017-02-21 16:56:56 -05:00
|
|
|
handleEventSpy)]));
|
2017-01-19 13:25:03 -05:00
|
|
|
|
|
|
|
rootNodes[0].click();
|
|
|
|
|
|
|
|
expect(handleEventSpy).toHaveBeenCalled();
|
|
|
|
let handleEventArgs = handleEventSpy.calls.mostRecent().args;
|
|
|
|
expect(handleEventArgs[0]).toBe(view);
|
2017-02-20 15:15:03 -05:00
|
|
|
expect(handleEventArgs[1]).toBe('click');
|
|
|
|
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 window events', () => {
|
|
|
|
const handleEventSpy = jasmine.createSpy('handleEvent');
|
|
|
|
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-03-29 12:34:45 -04:00
|
|
|
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');
|
|
|
|
addListenerSpy.calls.mostRecent().args[1]({name: 'windowClick'});
|
|
|
|
|
|
|
|
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');
|
|
|
|
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-03-29 12:34:45 -04:00
|
|
|
NodeFlags.None, null !, null !, 0, 'button', null !, null !,
|
|
|
|
[['document', 'documentClick']], handleEventSpy)]));
|
2017-01-19 13:25:03 -05:00
|
|
|
|
|
|
|
expect(addListenerSpy).toHaveBeenCalled();
|
|
|
|
expect(addListenerSpy.calls.mostRecent().args[0]).toBe('documentClick');
|
|
|
|
addListenerSpy.calls.mostRecent().args[1]({name: 'documentClick'});
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should preventDefault only if the handler returns false', () => {
|
|
|
|
let eventHandlerResult: any;
|
2017-03-29 12:34:45 -04:00
|
|
|
let preventDefaultSpy: jasmine.Spy = undefined !;
|
2017-01-19 13:25:03 -05:00
|
|
|
|
2017-02-20 15:15:03 -05:00
|
|
|
const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef(
|
2017-03-29 12:34:45 -04:00
|
|
|
NodeFlags.None, null !, null !, 0, 'button', null !, null !, [[null !, 'click']],
|
2017-02-20 15:15:03 -05:00
|
|
|
(view, eventName, event) => {
|
2017-01-19 13:25:03 -05:00
|
|
|
preventDefaultSpy = spyOn(event, 'preventDefault').and.callThrough();
|
|
|
|
return eventHandlerResult;
|
2017-02-20 15:15:03 -05:00
|
|
|
})]));
|
2017-01-19 13:25:03 -05:00
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2017-01-26 20:07:37 -05:00
|
|
|
it('should report debug info on event errors', () => {
|
2017-04-28 14:50:45 -04:00
|
|
|
const handleErrorSpy = spyOn(TestBed.get(ErrorHandler), 'handleError');
|
2017-01-26 20:07:37 -05:00
|
|
|
const addListenerSpy = spyOn(HTMLElement.prototype, 'addEventListener').and.callThrough();
|
2017-02-21 16:56:56 -05:00
|
|
|
const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef(
|
2017-03-29 12:34:45 -04:00
|
|
|
NodeFlags.None, null !, null !, 0, 'button', null !, null !, [[null !, 'click']],
|
2017-02-21 16:56:56 -05:00
|
|
|
() => { throw new Error('Test'); })]));
|
2017-01-26 20:07:37 -05:00
|
|
|
|
2017-04-28 14:50:45 -04:00
|
|
|
addListenerSpy.calls.mostRecent().args[1]('SomeEvent');
|
|
|
|
const err = handleErrorSpy.calls.mostRecent().args[0];
|
2017-01-26 20:07:37 -05:00
|
|
|
expect(err).toBeTruthy();
|
|
|
|
expect(err.message).toBe('Test');
|
2017-01-27 16:19:00 -05:00
|
|
|
const debugCtx = getDebugContext(err);
|
2017-01-26 20:07:37 -05:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|