| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08: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 19:16:25 -07:00
										 |  |  | import {ɵgetDOM as getDOM} from '@angular/common'; | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  | import {Injector, NgModuleRef} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | import {ArgumentType, initServicesIfNeeded, NodeCheckFn, NodeDef, rootRenderNodes, Services, ViewData, viewDef, ViewDefinition, ViewDefinitionFactory, ViewFlags, ViewUpdateFn} from '@angular/core/src/view/index'; | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  | import {TestBed} from '@angular/core/testing'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function isBrowser() { | 
					
						
							| 
									
										
										
										
											2021-03-10 08:20:40 +01:00
										 |  |  |   return getDOM().supportsDOMEvents; | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  | export const ARG_TYPE_VALUES = [ArgumentType.Inline, ArgumentType.Dynamic]; | 
					
						
							| 
									
										
										
										
											2017-01-20 09:21:09 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08: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 09:21:09 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-02-01 11:32:27 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  | export function createRootView( | 
					
						
							|  |  |  |     def: ViewDefinition, context?: any, projectableNodes?: any[][], | 
					
						
							|  |  |  |     rootSelectorOrNode?: any): ViewData { | 
					
						
							|  |  |  |   initServicesIfNeeded(); | 
					
						
							|  |  |  |   return Services.createRootView( | 
					
						
							| 
									
										
										
										
											2019-08-28 16:22:36 -07:00
										 |  |  |       TestBed.inject(Injector), projectableNodes || [], rootSelectorOrNode, def, | 
					
						
							|  |  |  |       TestBed.inject(NgModuleRef), context); | 
					
						
							| 
									
										
										
										
											2017-02-01 11:32:27 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-15 13:12:10 -07:00
										 |  |  | export function createEmbeddedView(parent: ViewData, anchorDef: NodeDef, context?: any): ViewData { | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |   return Services.createEmbeddedView(parent, anchorDef, anchorDef.element!.template !, context); | 
					
						
							| 
									
										
										
										
											2017-05-15 13:12:10 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  | export function compViewDef( | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     nodes: NodeDef[], updateDirectives?: null|ViewUpdateFn, updateRenderer?: null|ViewUpdateFn, | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07: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 16:40:21 -07:00
										 |  |  |       throw new Error(`nodeIndex and checkIndex should be the same, got ${node.nodeIndex} !== ${ | 
					
						
							|  |  |  |           node.checkIndex}`);
 | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return def; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function compViewDefFactory( | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     nodes: NodeDef[], updateDirectives?: null|ViewUpdateFn, updateRenderer?: null|ViewUpdateFn, | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07: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 13:29:38 -07:00
										 |  |  | let removeNodes: Node[]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | beforeEach(() => { | 
					
						
							|  |  |  |   removeNodes = []; | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | afterEach(() => { | 
					
						
							|  |  |  |   removeNodes.forEach((node) => getDOM().remove(node)); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2017-05-16 13:29:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function recordNodeToRemove(node: Node) { | 
					
						
							|  |  |  |   removeNodes.push(node); | 
					
						
							| 
									
										
										
										
											2017-08-31 15:52:51 +09: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 16:22:36 -07:00
										 |  |  | } |