|
| 1 | +import { |
| 2 | + AsyncTestCompleter, |
| 3 | + TestComponentBuilder, |
| 4 | + beforeEach, |
| 5 | + ddescribe, |
| 6 | + describe, |
| 7 | + el, |
| 8 | + expect, |
| 9 | + iit, |
| 10 | + inject, |
| 11 | + it, |
| 12 | + xit, |
| 13 | +} from 'angular2/testing_internal'; |
| 14 | + |
| 15 | +import {Component, Directive, TemplateRef, ContentChildren, QueryList} from 'angular2/core'; |
| 16 | + |
| 17 | +import {NgTemplateOutlet} from 'angular2/src/common/directives/ng_template_outlet'; |
| 18 | + |
| 19 | +export function main() { |
| 20 | + describe('insert', () => { |
| 21 | + it('should do nothing if templateRef is null', |
| 22 | + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { |
| 23 | + var template = `<template [ngTemplateOutlet]="null"></template>`; |
| 24 | + tcb.overrideTemplate(TestComponent, template) |
| 25 | + .createAsync(TestComponent) |
| 26 | + .then((fixture) => { |
| 27 | + |
| 28 | + fixture.detectChanges(); |
| 29 | + expect(fixture.nativeElement).toHaveText(''); |
| 30 | + |
| 31 | + async.done(); |
| 32 | + }); |
| 33 | + })); |
| 34 | + |
| 35 | + it('should insert content specified by TemplateRef', |
| 36 | + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { |
| 37 | + var template = |
| 38 | + `<tpl-refs #refs="tplRefs"><template>foo</template></tpl-refs><template [ngTemplateOutlet]="currentTplRef"></template>`; |
| 39 | + tcb.overrideTemplate(TestComponent, template) |
| 40 | + .createAsync(TestComponent) |
| 41 | + .then((fixture) => { |
| 42 | + |
| 43 | + fixture.detectChanges(); |
| 44 | + expect(fixture.nativeElement).toHaveText(''); |
| 45 | + |
| 46 | + var refs = fixture.debugElement.children[0].getLocal('refs'); |
| 47 | + |
| 48 | + fixture.componentInstance.currentTplRef = refs.tplRefs.first; |
| 49 | + fixture.detectChanges(); |
| 50 | + expect(fixture.nativeElement).toHaveText('foo'); |
| 51 | + |
| 52 | + async.done(); |
| 53 | + }); |
| 54 | + })); |
| 55 | + |
| 56 | + it('should clear content if TemplateRef becomes null', |
| 57 | + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { |
| 58 | + var template = |
| 59 | + `<tpl-refs #refs="tplRefs"><template>foo</template></tpl-refs><template [ngTemplateOutlet]="currentTplRef"></template>`; |
| 60 | + tcb.overrideTemplate(TestComponent, template) |
| 61 | + .createAsync(TestComponent) |
| 62 | + .then((fixture) => { |
| 63 | + |
| 64 | + fixture.detectChanges(); |
| 65 | + var refs = fixture.debugElement.children[0].getLocal('refs'); |
| 66 | + |
| 67 | + fixture.componentInstance.currentTplRef = refs.tplRefs.first; |
| 68 | + fixture.detectChanges(); |
| 69 | + expect(fixture.nativeElement).toHaveText('foo'); |
| 70 | + |
| 71 | + fixture.componentInstance.currentTplRef = null; |
| 72 | + fixture.detectChanges(); |
| 73 | + expect(fixture.nativeElement).toHaveText(''); |
| 74 | + |
| 75 | + async.done(); |
| 76 | + }); |
| 77 | + })); |
| 78 | + |
| 79 | + it('should swap content if TemplateRef changes', |
| 80 | + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { |
| 81 | + var template = `<tpl-refs #refs="tplRefs"><template>foo</template><template>bar</template></tpl-refs><template [ngTemplateOutlet]="currentTplRef"></template>`; |
| 82 | + tcb.overrideTemplate(TestComponent, template) |
| 83 | + .createAsync(TestComponent) |
| 84 | + .then((fixture) => { |
| 85 | + |
| 86 | + fixture.detectChanges(); |
| 87 | + var refs = fixture.debugElement.children[0].getLocal('refs'); |
| 88 | + |
| 89 | + fixture.componentInstance.currentTplRef = refs.tplRefs.first; |
| 90 | + fixture.detectChanges(); |
| 91 | + expect(fixture.nativeElement).toHaveText('foo'); |
| 92 | + |
| 93 | + fixture.componentInstance.currentTplRef = refs.tplRefs.last; |
| 94 | + fixture.detectChanges(); |
| 95 | + expect(fixture.nativeElement).toHaveText('bar'); |
| 96 | + |
| 97 | + async.done(); |
| 98 | + }); |
| 99 | + })); |
| 100 | + |
| 101 | + }); |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +@Directive({selector: 'tpl-refs', exportAs: 'tplRefs'}) |
| 106 | +class CaptureTplRefs { |
| 107 | + @ContentChildren(TemplateRef) tplRefs: QueryList<TemplateRef>; |
| 108 | +} |
| 109 | + |
| 110 | +@Component({selector: 'test-cmp', directives: [NgTemplateOutlet, CaptureTplRefs], template: ''}) |
| 111 | +class TestComponent { |
| 112 | + currentTplRef: TemplateRef; |
| 113 | +} |
0 commit comments