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
|
|
|
|
*/
|
|
|
|
|
2019-08-22 22:16:25 -04:00
|
|
|
import {ɵgetDOM as getDOM} from '@angular/common';
|
2017-09-22 17:29:16 -04:00
|
|
|
import {Injector, NgModuleRef} from '@angular/core';
|
2020-04-13 19:40:21 -04:00
|
|
|
import {ArgumentType, initServicesIfNeeded, NodeCheckFn, NodeDef, rootRenderNodes, Services, ViewData, viewDef, ViewDefinition, ViewDefinitionFactory, ViewFlags, ViewUpdateFn} from '@angular/core/src/view/index';
|
2017-01-20 16:10:57 -05:00
|
|
|
import {TestBed} from '@angular/core/testing';
|
|
|
|
|
|
|
|
export function isBrowser() {
|
|
|
|
return getDOM().supportsDOMEvents();
|
|
|
|
}
|
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
export const ARG_TYPE_VALUES = [ArgumentType.Inline, ArgumentType.Dynamic];
|
2017-01-20 12:21:09 -05:00
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
export function checkNodeInlineOrDynamic(
|
|
|
|
check: NodeCheckFn, view: ViewData, nodeIndex: number, argType: ArgumentType,
|
|
|
|
values: any[]): any {
|
|
|
|
switch (argType) {
|
|
|
|
case ArgumentType.Inline:
|
|
|
|
return (<any>check)(view, nodeIndex, argType, ...values);
|
|
|
|
case ArgumentType.Dynamic:
|
|
|
|
return check(view, nodeIndex, argType, values);
|
2017-01-20 12:21:09 -05:00
|
|
|
}
|
|
|
|
}
|
2017-02-01 14:32:27 -05:00
|
|
|
|
2017-02-03 18:20:50 -05:00
|
|
|
export function createRootView(
|
|
|
|
def: ViewDefinition, context?: any, projectableNodes?: any[][],
|
|
|
|
rootSelectorOrNode?: any): ViewData {
|
|
|
|
initServicesIfNeeded();
|
|
|
|
return Services.createRootView(
|
2019-08-28 19:22:36 -04:00
|
|
|
TestBed.inject(Injector), projectableNodes || [], rootSelectorOrNode, def,
|
|
|
|
TestBed.inject(NgModuleRef), context);
|
2017-02-01 14:32:27 -05:00
|
|
|
}
|
|
|
|
|
2017-05-15 16:12:10 -04:00
|
|
|
export function createEmbeddedView(parent: ViewData, anchorDef: NodeDef, context?: any): ViewData {
|
2020-04-13 19:40:21 -04:00
|
|
|
return Services.createEmbeddedView(parent, anchorDef, anchorDef.element!.template !, context);
|
2017-05-15 16:12:10 -04:00
|
|
|
}
|
|
|
|
|
2017-09-22 17:29:16 -04:00
|
|
|
export function compViewDef(
|
2020-04-13 19:40:21 -04:00
|
|
|
nodes: NodeDef[], updateDirectives?: null|ViewUpdateFn, updateRenderer?: null|ViewUpdateFn,
|
2017-09-22 17:29:16 -04:00
|
|
|
viewFlags: ViewFlags = ViewFlags.None): ViewDefinition {
|
|
|
|
const def = viewDef(viewFlags, nodes, updateDirectives, updateRenderer);
|
|
|
|
|
|
|
|
def.nodes.forEach((node, index) => {
|
|
|
|
if (node.nodeIndex !== index) {
|
|
|
|
throw new Error('nodeIndex should be the same as the index of the node');
|
|
|
|
}
|
|
|
|
|
|
|
|
// This check should be removed when we start reordering nodes at runtime
|
|
|
|
if (node.checkIndex > -1 && node.checkIndex !== node.nodeIndex) {
|
2020-04-13 19:40:21 -04:00
|
|
|
throw new Error(`nodeIndex and checkIndex should be the same, got ${node.nodeIndex} !== ${
|
|
|
|
node.checkIndex}`);
|
2017-09-22 17:29:16 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function compViewDefFactory(
|
2020-04-13 19:40:21 -04:00
|
|
|
nodes: NodeDef[], updateDirectives?: null|ViewUpdateFn, updateRenderer?: null|ViewUpdateFn,
|
2017-09-22 17:29:16 -04:00
|
|
|
viewFlags: ViewFlags = ViewFlags.None): ViewDefinitionFactory {
|
|
|
|
return () => compViewDef(nodes, updateDirectives, updateRenderer, viewFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createAndGetRootNodes(
|
|
|
|
viewDef: ViewDefinition, ctx?: any): {rootNodes: any[], view: ViewData} {
|
|
|
|
const view = createRootView(viewDef, ctx);
|
|
|
|
const rootNodes = rootRenderNodes(view);
|
|
|
|
return {rootNodes, view};
|
|
|
|
}
|
|
|
|
|
2017-05-16 16:29:38 -04:00
|
|
|
let removeNodes: Node[];
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
removeNodes = [];
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
removeNodes.forEach((node) => getDOM().remove(node));
|
|
|
|
});
|
2017-05-16 16:29:38 -04:00
|
|
|
|
|
|
|
export function recordNodeToRemove(node: Node) {
|
|
|
|
removeNodes.push(node);
|
2017-08-31 02:52:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function callMostRecentEventListenerHandler(spy: any, params: any) {
|
|
|
|
const mostRecent = spy.calls.mostRecent();
|
|
|
|
if (!mostRecent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const obj = mostRecent.object;
|
|
|
|
const args = mostRecent.args;
|
|
|
|
|
|
|
|
const eventName = args[0];
|
|
|
|
const handler = args[1];
|
|
|
|
|
|
|
|
handler && handler.apply(obj, [{type: eventName}]);
|
2019-08-28 19:22:36 -04:00
|
|
|
}
|