|
| 1 | +import { |
| 2 | + AsyncTestCompleter, |
| 3 | + beforeEach, |
| 4 | + ddescribe, |
| 5 | + describe, |
| 6 | + expect, |
| 7 | + iit, |
| 8 | + inject, |
| 9 | + it, |
| 10 | + xit, |
| 11 | +} from '@angular/core/testing/testing_internal'; |
| 12 | +import { TestComponentBuilder } from '@angular/compiler/testing'; |
| 13 | + |
| 14 | +import { |
| 15 | + Component, |
| 16 | + ViewMetadata, |
| 17 | + Input, |
| 18 | + Directive, |
| 19 | + ViewChild, |
| 20 | + ViewChildren, |
| 21 | + QueryList, |
| 22 | + ElementRef |
| 23 | +} from '@angular/core'; |
| 24 | + |
| 25 | +export function main() { |
| 26 | + describe('ViewChild', () => { |
| 27 | + it('should support type selector', |
| 28 | + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { |
| 29 | + tcb.overrideView(ViewChildTypeSelectorComponent, |
| 30 | + new ViewMetadata( |
| 31 | + {template: `<simple [marker]="'1'"></simple><simple [marker]="'2'"></simple>`, directives: [Simple]})) |
| 32 | + .createAsync(ViewChildTypeSelectorComponent) |
| 33 | + .then((view) => { |
| 34 | + view.detectChanges(); |
| 35 | + expect(view.componentInstance.child).toBeDefined(); |
| 36 | + expect(view.componentInstance.child.marker).toBe("1"); |
| 37 | + async.done(); |
| 38 | + }); |
| 39 | + })); |
| 40 | + it('should support string selector', |
| 41 | + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { |
| 42 | + tcb.overrideView( |
| 43 | + ViewChildStringSelectorComponent, |
| 44 | + new ViewMetadata({template: `<simple #child></simple>`, directives: [Simple]})) |
| 45 | + .createAsync(ViewChildStringSelectorComponent) |
| 46 | + .then((view) => { |
| 47 | + view.detectChanges(); |
| 48 | + expect(view.componentInstance.child).toBeDefined(); |
| 49 | + async.done(); |
| 50 | + }); |
| 51 | + })); |
| 52 | + }); |
| 53 | + describe('ViewChildren', () => { |
| 54 | + it('should support type selector', |
| 55 | + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { |
| 56 | + tcb.overrideView(ViewChildrenTypeSelectorComponent, |
| 57 | + new ViewMetadata({template: `<simple></simple><simple></simple>`, directives: [Simple]})) |
| 58 | + .createAsync(ViewChildrenTypeSelectorComponent) |
| 59 | + .then((view) => { |
| 60 | + view.detectChanges(); |
| 61 | + expect(view.componentInstance.children).toBeDefined(); |
| 62 | + expect(view.componentInstance.children.length).toBe(2); |
| 63 | + async.done(); |
| 64 | + }); |
| 65 | + })); |
| 66 | + it('should support string selector', |
| 67 | + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { |
| 68 | + tcb.overrideView( |
| 69 | + ViewChildrenStringSelectorComponent, |
| 70 | + new ViewMetadata({template: `<simple #child1></simple><simple #child2></simple>`, directives: [Simple]})) |
| 71 | + .createAsync(ViewChildrenStringSelectorComponent) |
| 72 | + .then((view) => { |
| 73 | + view.detectChanges(); |
| 74 | + expect(view.componentInstance.children).toBeDefined(); |
| 75 | + expect(view.componentInstance.children.length).toBe(2); |
| 76 | + async.done(); |
| 77 | + }); |
| 78 | + })); |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +@Directive({selector: "simple"}) |
| 84 | +class Simple { |
| 85 | + @Input() marker: string; |
| 86 | +} |
| 87 | + |
| 88 | +@Component({selector: 'view-child-type-selector'}) |
| 89 | +class ViewChildTypeSelectorComponent { |
| 90 | + @ViewChild(Simple) child: Simple; |
| 91 | +} |
| 92 | + |
| 93 | +@Component({selector: 'view-child-string-selector'}) |
| 94 | +class ViewChildStringSelectorComponent { |
| 95 | + @ViewChild("child") child: ElementRef; |
| 96 | +} |
| 97 | + |
| 98 | +@Component({selector: 'view-children-type-selector'}) |
| 99 | +class ViewChildrenTypeSelectorComponent { |
| 100 | + @ViewChildren(Simple) children: QueryList<Simple>; |
| 101 | +} |
| 102 | + |
| 103 | +@Component({selector: 'view-child-string-selector'}) |
| 104 | +class ViewChildrenStringSelectorComponent { |
| 105 | + // Allow comma separated selector (with spaces). |
| 106 | + @ViewChildren("child1 , child2") children: QueryList<ElementRef>; |
| 107 | +} |
0 commit comments