11import { ReflectiveInjector } from '@angular/core' ;
2- import { PlatformLocation } from '@angular/common' ;
2+ import { Location , LocationStrategy , PlatformLocation , ViewportScroller } from '@angular/common' ;
33import { DOCUMENT } from '@angular/common' ;
4+ import { MockLocationStrategy , SpyLocation } from '@angular/common/testing' ;
5+ import { fakeAsync , tick } from '@angular/core/testing' ;
46
57import { ScrollService , topMargin } from './scroll.service' ;
68
79describe ( 'ScrollService' , ( ) => {
810 const topOfPageElem = { } as Element ;
911 let injector : ReflectiveInjector ;
1012 let document : MockDocument ;
11- let location : MockPlatformLocation ;
13+ let platformLocation : MockPlatformLocation ;
1214 let scrollService : ScrollService ;
15+ let location : SpyLocation ;
1316
1417 class MockPlatformLocation {
1518 hash : string ;
@@ -27,19 +30,36 @@ describe('ScrollService', () => {
2730 scrollIntoView = jasmine . createSpy ( 'Element scrollIntoView' ) ;
2831 }
2932
33+ const viewportScrollerStub = jasmine . createSpyObj (
34+ 'viewportScroller' ,
35+ [ 'getScrollPosition' , 'scrollToPosition' ] ) ;
36+
37+
3038 beforeEach ( ( ) => {
3139 spyOn ( window , 'scrollBy' ) ;
3240 } ) ;
3341
3442 beforeEach ( ( ) => {
3543 injector = ReflectiveInjector . resolveAndCreate ( [
3644 ScrollService ,
45+ { provide : Location , useClass : SpyLocation } ,
3746 { provide : DOCUMENT , useClass : MockDocument } ,
38- { provide : PlatformLocation , useClass : MockPlatformLocation }
47+ { provide : PlatformLocation , useClass : MockPlatformLocation } ,
48+ { provide : ViewportScroller , useValue : viewportScrollerStub } ,
49+ { provide : LocationStrategy , useClass : MockLocationStrategy }
3950 ] ) ;
40- location = injector . get ( PlatformLocation ) ;
51+ platformLocation = injector . get ( PlatformLocation ) ;
4152 document = injector . get ( DOCUMENT ) ;
4253 scrollService = injector . get ( ScrollService ) ;
54+ location = injector . get ( Location ) ;
55+ } ) ;
56+
57+ it ( 'should set `scrollRestoration` to `manual` if supported' , ( ) => {
58+ if ( scrollService . supportManualScrollRestoration ) {
59+ expect ( window . history . scrollRestoration ) . toBe ( 'manual' ) ;
60+ } else {
61+ expect ( window . history . scrollRestoration ) . toBeUndefined ( ) ;
62+ }
4363 } ) ;
4464
4565 describe ( '#topOffset' , ( ) => {
@@ -107,7 +127,7 @@ describe('ScrollService', () => {
107127
108128 describe ( '#scroll' , ( ) => {
109129 it ( 'should scroll to the top if there is no hash' , ( ) => {
110- location . hash = '' ;
130+ platformLocation . hash = '' ;
111131
112132 const topOfPage = new MockElement ( ) ;
113133 document . getElementById . and
@@ -118,7 +138,7 @@ describe('ScrollService', () => {
118138 } ) ;
119139
120140 it ( 'should not scroll if the hash does not match an element id' , ( ) => {
121- location . hash = 'not-found' ;
141+ platformLocation . hash = 'not-found' ;
122142 document . getElementById . and . returnValue ( null ) ;
123143
124144 scrollService . scroll ( ) ;
@@ -128,7 +148,7 @@ describe('ScrollService', () => {
128148
129149 it ( 'should scroll to the element whose id matches the hash' , ( ) => {
130150 const element = new MockElement ( ) ;
131- location . hash = 'some-id' ;
151+ platformLocation . hash = 'some-id' ;
132152 document . getElementById . and . returnValue ( element ) ;
133153
134154 scrollService . scroll ( ) ;
@@ -139,7 +159,7 @@ describe('ScrollService', () => {
139159
140160 it ( 'should scroll to the element whose id matches the hash with encoded characters' , ( ) => {
141161 const element = new MockElement ( ) ;
142- location . hash = '%F0%9F%91%8D' ; // 👍
162+ platformLocation . hash = '%F0%9F%91%8D' ; // 👍
143163 document . getElementById . and . returnValue ( element ) ;
144164
145165 scrollService . scroll ( ) ;
@@ -210,4 +230,136 @@ describe('ScrollService', () => {
210230 } ) ;
211231 } ) ;
212232
233+ describe ( '#isLocationWithHash' , ( ) => {
234+ it ( 'should return true when the location has a hash' , ( ) => {
235+ platformLocation . hash = 'anchor' ;
236+ expect ( scrollService . isLocationWithHash ( ) ) . toBe ( true ) ;
237+ } ) ;
238+
239+ it ( 'should return false when the location has no hash' , ( ) => {
240+ platformLocation . hash = '' ;
241+ expect ( scrollService . isLocationWithHash ( ) ) . toBe ( false ) ;
242+ } ) ;
243+ } ) ;
244+
245+ describe ( '#needToFixScrollPosition' , async ( ) => {
246+ it ( 'should return true when popState event was fired after a back navigation if the browser supports ' +
247+ 'scrollRestoration`. Otherwise, needToFixScrollPosition() returns false' , ( ) => {
248+
249+ if ( scrollService . supportManualScrollRestoration ) {
250+ location . go ( '/initial-url1' ) ;
251+ // We simulate a scroll down
252+ location . replaceState ( '/initial-url1' , 'hack' , { scrollPosition : [ 2000 , 0 ] } ) ;
253+ location . go ( '/initial-url2' ) ;
254+ location . back ( ) ;
255+
256+ expect ( scrollService . popStateFired ) . toBe ( true ) ;
257+ expect ( scrollService . scrollPosition ) . toEqual ( [ 2000 , 0 ] ) ;
258+ expect ( scrollService . needToFixScrollPosition ( ) ) . toBe ( true ) ;
259+ } else {
260+ location . go ( '/initial-url1' ) ;
261+ location . go ( '/initial-url2' ) ;
262+ location . back ( ) ;
263+
264+ expect ( scrollService . popStateFired ) . toBe ( false ) ; // popStateFired is always false
265+ expect ( scrollService . scrollPosition ) . toEqual ( [ 0 , 0 ] ) ; // scrollPosition always equals [0, 0]
266+ expect ( scrollService . needToFixScrollPosition ( ) ) . toBe ( false ) ;
267+ }
268+
269+ } ) ;
270+
271+ it ( 'should return true when popState event was fired after a forward navigation if the browser supports ' +
272+ 'scrollRestoration`. Otherwise, needToFixScrollPosition() returns false' , ( ) => {
273+
274+ if ( scrollService . supportManualScrollRestoration ) {
275+ location . go ( '/initial-url1' ) ;
276+ location . go ( '/initial-url2' ) ;
277+ // We simulate a scroll down
278+ location . replaceState ( '/initial-url1' , 'hack' , { scrollPosition : [ 2000 , 0 ] } ) ;
279+
280+ location . back ( ) ;
281+ scrollService . popStateFired = false ;
282+ scrollService . scrollPosition = [ 0 , 0 ] ;
283+ location . forward ( ) ;
284+
285+ expect ( scrollService . popStateFired ) . toBe ( true ) ;
286+ expect ( scrollService . scrollPosition ) . toEqual ( [ 2000 , 0 ] ) ;
287+ expect ( scrollService . needToFixScrollPosition ( ) ) . toBe ( true ) ;
288+ } else {
289+ location . go ( '/initial-url1' ) ;
290+ location . go ( '/initial-url2' ) ;
291+ location . back ( ) ;
292+ location . forward ( ) ;
293+
294+ expect ( scrollService . popStateFired ) . toBe ( false ) ; // popStateFired is always false
295+ expect ( scrollService . scrollPosition ) . toEqual ( [ 0 , 0 ] ) ; // scrollPosition always equals [0, 0]
296+ expect ( scrollService . needToFixScrollPosition ( ) ) . toBe ( false ) ;
297+ }
298+
299+ } ) ;
300+ } ) ;
301+
302+ describe ( '#scrollAfterRender' , async ( ) => {
303+
304+ let scrollSpy : jasmine . Spy ;
305+ let scrollToTopSpy : jasmine . Spy ;
306+ let needToFixScrollPositionSpy : jasmine . Spy ;
307+ let scrollToPosition : jasmine . Spy ;
308+ let isLocationWithHashSpy : jasmine . Spy ;
309+ let getStoredScrollPositionSpy : jasmine . Spy ;
310+ const scrollDelay = 500 ;
311+
312+ beforeEach ( ( ) => {
313+ scrollSpy = spyOn ( scrollService , 'scroll' ) ;
314+ scrollToTopSpy = spyOn ( scrollService , 'scrollToTop' ) ;
315+ scrollToPosition = spyOn ( scrollService , 'scrollToPosition' ) ;
316+ needToFixScrollPositionSpy = spyOn ( scrollService , 'needToFixScrollPosition' ) ;
317+ getStoredScrollPositionSpy = spyOn ( scrollService , 'getStoredScrollPosition' ) ;
318+ isLocationWithHashSpy = spyOn ( scrollService , 'isLocationWithHash' ) ;
319+ } ) ;
320+
321+
322+ it ( 'should call `scroll` when we navigate to a location with anchor' , fakeAsync ( ( ) => {
323+ needToFixScrollPositionSpy . and . returnValue ( false ) ;
324+ getStoredScrollPositionSpy . and . returnValue ( null ) ;
325+ isLocationWithHashSpy . and . returnValue ( true ) ;
326+
327+ scrollService . scrollAfterRender ( scrollDelay ) ;
328+
329+ expect ( scrollSpy ) . not . toHaveBeenCalled ( ) ;
330+ tick ( scrollDelay ) ;
331+ expect ( scrollSpy ) . toHaveBeenCalled ( ) ;
332+ } ) ) ;
333+
334+ it ( 'should call `scrollToTop` when we navigate to a location without anchor' , fakeAsync ( ( ) => {
335+ needToFixScrollPositionSpy . and . returnValue ( false ) ;
336+ getStoredScrollPositionSpy . and . returnValue ( null ) ;
337+ isLocationWithHashSpy . and . returnValue ( false ) ;
338+
339+ scrollService . scrollAfterRender ( scrollDelay ) ;
340+
341+ expect ( scrollToTopSpy ) . toHaveBeenCalled ( ) ;
342+ tick ( scrollDelay ) ;
343+ expect ( scrollSpy ) . not . toHaveBeenCalled ( ) ;
344+ } ) ) ;
345+
346+ it ( 'should call `viewportScroller.scrollToPosition` when we reload a page' , fakeAsync ( ( ) => {
347+ getStoredScrollPositionSpy . and . returnValue ( [ 0 , 1000 ] ) ;
348+
349+ scrollService . scrollAfterRender ( scrollDelay ) ;
350+
351+ expect ( viewportScrollerStub . scrollToPosition ) . toHaveBeenCalled ( ) ;
352+ expect ( getStoredScrollPositionSpy ) . toHaveBeenCalled ( ) ;
353+ } ) ) ;
354+
355+ it ( 'should call `scrollToPosition` after a popState' , fakeAsync ( ( ) => {
356+ needToFixScrollPositionSpy . and . returnValue ( true ) ;
357+ getStoredScrollPositionSpy . and . returnValue ( null ) ;
358+ scrollService . scrollAfterRender ( scrollDelay ) ;
359+ expect ( scrollToPosition ) . toHaveBeenCalled ( ) ;
360+ tick ( scrollDelay ) ;
361+ expect ( scrollSpy ) . not . toHaveBeenCalled ( ) ;
362+ expect ( scrollToTopSpy ) . not . toHaveBeenCalled ( ) ;
363+ } ) ) ;
364+ } ) ;
213365} ) ;
0 commit comments