| 
									
										
										
										
											2017-01-26 17:07:37 -08: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-02-01 11:32:27 -08:00
										 |  |  | import {Injector, RenderComponentType, RootRenderer, Sanitizer, SecurityContext, ViewEncapsulation, getDebugNode} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  | import {DebugContext, NodeDef, NodeFlags, QueryValueType, RootData, Services, ViewData, ViewDefinition, ViewFlags, ViewHandleEventFn, ViewUpdateFn, anchorDef, asElementData, asProviderData, asTextData, directiveDef, elementDef, rootRenderNodes, textDef, viewDef} from '@angular/core/src/view/index'; | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  | import {inject} from '@angular/core/testing'; | 
					
						
							|  |  |  | import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  | import {createRootView, isBrowser} from './helper'; | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |   describe('View Services', () => { | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  |     function compViewDef( | 
					
						
							| 
									
										
										
										
											2017-02-09 14:59:57 -08:00
										 |  |  |         nodes: NodeDef[], updateDirectives?: ViewUpdateFn, updateRenderer?: ViewUpdateFn, | 
					
						
							| 
									
										
										
										
											2017-02-20 12:15:03 -08:00
										 |  |  |         viewFlags: ViewFlags = ViewFlags.None): ViewDefinition { | 
					
						
							|  |  |  |       return viewDef(viewFlags, nodes, updateDirectives, updateRenderer); | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function createAndGetRootNodes( | 
					
						
							|  |  |  |         viewDef: ViewDefinition, context: any = null): {rootNodes: any[], view: ViewData} { | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |       const view = createRootView(viewDef, context); | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  |       const rootNodes = rootRenderNodes(view); | 
					
						
							|  |  |  |       return {rootNodes, view}; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('DebugContext', () => { | 
					
						
							|  |  |  |       class AComp {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       class AService {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       function createViewWithData() { | 
					
						
							|  |  |  |         const {view} = createAndGetRootNodes(compViewDef([ | 
					
						
							| 
									
										
										
										
											2017-02-21 13:56:56 -08:00
										 |  |  |           elementDef( | 
					
						
							| 
									
										
										
										
											2017-03-29 09:34:45 -07:00
										 |  |  |               NodeFlags.None, null !, null !, 1, 'div', null !, null !, null !, null !, | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  |               () => compViewDef([ | 
					
						
							| 
									
										
										
										
											2017-03-29 09:34:45 -07:00
										 |  |  |                 elementDef(NodeFlags.None, [['ref', QueryValueType.ElementRef]], null !, 2, 'span'), | 
					
						
							|  |  |  |                 directiveDef(NodeFlags.None, null !, 0, AService, []), textDef(null !, ['a']) | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  |               ])), | 
					
						
							| 
									
										
										
										
											2017-03-29 09:34:45 -07:00
										 |  |  |           directiveDef(NodeFlags.Component, null !, 0, AComp, []), | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  |         ])); | 
					
						
							|  |  |  |         return view; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should provide data for elements', () => { | 
					
						
							|  |  |  |         const view = createViewWithData(); | 
					
						
							| 
									
										
										
										
											2017-02-21 13:56:56 -08:00
										 |  |  |         const compView = asElementData(view, 0).componentView; | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |         const debugCtx = Services.createDebugContext(compView, 0); | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(debugCtx.componentRenderElement).toBe(asElementData(view, 0).renderElement); | 
					
						
							|  |  |  |         expect(debugCtx.renderNode).toBe(asElementData(compView, 0).renderElement); | 
					
						
							|  |  |  |         expect(debugCtx.injector.get(AComp)).toBe(compView.component); | 
					
						
							|  |  |  |         expect(debugCtx.component).toBe(compView.component); | 
					
						
							|  |  |  |         expect(debugCtx.context).toBe(compView.context); | 
					
						
							|  |  |  |         expect(debugCtx.providerTokens).toEqual([AService]); | 
					
						
							|  |  |  |         expect(debugCtx.references['ref'].nativeElement) | 
					
						
							|  |  |  |             .toBe(asElementData(compView, 0).renderElement); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should provide data for text nodes', () => { | 
					
						
							|  |  |  |         const view = createViewWithData(); | 
					
						
							| 
									
										
										
										
											2017-02-21 13:56:56 -08:00
										 |  |  |         const compView = asElementData(view, 0).componentView; | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |         const debugCtx = Services.createDebugContext(compView, 2); | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(debugCtx.componentRenderElement).toBe(asElementData(view, 0).renderElement); | 
					
						
							|  |  |  |         expect(debugCtx.renderNode).toBe(asTextData(compView, 2).renderText); | 
					
						
							|  |  |  |         expect(debugCtx.injector.get(AComp)).toBe(compView.component); | 
					
						
							|  |  |  |         expect(debugCtx.component).toBe(compView.component); | 
					
						
							|  |  |  |         expect(debugCtx.context).toBe(compView.context); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should provide data for other nodes based on the nearest element parent', () => { | 
					
						
							|  |  |  |         const view = createViewWithData(); | 
					
						
							| 
									
										
										
										
											2017-02-21 13:56:56 -08:00
										 |  |  |         const compView = asElementData(view, 0).componentView; | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |         const debugCtx = Services.createDebugContext(compView, 1); | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(debugCtx.renderNode).toBe(asElementData(compView, 0).renderElement); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |