1- import { inject , TestBed , async , ComponentFixture } from '@angular/core/testing' ;
2- import { NgModule , Component , ViewChild , ElementRef , QueryList , ViewChildren } from '@angular/core' ;
1+ import { inject , TestBed , async , fakeAsync , ComponentFixture , tick } from '@angular/core/testing' ;
2+ import { NgModule , Component , ViewChild , ElementRef } from '@angular/core' ;
33import { ScrollDispatcher } from './scroll-dispatcher' ;
44import { OverlayModule } from '../overlay-directives' ;
55import { Scrollable } from './scrollable' ;
@@ -38,15 +38,17 @@ describe('Scroll Dispatcher', () => {
3838 expect ( scroll . scrollableReferences . has ( componentScrollable ) ) . toBe ( false ) ;
3939 } ) ;
4040
41- it ( 'should notify through the directive and service that a scroll event occurred' , ( ) => {
41+ it ( 'should notify through the directive and service that a scroll event occurred' ,
42+ fakeAsync ( ( ) => {
4243 let hasDirectiveScrollNotified = false ;
4344 // Listen for notifications from scroll directive
4445 let scrollable = fixture . componentInstance . scrollable ;
4546 scrollable . elementScrolled ( ) . subscribe ( ( ) => { hasDirectiveScrollNotified = true ; } ) ;
4647
47- // Listen for notifications from scroll service
48+ // Listen for notifications from scroll service with a throttle of 100ms
49+ const throttleTime = 100 ;
4850 let hasServiceScrollNotified = false ;
49- scroll . scrolled ( ) . subscribe ( ( ) => { hasServiceScrollNotified = true ; } ) ;
51+ scroll . scrolled ( throttleTime ) . subscribe ( ( ) => { hasServiceScrollNotified = true ; } ) ;
5052
5153 // Emit a scroll event from the scrolling element in our component.
5254 // This event should be picked up by the scrollable directive and notify.
@@ -55,9 +57,17 @@ describe('Scroll Dispatcher', () => {
5557 scrollEvent . initUIEvent ( 'scroll' , true , true , window , 0 ) ;
5658 fixture . componentInstance . scrollingElement . nativeElement . dispatchEvent ( scrollEvent ) ;
5759
60+ // The scrollable directive should have notified the service immediately.
5861 expect ( hasDirectiveScrollNotified ) . toBe ( true ) ;
62+
63+ // Verify that the throttle is used, the service should wait for the throttle time until
64+ // sending the notification.
65+ expect ( hasServiceScrollNotified ) . toBe ( false ) ;
66+
67+ // After the throttle time, the notification should be sent.
68+ tick ( throttleTime ) ;
5969 expect ( hasServiceScrollNotified ) . toBe ( true ) ;
60- } ) ;
70+ } ) ) ;
6171 } ) ;
6272
6373 describe ( 'Nested scrollables' , ( ) => {
@@ -107,7 +117,6 @@ class ScrollingComponent {
107117} )
108118class NestedScrollingComponent {
109119 @ViewChild ( 'interestingElement' ) interestingElement : ElementRef ;
110- @ViewChildren ( Scrollable ) scrollables : QueryList < Scrollable > ;
111120}
112121
113122const TEST_COMPONENTS = [ ScrollingComponent , NestedScrollingComponent ] ;
0 commit comments