|
| 1 | +import { |
| 2 | + inject, |
| 3 | + TestComponentBuilder, |
| 4 | + fakeAsync, |
| 5 | + flushMicrotasks, |
| 6 | + beforeEachProviders, |
| 7 | +} from 'angular2/testing'; |
| 8 | +import { |
| 9 | + it, |
| 10 | + describe, |
| 11 | + expect, |
| 12 | + beforeEach, |
| 13 | +} from '../../core/facade/testing'; |
| 14 | +import { |
| 15 | + Component, |
| 16 | + ViewChild, |
| 17 | + ElementRef, |
| 18 | + provide, |
| 19 | +} from 'angular2/core'; |
| 20 | +import {BrowserDomAdapter} from '../platform/browser/browser_adapter'; |
| 21 | +import {TemplatePortalDirective} from '../portal/portal-directives'; |
| 22 | +import {TemplatePortal, ComponentPortal} from '../portal/portal'; |
| 23 | +import {Overlay, OVERLAY_CONTAINER_TOKEN} from './overlay'; |
| 24 | +import {DOM} from '../platform/dom/dom_adapter'; |
| 25 | +import {OverlayRef} from './overlay-ref'; |
| 26 | + |
| 27 | + |
| 28 | +export function main() { |
| 29 | + describe('Overlay', () => { |
| 30 | + BrowserDomAdapter.makeCurrent(); |
| 31 | + |
| 32 | + let builder: TestComponentBuilder; |
| 33 | + let overlay: Overlay; |
| 34 | + let componentPortal: ComponentPortal; |
| 35 | + let templatePortal: TemplatePortal; |
| 36 | + let overlayContainerElement: Element; |
| 37 | + |
| 38 | + beforeEachProviders(() => [ |
| 39 | + Overlay, |
| 40 | + provide(OVERLAY_CONTAINER_TOKEN, {useFactory: () => { |
| 41 | + overlayContainerElement = DOM.createElement('div'); |
| 42 | + return overlayContainerElement; |
| 43 | + }}) |
| 44 | + ]); |
| 45 | + |
| 46 | + let deps = [TestComponentBuilder, Overlay]; |
| 47 | + beforeEach(inject(deps, fakeAsync((tcb: TestComponentBuilder, o: Overlay) => { |
| 48 | + builder = tcb; |
| 49 | + overlay = o; |
| 50 | + |
| 51 | + builder.createAsync(TestComponentWithTemplatePortals).then(fixture => { |
| 52 | + fixture.detectChanges(); |
| 53 | + templatePortal = fixture.componentInstance.templatePortal; |
| 54 | + componentPortal = new ComponentPortal(PizzaMsg, fixture.componentInstance.elementRef); |
| 55 | + }); |
| 56 | + |
| 57 | + flushMicrotasks(); |
| 58 | + }))); |
| 59 | + |
| 60 | + it('should load a component into an overlay', fakeAsyncTest(() => { |
| 61 | + let overlayRef: OverlayRef; |
| 62 | + |
| 63 | + overlay.create().then(ref => { |
| 64 | + overlayRef = ref; |
| 65 | + overlayRef.attach(componentPortal); |
| 66 | + }); |
| 67 | + |
| 68 | + flushMicrotasks(); |
| 69 | + |
| 70 | + expect(overlayContainerElement.textContent).toContain('Pizza'); |
| 71 | + |
| 72 | + overlayRef.dispose(); |
| 73 | + expect(overlayContainerElement.childNodes.length).toBe(0); |
| 74 | + expect(overlayContainerElement.textContent).toBe(''); |
| 75 | + })); |
| 76 | + |
| 77 | + it('should load a template portal into an overlay', fakeAsyncTest(() => { |
| 78 | + let overlayRef: OverlayRef; |
| 79 | + |
| 80 | + overlay.create().then(ref => { |
| 81 | + overlayRef = ref; |
| 82 | + overlayRef.attach(templatePortal); |
| 83 | + }); |
| 84 | + |
| 85 | + flushMicrotasks(); |
| 86 | + |
| 87 | + expect(overlayContainerElement.textContent).toContain('Cake'); |
| 88 | + |
| 89 | + overlayRef.dispose(); |
| 90 | + expect(overlayContainerElement.childNodes.length).toBe(0); |
| 91 | + expect(overlayContainerElement.textContent).toBe(''); |
| 92 | + })); |
| 93 | + |
| 94 | + it('should open multiple overlays', fakeAsyncTest(() => { |
| 95 | + let pizzaOverlayRef: OverlayRef; |
| 96 | + let cakeOverlayRef: OverlayRef; |
| 97 | + |
| 98 | + overlay.create().then(ref => { |
| 99 | + pizzaOverlayRef = ref; |
| 100 | + pizzaOverlayRef.attach(componentPortal); |
| 101 | + }); |
| 102 | + |
| 103 | + flushMicrotasks(); |
| 104 | + |
| 105 | + overlay.create().then(ref => { |
| 106 | + cakeOverlayRef = ref; |
| 107 | + cakeOverlayRef.attach(templatePortal); |
| 108 | + }); |
| 109 | + |
| 110 | + flushMicrotasks(); |
| 111 | + |
| 112 | + expect(overlayContainerElement.childNodes.length).toBe(2); |
| 113 | + expect(overlayContainerElement.textContent).toContain('Pizza'); |
| 114 | + expect(overlayContainerElement.textContent).toContain('Cake'); |
| 115 | + |
| 116 | + pizzaOverlayRef.dispose(); |
| 117 | + expect(overlayContainerElement.childNodes.length).toBe(1); |
| 118 | + expect(overlayContainerElement.textContent).toContain('Cake'); |
| 119 | + |
| 120 | + cakeOverlayRef.dispose(); |
| 121 | + expect(overlayContainerElement.childNodes.length).toBe(0); |
| 122 | + expect(overlayContainerElement.textContent).toBe(''); |
| 123 | + })); |
| 124 | + }); |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | +/** Simple component for testing ComponentPortal. */ |
| 129 | +@Component({ |
| 130 | + selector: 'pizza-msg', |
| 131 | + template: '<p>Pizza</p>', |
| 132 | +}) |
| 133 | +class PizzaMsg {} |
| 134 | + |
| 135 | + |
| 136 | +/** Test-bed component that contains a TempatePortal and an ElementRef. */ |
| 137 | +@Component({ |
| 138 | + selector: 'portal-test', |
| 139 | + template: `<template portal>Cake</template>`, |
| 140 | + directives: [TemplatePortalDirective], |
| 141 | +}) |
| 142 | +class TestComponentWithTemplatePortals { |
| 143 | + @ViewChild(TemplatePortalDirective) templatePortal: TemplatePortalDirective; |
| 144 | + constructor(public elementRef: ElementRef) { } |
| 145 | +} |
| 146 | + |
| 147 | +function fakeAsyncTest(fn: () => void) { |
| 148 | + return inject([], fakeAsync(fn)); |
| 149 | +} |
0 commit comments