| 
									
										
										
										
											2016-06-23 09:47:54 -07: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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-23 10:52:40 -07:00
										 |  |  | import {Component, Directive, ElementRef, Input, NO_ERRORS_SCHEMA, QueryList, ViewChild, ViewChildren} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-08-17 15:05:22 -07:00
										 |  |  | import {TestBed} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2016-05-27 02:18:31 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-15 16:28:41 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-05-27 02:18:31 +09:00
										 |  |  |   describe('ViewChild', () => { | 
					
						
							| 
									
										
										
										
											2016-08-17 15:05:22 -07:00
										 |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       TestBed.configureTestingModule({ | 
					
						
							| 
									
										
										
										
											2016-08-23 10:52:40 -07:00
										 |  |  |         declarations: [ViewChildTypeSelectorComponent, ViewChildStringSelectorComponent, Simple], | 
					
						
							|  |  |  |         schemas: [NO_ERRORS_SCHEMA], | 
					
						
							| 
									
										
										
										
											2016-08-17 15:05:22 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should support type selector', () => { | 
					
						
							|  |  |  |       TestBed.overrideComponent( | 
					
						
							|  |  |  |           ViewChildTypeSelectorComponent, | 
					
						
							|  |  |  |           {set: {template: `<simple [marker]="'1'"></simple><simple [marker]="'2'"></simple>`}}); | 
					
						
							|  |  |  |       const view = TestBed.createComponent(ViewChildTypeSelectorComponent); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       view.detectChanges(); | 
					
						
							|  |  |  |       expect(view.componentInstance.child).toBeDefined(); | 
					
						
							|  |  |  |       expect(view.componentInstance.child.marker).toBe('1'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should support string selector', () => { | 
					
						
							|  |  |  |       TestBed.overrideComponent( | 
					
						
							|  |  |  |           ViewChildStringSelectorComponent, {set: {template: `<simple #child></simple>`}}); | 
					
						
							|  |  |  |       const view = TestBed.createComponent(ViewChildStringSelectorComponent); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       view.detectChanges(); | 
					
						
							|  |  |  |       expect(view.componentInstance.child).toBeDefined(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-05-27 02:18:31 +09:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-08-17 15:05:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-27 02:18:31 +09:00
										 |  |  |   describe('ViewChildren', () => { | 
					
						
							| 
									
										
										
										
											2016-08-17 15:05:22 -07:00
										 |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       TestBed.configureTestingModule({ | 
					
						
							|  |  |  |         declarations: | 
					
						
							| 
									
										
										
										
											2016-08-23 10:52:40 -07:00
										 |  |  |             [ViewChildrenTypeSelectorComponent, ViewChildrenStringSelectorComponent, Simple], | 
					
						
							|  |  |  |         schemas: [NO_ERRORS_SCHEMA], | 
					
						
							| 
									
										
										
										
											2016-08-17 15:05:22 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should support type selector', () => { | 
					
						
							|  |  |  |       TestBed.overrideComponent( | 
					
						
							|  |  |  |           ViewChildrenTypeSelectorComponent, | 
					
						
							|  |  |  |           {set: {template: `<simple></simple><simple></simple>`}}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const view = TestBed.createComponent(ViewChildrenTypeSelectorComponent); | 
					
						
							|  |  |  |       view.detectChanges(); | 
					
						
							|  |  |  |       expect(view.componentInstance.children).toBeDefined(); | 
					
						
							|  |  |  |       expect(view.componentInstance.children.length).toBe(2); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should support string selector', () => { | 
					
						
							|  |  |  |       TestBed.overrideComponent( | 
					
						
							|  |  |  |           ViewChildrenStringSelectorComponent, | 
					
						
							|  |  |  |           {set: {template: `<simple #child1></simple><simple #child2></simple>`}}); | 
					
						
							| 
									
										
										
										
											2016-08-23 10:52:40 -07:00
										 |  |  |       const view = TestBed.configureTestingModule({schemas: [NO_ERRORS_SCHEMA]}) | 
					
						
							|  |  |  |                        .createComponent(ViewChildrenStringSelectorComponent); | 
					
						
							| 
									
										
										
										
											2016-08-17 15:05:22 -07:00
										 |  |  |       view.detectChanges(); | 
					
						
							|  |  |  |       expect(view.componentInstance.children).toBeDefined(); | 
					
						
							|  |  |  |       expect(view.componentInstance.children.length).toBe(2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-05-27 02:18:31 +09:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | @Directive({selector: 'simple'}) | 
					
						
							| 
									
										
										
										
											2016-05-27 02:18:31 +09:00
										 |  |  | class Simple { | 
					
						
							| 
									
										
										
										
											2018-06-18 16:38:33 -07:00
										 |  |  |   // TODO(issue/24571): remove '!'.
 | 
					
						
							|  |  |  |   @Input() marker !: string; | 
					
						
							| 
									
										
										
										
											2016-05-27 02:18:31 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 15:05:22 -07:00
										 |  |  | @Component({selector: 'view-child-type-selector', template: ''}) | 
					
						
							| 
									
										
										
										
											2016-05-27 02:18:31 +09:00
										 |  |  | class ViewChildTypeSelectorComponent { | 
					
						
							| 
									
										
										
										
											2018-06-18 16:38:33 -07:00
										 |  |  |   // TODO(issue/24571): remove '!'.
 | 
					
						
							| 
									
										
										
										
											2019-10-05 10:04:54 +02:00
										 |  |  |   @ViewChild(Simple) child !: Simple; | 
					
						
							| 
									
										
										
										
											2016-05-27 02:18:31 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 15:05:22 -07:00
										 |  |  | @Component({selector: 'view-child-string-selector', template: ''}) | 
					
						
							| 
									
										
										
										
											2016-05-27 02:18:31 +09:00
										 |  |  | class ViewChildStringSelectorComponent { | 
					
						
							| 
									
										
										
										
											2018-06-18 16:38:33 -07:00
										 |  |  |   // TODO(issue/24571): remove '!'.
 | 
					
						
							| 
									
										
										
										
											2019-10-05 10:04:54 +02:00
										 |  |  |   @ViewChild('child') child !: ElementRef; | 
					
						
							| 
									
										
										
										
											2016-05-27 02:18:31 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 15:05:22 -07:00
										 |  |  | @Component({selector: 'view-children-type-selector', template: ''}) | 
					
						
							| 
									
										
										
										
											2016-05-27 02:18:31 +09:00
										 |  |  | class ViewChildrenTypeSelectorComponent { | 
					
						
							| 
									
										
										
										
											2018-06-18 16:38:33 -07:00
										 |  |  |   // TODO(issue/24571): remove '!'.
 | 
					
						
							|  |  |  |   @ViewChildren(Simple) children !: QueryList<Simple>; | 
					
						
							| 
									
										
										
										
											2016-05-27 02:18:31 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 15:05:22 -07:00
										 |  |  | @Component({selector: 'view-child-string-selector', template: ''}) | 
					
						
							| 
									
										
										
										
											2016-05-27 02:18:31 +09:00
										 |  |  | class ViewChildrenStringSelectorComponent { | 
					
						
							|  |  |  |   // Allow comma separated selector (with spaces).
 | 
					
						
							| 
									
										
										
										
											2018-06-18 16:38:33 -07:00
										 |  |  |   // TODO(issue/24571): remove '!'.
 | 
					
						
							|  |  |  |   @ViewChildren('child1 , child2') children !: QueryList<ElementRef>; | 
					
						
							| 
									
										
										
										
											2016-05-27 02:18:31 +09:00
										 |  |  | } |