| 
									
										
										
										
											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'; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | import {ErrorHandler, getDebugNode, SecurityContext} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2017-01-27 13:19:00 -08:00
										 |  |  | import {getDebugContext} from '@angular/core/src/errors'; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | import {asElementData, BindingFlags, elementDef, NodeFlags, Services, ViewData, ViewDefinition} from '@angular/core/src/view/index'; | 
					
						
							| 
									
										
										
										
											2017-04-28 11:50:45 -07:00
										 |  |  | import {TestBed} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  | import {ARG_TYPE_VALUES, callMostRecentEventListenerHandler, checkNodeInlineOrDynamic, compViewDef, createAndGetRootNodes, isBrowser, recordNodeToRemove} from './helper'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-31 15:52:51 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-13 15:36:11 -07: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-10 00:48:07 +08:00
										 |  |  | const addEventListener = 'addEventListener'; | 
					
						
							|  |  |  | const removeEventListener = 'removeEventListener'; | 
					
						
							| 
									
										
										
										
											2017-07-13 15:36:11 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-15 16:28:41 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |   describe(`View Elements`, () => { | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |     describe('create', () => { | 
					
						
							|  |  |  |       it('should create elements without parents', () => { | 
					
						
							| 
									
										
										
										
											2017-01-23 16:59:20 -08:00
										 |  |  |         const rootNodes = createAndGetRootNodes(compViewDef([ | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |                             elementDef(0, NodeFlags.None, null, null, 0, 'span') | 
					
						
							| 
									
										
										
										
											2017-01-23 16:59:20 -08:00
										 |  |  |                           ])).rootNodes; | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |         expect(rootNodes.length).toBe(1); | 
					
						
							| 
									
										
										
										
											2019-08-24 08:18:00 -07:00
										 |  |  |         expect(rootNodes[0].nodeName.toLowerCase()).toBe('span'); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should create views with multiple root elements', () => { | 
					
						
							| 
									
										
										
										
											2017-01-23 16:59:20 -08:00
										 |  |  |         const rootNodes = createAndGetRootNodes(compViewDef([ | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |                             elementDef(0, NodeFlags.None, null, null, 0, 'span'), | 
					
						
							|  |  |  |                             elementDef(1, NodeFlags.None, null, null, 0, 'span'), | 
					
						
							| 
									
										
										
										
											2017-01-23 16:59:20 -08:00
										 |  |  |                           ])).rootNodes; | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |         expect(rootNodes.length).toBe(2); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should create elements with parents', () => { | 
					
						
							|  |  |  |         const rootNodes = createAndGetRootNodes(compViewDef([ | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |                             elementDef(0, NodeFlags.None, null, null, 1, 'div'), | 
					
						
							|  |  |  |                             elementDef(1, NodeFlags.None, null, null, 0, 'span'), | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |                           ])).rootNodes; | 
					
						
							|  |  |  |         expect(rootNodes.length).toBe(1); | 
					
						
							| 
									
										
										
										
											2019-08-24 08:01:24 -07:00
										 |  |  |         const spanEl = rootNodes[0].childNodes[0]; | 
					
						
							| 
									
										
										
										
											2019-08-24 08:18:00 -07:00
										 |  |  |         expect(spanEl.nodeName.toLowerCase()).toBe('span'); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should set fixed attributes', () => { | 
					
						
							|  |  |  |         const rootNodes = createAndGetRootNodes(compViewDef([ | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |                             elementDef(0, NodeFlags.None, null, null, 0, 'div', [['title', 'a']]), | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |                           ])).rootNodes; | 
					
						
							|  |  |  |         expect(rootNodes.length).toBe(1); | 
					
						
							| 
									
										
										
										
											2019-08-30 12:52:48 -07:00
										 |  |  |         expect(rootNodes[0].getAttribute('title')).toBe('a'); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |       it('should add debug information to the renderer', () => { | 
					
						
							| 
									
										
										
										
											2019-10-17 02:46:50 +02:00
										 |  |  |         const someContext = {}; | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |         const {view, rootNodes} = createAndGetRootNodes( | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |             compViewDef([elementDef(0, NodeFlags.None, null, null, 0, 'div')]), someContext); | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |         expect(getDebugNode(rootNodes[0])!.nativeNode).toBe(asElementData(view, 0).renderElement); | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('change properties', () => { | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |       ARG_TYPE_VALUES.forEach((inlineDynamic) => { | 
					
						
							| 
									
										
										
										
											2017-02-27 09:14:18 -08:00
										 |  |  |         it(`should update via strategy ${inlineDynamic}`, () => { | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |           const {view, rootNodes} = createAndGetRootNodes(compViewDef( | 
					
						
							|  |  |  |               [ | 
					
						
							|  |  |  |                 elementDef( | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |                     0, NodeFlags.None, null, null, 0, 'input', null, | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |                     [ | 
					
						
							| 
									
										
										
										
											2017-03-17 09:23:28 -07:00
										 |  |  |                       [BindingFlags.TypeProperty, 'title', SecurityContext.NONE], | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |                       [BindingFlags.TypeProperty, 'value', SecurityContext.NONE], | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |                     ]), | 
					
						
							|  |  |  |               ], | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |               null, (check, view) => { | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |                 checkNodeInlineOrDynamic(check, view, 0, inlineDynamic, ['v1', 'v2']); | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  |               })); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |           Services.checkAndUpdateView(view); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           const el = rootNodes[0]; | 
					
						
							| 
									
										
										
										
											2021-03-10 08:20:40 +01:00
										 |  |  |           expect(el.title).toBe('v1'); | 
					
						
							|  |  |  |           expect(el.value).toBe('v2'); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('change attributes', () => { | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |       ARG_TYPE_VALUES.forEach((inlineDynamic) => { | 
					
						
							| 
									
										
										
										
											2017-02-27 09:14:18 -08:00
										 |  |  |         it(`should update via strategy ${inlineDynamic}`, () => { | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |           const {view, rootNodes} = createAndGetRootNodes(compViewDef( | 
					
						
							|  |  |  |               [ | 
					
						
							|  |  |  |                 elementDef( | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |                     0, NodeFlags.None, null, null, 0, 'div', null, | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |                     [ | 
					
						
							| 
									
										
										
										
											2017-03-17 09:23:28 -07:00
										 |  |  |                       [BindingFlags.TypeElementAttribute, 'a1', SecurityContext.NONE], | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |                       [BindingFlags.TypeElementAttribute, 'a2', SecurityContext.NONE], | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |                     ]), | 
					
						
							|  |  |  |               ], | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |               null, (check, view) => { | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |                 checkNodeInlineOrDynamic(check, view, 0, inlineDynamic, ['v1', 'v2']); | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  |               })); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |           Services.checkAndUpdateView(view); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           const el = rootNodes[0]; | 
					
						
							| 
									
										
										
										
											2019-08-30 12:52:48 -07:00
										 |  |  |           expect(el.getAttribute('a1')).toBe('v1'); | 
					
						
							|  |  |  |           expect(el.getAttribute('a2')).toBe('v2'); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('change classes', () => { | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |       ARG_TYPE_VALUES.forEach((inlineDynamic) => { | 
					
						
							| 
									
										
										
										
											2017-02-27 09:14:18 -08:00
										 |  |  |         it(`should update via strategy ${inlineDynamic}`, () => { | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |           const {view, rootNodes} = createAndGetRootNodes(compViewDef( | 
					
						
							|  |  |  |               [ | 
					
						
							|  |  |  |                 elementDef( | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |                     0, NodeFlags.None, null, null, 0, 'div', null, | 
					
						
							| 
									
										
										
										
											2017-03-17 09:23:28 -07:00
										 |  |  |                     [ | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |                       [BindingFlags.TypeElementClass, 'c1', null], | 
					
						
							|  |  |  |                       [BindingFlags.TypeElementClass, 'c2', null], | 
					
						
							| 
									
										
										
										
											2017-03-17 09:23:28 -07:00
										 |  |  |                     ]), | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |               ], | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |               (check, view) => { | 
					
						
							|  |  |  |                 checkNodeInlineOrDynamic(check, view, 0, inlineDynamic, [true, true]); | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  |               })); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |           Services.checkAndUpdateView(view); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           const el = rootNodes[0]; | 
					
						
							| 
									
										
										
										
											2019-08-23 12:32:00 -07:00
										 |  |  |           expect(el.classList.contains('c1')).toBeTruthy(); | 
					
						
							|  |  |  |           expect(el.classList.contains('c2')).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('change styles', () => { | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |       ARG_TYPE_VALUES.forEach((inlineDynamic) => { | 
					
						
							| 
									
										
										
										
											2017-02-27 09:14:18 -08:00
										 |  |  |         it(`should update via strategy ${inlineDynamic}`, () => { | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |           const {view, rootNodes} = createAndGetRootNodes(compViewDef( | 
					
						
							|  |  |  |               [ | 
					
						
							|  |  |  |                 elementDef( | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |                     0, NodeFlags.None, null, null, 0, 'div', null, | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |                     [ | 
					
						
							| 
									
										
										
										
											2017-03-17 09:23:28 -07:00
										 |  |  |                       [BindingFlags.TypeElementStyle, 'width', 'px'], | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |                       [BindingFlags.TypeElementStyle, 'color', null], | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |                     ]), | 
					
						
							|  |  |  |               ], | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |               null, (check, view) => { | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |                 checkNodeInlineOrDynamic(check, view, 0, inlineDynamic, [10, 'red']); | 
					
						
							| 
									
										
										
										
											2017-01-26 17:07:37 -08:00
										 |  |  |               })); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |           Services.checkAndUpdateView(view); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           const el = rootNodes[0]; | 
					
						
							| 
									
										
										
										
											2019-08-30 10:16:20 -07:00
										 |  |  |           expect(el.style['width']).toBe('10px'); | 
					
						
							|  |  |  |           expect(el.style['color']).toBe('red'); | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-01-19 10:25:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-31 14:52:01 -08:00
										 |  |  |     if (isBrowser()) { | 
					
						
							| 
									
										
										
										
											2017-01-19 10:25:03 -08: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 13:29:38 -07:00
										 |  |  |             recordNodeToRemove(node); | 
					
						
							| 
									
										
										
										
											2017-01-19 10:25:03 -08:00
										 |  |  |           }); | 
					
						
							|  |  |  |           return result; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-13 11:02:59 +01: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 16:40:21 -07:00
										 |  |  |               0, NodeFlags.None, null, null, 0, 'button', null, null, [[null!, 'click']], | 
					
						
							| 
									
										
										
										
											2018-12-13 11:02:59 +01:00
										 |  |  |               handleEventSpy)])); | 
					
						
							| 
									
										
										
										
											2017-01-19 10:25:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-13 11:02:59 +01:00
										 |  |  |           rootNodes[0].click(); | 
					
						
							| 
									
										
										
										
											2017-01-19 10:25:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-13 11:02:59 +01: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 10:25:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-13 11:02:59 +01:00
										 |  |  |           Services.destroyView(view); | 
					
						
							| 
									
										
										
										
											2017-01-19 10:25:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-13 11:02:59 +01:00
										 |  |  |           expect(removeListenerSpy).toHaveBeenCalled(); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-01-19 10:25:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         it('should listen to window events', () => { | 
					
						
							|  |  |  |           const handleEventSpy = jasmine.createSpy('handleEvent'); | 
					
						
							| 
									
										
										
										
											2017-07-13 15:36:11 -07:00
										 |  |  |           const addListenerSpy = spyOn(window, addEventListener); | 
					
						
							|  |  |  |           const removeListenerSpy = spyOn(window, removeEventListener); | 
					
						
							| 
									
										
										
										
											2017-02-20 12:15:03 -08:00
										 |  |  |           const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef( | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |               0, NodeFlags.None, null, null, 0, 'button', null, null, [['window', 'windowClick']], | 
					
						
							|  |  |  |               handleEventSpy)])); | 
					
						
							| 
									
										
										
										
											2017-01-19 10:25:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(addListenerSpy).toHaveBeenCalled(); | 
					
						
							|  |  |  |           expect(addListenerSpy.calls.mostRecent().args[0]).toBe('windowClick'); | 
					
						
							| 
									
										
										
										
											2017-08-31 15:52:51 +09:00
										 |  |  |           callMostRecentEventListenerHandler(addListenerSpy, {name: 'windowClick'}); | 
					
						
							| 
									
										
										
										
											2017-01-19 10:25:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(handleEventSpy).toHaveBeenCalled(); | 
					
						
							|  |  |  |           const handleEventArgs = handleEventSpy.calls.mostRecent().args; | 
					
						
							|  |  |  |           expect(handleEventArgs[0]).toBe(view); | 
					
						
							| 
									
										
										
										
											2017-02-20 12:15:03 -08:00
										 |  |  |           expect(handleEventArgs[1]).toBe('window:windowClick'); | 
					
						
							|  |  |  |           expect(handleEventArgs[2]).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2017-01-19 10:25:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |           Services.destroyView(view); | 
					
						
							| 
									
										
										
										
											2017-01-19 10:25:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(removeListenerSpy).toHaveBeenCalled(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should listen to document events', () => { | 
					
						
							|  |  |  |           const handleEventSpy = jasmine.createSpy('handleEvent'); | 
					
						
							| 
									
										
										
										
											2017-07-13 15:36:11 -07:00
										 |  |  |           const addListenerSpy = spyOn(document, addEventListener); | 
					
						
							|  |  |  |           const removeListenerSpy = spyOn(document, removeEventListener); | 
					
						
							| 
									
										
										
										
											2017-02-20 12:15:03 -08:00
										 |  |  |           const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef( | 
					
						
							| 
									
										
										
										
											2017-09-22 14:29:16 -07:00
										 |  |  |               0, NodeFlags.None, null, null, 0, 'button', null, null, | 
					
						
							| 
									
										
										
										
											2017-03-29 09:34:45 -07:00
										 |  |  |               [['document', 'documentClick']], handleEventSpy)])); | 
					
						
							| 
									
										
										
										
											2017-01-19 10:25:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(addListenerSpy).toHaveBeenCalled(); | 
					
						
							|  |  |  |           expect(addListenerSpy.calls.mostRecent().args[0]).toBe('documentClick'); | 
					
						
							| 
									
										
										
										
											2017-08-31 15:52:51 +09:00
										 |  |  |           callMostRecentEventListenerHandler(addListenerSpy, {name: 'windowClick'}); | 
					
						
							| 
									
										
										
										
											2017-01-19 10:25:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(handleEventSpy).toHaveBeenCalled(); | 
					
						
							|  |  |  |           const handleEventArgs = handleEventSpy.calls.mostRecent().args; | 
					
						
							|  |  |  |           expect(handleEventArgs[0]).toBe(view); | 
					
						
							| 
									
										
										
										
											2017-02-20 12:15:03 -08:00
										 |  |  |           expect(handleEventArgs[1]).toBe('document:documentClick'); | 
					
						
							|  |  |  |           expect(handleEventArgs[2]).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2017-01-19 10:25:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 15:20:50 -08:00
										 |  |  |           Services.destroyView(view); | 
					
						
							| 
									
										
										
										
											2017-01-19 10:25:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           expect(removeListenerSpy).toHaveBeenCalled(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-13 11:02:59 +01:00
										 |  |  |         it('should preventDefault only if the handler returns false', () => { | 
					
						
							|  |  |  |           let eventHandlerResult: any; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |           let preventDefaultSpy: jasmine.Spy = undefined!; | 
					
						
							| 
									
										
										
										
											2018-12-13 11:02:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |           const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef( | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |               0, NodeFlags.None, null, null, 0, 'button', null, null, [[null!, 'click']], | 
					
						
							| 
									
										
										
										
											2018-12-13 11:02:59 +01: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 16:22:36 -07:00
										 |  |  |           const handleErrorSpy = spyOn(TestBed.inject(ErrorHandler), 'handleError'); | 
					
						
							| 
									
										
										
										
											2018-12-13 11:02:59 +01:00
										 |  |  |           const addListenerSpy = spyOn(HTMLElement.prototype, addEventListener).and.callThrough(); | 
					
						
							|  |  |  |           const {view, rootNodes} = createAndAttachAndGetRootNodes(compViewDef([elementDef( | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |               0, NodeFlags.None, null, null, 0, 'button', null, null, [[null!, 'click']], () => { | 
					
						
							|  |  |  |                 throw new Error('Test'); | 
					
						
							|  |  |  |               })])); | 
					
						
							| 
									
										
										
										
											2018-12-13 11:02:59 +01: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 10:25:03 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-01-20 13:10:57 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | } |