|
| 1 | +import {inject, TestBed, async, ComponentFixture} from '@angular/core/testing'; |
| 2 | +import {NgModule, Component, ViewChild, ElementRef} from '@angular/core'; |
| 3 | +import {ScrollDispatcher} from './scroll-dispatcher'; |
| 4 | +import {OverlayModule} from '../overlay-directives'; |
| 5 | +import {Scrollable} from './scrollable'; |
| 6 | + |
| 7 | +describe('Scroll Dispatcher', () => { |
| 8 | + let scroll: ScrollDispatcher; |
| 9 | + let fixture: ComponentFixture<ScrollingComponent>; |
| 10 | + |
| 11 | + beforeEach(async(() => { |
| 12 | + TestBed.configureTestingModule({ |
| 13 | + imports: [OverlayModule.forRoot(), ScrollTestModule], |
| 14 | + }); |
| 15 | + |
| 16 | + TestBed.compileComponents(); |
| 17 | + })); |
| 18 | + |
| 19 | + beforeEach(inject([ScrollDispatcher], (s: ScrollDispatcher) => { |
| 20 | + scroll = s; |
| 21 | + |
| 22 | + fixture = TestBed.createComponent(ScrollingComponent); |
| 23 | + fixture.detectChanges(); |
| 24 | + })); |
| 25 | + |
| 26 | + it('should be registered with the scrollable directive with the scroll service', () => { |
| 27 | + const componentScrollable = fixture.componentInstance.scrollable; |
| 28 | + expect(scroll.scrollableReferences.has(componentScrollable)).toBe(true); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should have the scrollable directive deregistered when the component is destroyed', () => { |
| 32 | + const componentScrollable = fixture.componentInstance.scrollable; |
| 33 | + expect(scroll.scrollableReferences.has(componentScrollable)).toBe(true); |
| 34 | + |
| 35 | + fixture.destroy(); |
| 36 | + expect(scroll.scrollableReferences.has(componentScrollable)).toBe(false); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should notify through the directive and service that a scroll event occurred', () => { |
| 40 | + let hasDirectiveScrollNotified = false; |
| 41 | + // Listen for notifications from scroll directive |
| 42 | + let scrollable = fixture.componentInstance.scrollable; |
| 43 | + scrollable.elementScrolled().subscribe(() => { hasDirectiveScrollNotified = true; }); |
| 44 | + |
| 45 | + // Listen for notifications from scroll service |
| 46 | + let hasServiceScrollNotified = false; |
| 47 | + scroll.scrolled().subscribe(() => { hasServiceScrollNotified = true; }); |
| 48 | + |
| 49 | + // Emit a scroll event from the scrolling element in our component. |
| 50 | + // This event should be picked up by the scrollable directive and notify. |
| 51 | + // The notification should be picked up by the service. |
| 52 | + const scrollEvent = document.createEvent('UIEvents'); |
| 53 | + scrollEvent.initUIEvent('scroll', true, true, window, 0); |
| 54 | + fixture.componentInstance.scrollingElement.nativeElement.dispatchEvent(scrollEvent); |
| 55 | + |
| 56 | + expect(hasDirectiveScrollNotified).toBe(true); |
| 57 | + expect(hasServiceScrollNotified).toBe(true); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | + |
| 62 | +/** Simple component that contains a large div and can be scrolled. */ |
| 63 | +@Component({ |
| 64 | + template: `<div #scrollingElement cdk-scrollable style="height: 9999px"></div>` |
| 65 | +}) |
| 66 | +class ScrollingComponent { |
| 67 | + @ViewChild(Scrollable) scrollable: Scrollable; |
| 68 | + @ViewChild('scrollingElement') scrollingElement: ElementRef; |
| 69 | +} |
| 70 | + |
| 71 | +const TEST_COMPONENTS = [ScrollingComponent]; |
| 72 | +@NgModule({ |
| 73 | + imports: [OverlayModule], |
| 74 | + providers: [ScrollDispatcher], |
| 75 | + exports: TEST_COMPONENTS, |
| 76 | + declarations: TEST_COMPONENTS, |
| 77 | + entryComponents: TEST_COMPONENTS, |
| 78 | +}) |
| 79 | +class ScrollTestModule { } |
0 commit comments