|
| 1 | +import {async, inject, TestBed} from '@angular/core/testing'; |
| 2 | +import {Component, NgModule, ViewChild, ViewContainerRef} from '@angular/core'; |
| 3 | +import {PortalModule, CdkPortal} from '@angular/cdk/portal'; |
| 4 | +import {DOCUMENT} from '@angular/platform-browser'; |
| 5 | +import {Overlay, OverlayContainer, OverlayModule, FullscreenOverlayContainer} from './index'; |
| 6 | + |
| 7 | +describe('FullscreenOverlayContainer', () => { |
| 8 | + let overlay: Overlay; |
| 9 | + let overlayContainer: FullscreenOverlayContainer; |
| 10 | + let fullscreenListeners: Set<Function>; |
| 11 | + let fakeDocument: any; |
| 12 | + |
| 13 | + beforeEach(async(() => { |
| 14 | + fullscreenListeners = new Set(); |
| 15 | + |
| 16 | + TestBed.configureTestingModule({ |
| 17 | + imports: [OverlayTestModule], |
| 18 | + providers: [{ |
| 19 | + provide: DOCUMENT, |
| 20 | + useFactory: () => { |
| 21 | + // Provide a (very limited) stub for the document. This is the most practical solution for |
| 22 | + // now since we only hit a handful of Document APIs. If we end up having to add more |
| 23 | + // stubs here, we should reconsider whether to use a Proxy instead. Avoiding a proxy for |
| 24 | + // now since it isn't supported on IE. See: |
| 25 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy |
| 26 | + fakeDocument = { |
| 27 | + body: document.body, |
| 28 | + fullscreenElement: document.createElement('div'), |
| 29 | + fullscreenEnabled: true, |
| 30 | + addEventListener: function(eventName: string, listener: Function) { |
| 31 | + if (eventName === 'fullscreenchange') { |
| 32 | + fullscreenListeners.add(listener); |
| 33 | + } else { |
| 34 | + document.addEventListener.apply(document, arguments); |
| 35 | + } |
| 36 | + }, |
| 37 | + removeEventListener: function(eventName: string, listener: Function) { |
| 38 | + if (eventName === 'fullscreenchange') { |
| 39 | + fullscreenListeners.delete(listener); |
| 40 | + } else { |
| 41 | + document.addEventListener.apply(document, arguments); |
| 42 | + } |
| 43 | + }, |
| 44 | + querySelectorAll: function() { |
| 45 | + return document.querySelectorAll.apply(document, arguments); |
| 46 | + }, |
| 47 | + createElement: function() { |
| 48 | + return document.createElement.apply(document, arguments); |
| 49 | + }, |
| 50 | + }; |
| 51 | + |
| 52 | + return fakeDocument; |
| 53 | + } |
| 54 | + }] |
| 55 | + }).compileComponents(); |
| 56 | + })); |
| 57 | + |
| 58 | + beforeEach(inject([Overlay, OverlayContainer], (o: Overlay, oc: OverlayContainer) => { |
| 59 | + overlay = o; |
| 60 | + overlayContainer = oc as FullscreenOverlayContainer; |
| 61 | + })); |
| 62 | + |
| 63 | + afterEach(() => { |
| 64 | + overlayContainer.ngOnDestroy(); |
| 65 | + fakeDocument = null; |
| 66 | + }); |
| 67 | + |
| 68 | + it('should open an overlay inside a fullscreen element and move it to the body', () => { |
| 69 | + const fixture = TestBed.createComponent(TestComponentWithTemplatePortals); |
| 70 | + const overlayRef = overlay.create(); |
| 71 | + const fullscreenElement = fakeDocument.fullscreenElement; |
| 72 | + |
| 73 | + overlayRef.attach(fixture.componentInstance.templatePortal); |
| 74 | + fixture.detectChanges(); |
| 75 | + |
| 76 | + expect(fullscreenElement.contains(overlayRef.overlayElement)).toBe(true); |
| 77 | + |
| 78 | + fakeDocument.fullscreenElement = null; |
| 79 | + fullscreenListeners.forEach(listener => listener()); |
| 80 | + fixture.detectChanges(); |
| 81 | + |
| 82 | + expect(fullscreenElement.contains(overlayRef.overlayElement)).toBe(false); |
| 83 | + expect(document.body.contains(overlayRef.overlayElement)).toBe(true); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should open an overlay inside the body and move it to a fullscreen element', () => { |
| 87 | + const fullscreenElement = fakeDocument.fullscreenElement; |
| 88 | + fakeDocument.fullscreenElement = null; |
| 89 | + |
| 90 | + const fixture = TestBed.createComponent(TestComponentWithTemplatePortals); |
| 91 | + const overlayRef = overlay.create(); |
| 92 | + |
| 93 | + overlayRef.attach(fixture.componentInstance.templatePortal); |
| 94 | + fixture.detectChanges(); |
| 95 | + |
| 96 | + expect(fullscreenElement.contains(overlayRef.overlayElement)).toBe(false); |
| 97 | + expect(document.body.contains(overlayRef.overlayElement)).toBe(true); |
| 98 | + |
| 99 | + fakeDocument.fullscreenElement = fullscreenElement; |
| 100 | + fullscreenListeners.forEach(listener => listener()); |
| 101 | + fixture.detectChanges(); |
| 102 | + |
| 103 | + expect(fullscreenElement.contains(overlayRef.overlayElement)).toBe(true); |
| 104 | + }); |
| 105 | + |
| 106 | +}); |
| 107 | + |
| 108 | +/** Test-bed component that contains a TempatePortal and an ElementRef. */ |
| 109 | +@Component({ |
| 110 | + template: `<ng-template cdk-portal>Cake</ng-template>`, |
| 111 | + providers: [Overlay], |
| 112 | +}) |
| 113 | +class TestComponentWithTemplatePortals { |
| 114 | + @ViewChild(CdkPortal) templatePortal: CdkPortal; |
| 115 | + |
| 116 | + constructor(public viewContainerRef: ViewContainerRef) { } |
| 117 | +} |
| 118 | + |
| 119 | +@NgModule({ |
| 120 | + imports: [OverlayModule, PortalModule], |
| 121 | + declarations: [TestComponentWithTemplatePortals], |
| 122 | + providers: [{ |
| 123 | + provide: OverlayContainer, |
| 124 | + useClass: FullscreenOverlayContainer |
| 125 | + }] |
| 126 | +}) |
| 127 | +class OverlayTestModule { } |
0 commit comments