|
| 1 | +import {inject, TestBed, async} from '@angular/core/testing'; |
| 2 | +import {NgModule, Component} from '@angular/core'; |
| 3 | +import {Subject} from 'rxjs/Subject'; |
| 4 | +import { |
| 5 | + PortalModule, |
| 6 | + ComponentPortal, |
| 7 | + Overlay, |
| 8 | + OverlayState, |
| 9 | + OverlayRef, |
| 10 | + OverlayModule, |
| 11 | + ScrollStrategy, |
| 12 | + ScrollDispatcher, |
| 13 | + CloseScrollStrategy, |
| 14 | +} from '../../core'; |
| 15 | + |
| 16 | + |
| 17 | +describe('CloseScrollStrategy', () => { |
| 18 | + let overlayRef: OverlayRef; |
| 19 | + let componentPortal: ComponentPortal<MozarellaMsg>; |
| 20 | + let scrolledSubject = new Subject(); |
| 21 | + |
| 22 | + beforeEach(async(() => { |
| 23 | + TestBed.configureTestingModule({ |
| 24 | + imports: [OverlayModule, PortalModule, OverlayTestModule], |
| 25 | + providers: [ |
| 26 | + {provide: ScrollDispatcher, useFactory: () => { |
| 27 | + return {scrolled: (delay: number, callback: () => any) => { |
| 28 | + return scrolledSubject.asObservable().subscribe(callback); |
| 29 | + }}; |
| 30 | + }} |
| 31 | + ] |
| 32 | + }); |
| 33 | + |
| 34 | + TestBed.compileComponents(); |
| 35 | + })); |
| 36 | + |
| 37 | + beforeEach(inject([Overlay, ScrollDispatcher], (overlay: Overlay, |
| 38 | + scrollDispatcher: ScrollDispatcher) => { |
| 39 | + |
| 40 | + let overlayState = new OverlayState(); |
| 41 | + overlayState.scrollStrategy = new CloseScrollStrategy(scrollDispatcher); |
| 42 | + overlayRef = overlay.create(overlayState); |
| 43 | + componentPortal = new ComponentPortal(MozarellaMsg); |
| 44 | + })); |
| 45 | + |
| 46 | + it('should detach the overlay as soon as the user scrolls', () => { |
| 47 | + overlayRef.attach(componentPortal); |
| 48 | + spyOn(overlayRef, 'detach'); |
| 49 | + |
| 50 | + scrolledSubject.next(); |
| 51 | + expect(overlayRef.detach).toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should not attempt to detach the overlay after it has been detached', () => { |
| 55 | + overlayRef.attach(componentPortal); |
| 56 | + overlayRef.detach(); |
| 57 | + |
| 58 | + spyOn(overlayRef, 'detach'); |
| 59 | + scrolledSubject.next(); |
| 60 | + |
| 61 | + expect(overlayRef.detach).not.toHaveBeenCalled(); |
| 62 | + }); |
| 63 | + |
| 64 | +}); |
| 65 | + |
| 66 | + |
| 67 | +/** Simple component that we can attach to the overlay. */ |
| 68 | +@Component({template: '<p>Mozarella</p>'}) |
| 69 | +class MozarellaMsg { } |
| 70 | + |
| 71 | + |
| 72 | +/** Test module to hold the component. */ |
| 73 | +@NgModule({ |
| 74 | + imports: [OverlayModule, PortalModule], |
| 75 | + declarations: [MozarellaMsg], |
| 76 | + entryComponents: [MozarellaMsg], |
| 77 | +}) |
| 78 | +class OverlayTestModule { } |
0 commit comments