1
- import { ReflectiveInjector } from '@angular/core ' ;
2
- import { Location , LocationStrategy , PlatformLocation , ViewportScroller } from '@angular/common' ;
3
- import { DOCUMENT } from '@angular/common' ;
4
- import { MockLocationStrategy , SpyLocation } from '@angular/common/testing ' ;
5
- import { fakeAsync , tick } from '@angular/core/testing' ;
1
+ import { Location , LocationStrategy , PlatformLocation , ViewportScroller } from '@angular/common ' ;
2
+ import { DOCUMENT } from '@angular/common' ;
3
+ import { MockLocationStrategy , SpyLocation } from '@angular/common/testing ' ;
4
+ import { ReflectiveInjector } from '@angular/core ' ;
5
+ import { fakeAsync , tick } from '@angular/core/testing' ;
6
6
7
- import { ScrollService , topMargin } from './scroll.service' ;
7
+ import { ScrollService , topMargin } from './scroll.service' ;
8
8
9
9
describe ( 'ScrollService' , ( ) => {
10
10
const scrollServiceInstances : ScrollService [ ] = [ ] ;
@@ -32,27 +32,25 @@ describe('ScrollService', () => {
32
32
}
33
33
34
34
class MockElement {
35
- getBoundingClientRect = jasmine . createSpy ( 'Element getBoundingClientRect' )
36
- . and . returnValue ( { top : 0 } ) ;
35
+ getBoundingClientRect =
36
+ jasmine . createSpy ( 'Element getBoundingClientRect' ) . and . returnValue ( { top : 0 } ) ;
37
37
scrollIntoView = jasmine . createSpy ( 'Element scrollIntoView' ) ;
38
38
}
39
39
40
- const viewportScrollerStub = jasmine . createSpyObj (
41
- 'viewportScroller' ,
42
- [ 'getScrollPosition' , 'scrollToPosition' ] ) ;
40
+ const viewportScrollerStub =
41
+ jasmine . createSpyObj ( 'viewportScroller' , [ 'getScrollPosition' , 'scrollToPosition' ] ) ;
43
42
44
43
beforeEach ( ( ) => {
45
44
injector = ReflectiveInjector . resolveAndCreate ( [
46
- {
47
- provide : ScrollService ,
48
- useFactory : createScrollService ,
49
- deps : [ DOCUMENT , PlatformLocation , ViewportScroller , Location ] ,
50
- } ,
51
- { provide : Location , useClass : SpyLocation } ,
52
- { provide : DOCUMENT , useClass : MockDocument } ,
53
- { provide : PlatformLocation , useClass : MockPlatformLocation } ,
54
- { provide : ViewportScroller , useValue : viewportScrollerStub } ,
55
- { provide : LocationStrategy , useClass : MockLocationStrategy }
45
+ {
46
+ provide : ScrollService ,
47
+ useFactory : createScrollService ,
48
+ deps : [ DOCUMENT , PlatformLocation , ViewportScroller , Location ] ,
49
+ } ,
50
+ { provide : Location , useClass : SpyLocation } , { provide : DOCUMENT , useClass : MockDocument } ,
51
+ { provide : PlatformLocation , useClass : MockPlatformLocation } ,
52
+ { provide : ViewportScroller , useValue : viewportScrollerStub } ,
53
+ { provide : LocationStrategy , useClass : MockLocationStrategy }
56
54
] ) ;
57
55
platformLocation = injector . get ( PlatformLocation ) ;
58
56
document = injector . get ( DOCUMENT ) ;
@@ -68,18 +66,19 @@ describe('ScrollService', () => {
68
66
} ) ;
69
67
70
68
it ( 'should debounce `updateScrollPositonInHistory()`' , fakeAsync ( ( ) => {
71
- const updateScrollPositionInHistorySpy = spyOn ( scrollService , 'updateScrollPositionInHistory' ) ;
72
-
73
- window . dispatchEvent ( new Event ( 'scroll' ) ) ;
74
- tick ( 249 ) ;
75
- window . dispatchEvent ( new Event ( 'scroll' ) ) ;
76
- tick ( 249 ) ;
77
- window . dispatchEvent ( new Event ( 'scroll' ) ) ;
78
- tick ( 249 ) ;
79
- expect ( updateScrollPositionInHistorySpy ) . not . toHaveBeenCalled ( ) ;
80
- tick ( 1 ) ;
81
- expect ( updateScrollPositionInHistorySpy ) . toHaveBeenCalledTimes ( 1 ) ;
82
- } ) ) ;
69
+ const updateScrollPositionInHistorySpy =
70
+ spyOn ( scrollService , 'updateScrollPositionInHistory' ) ;
71
+
72
+ window . dispatchEvent ( new Event ( 'scroll' ) ) ;
73
+ tick ( 249 ) ;
74
+ window . dispatchEvent ( new Event ( 'scroll' ) ) ;
75
+ tick ( 249 ) ;
76
+ window . dispatchEvent ( new Event ( 'scroll' ) ) ;
77
+ tick ( 249 ) ;
78
+ expect ( updateScrollPositionInHistorySpy ) . not . toHaveBeenCalled ( ) ;
79
+ tick ( 1 ) ;
80
+ expect ( updateScrollPositionInHistorySpy ) . toHaveBeenCalledTimes ( 1 ) ;
81
+ } ) ) ;
83
82
84
83
it ( 'should set `scrollRestoration` to `manual` if supported' , ( ) => {
85
84
if ( scrollService . supportManualScrollRestoration ) {
@@ -96,7 +95,9 @@ describe('ScrollService', () => {
96
95
try {
97
96
// Simulate `window.sessionStorage` being inaccessible, when cookies are disabled.
98
97
Object . defineProperty ( window , 'sessionStorage' , {
99
- get ( ) { throw new Error ( 'The operation is insecure' ) ; } ,
98
+ get ( ) {
99
+ throw new Error ( 'The operation is insecure' ) ;
100
+ } ,
100
101
} ) ;
101
102
102
103
const platformLoc = platformLocation as PlatformLocation ;
@@ -198,8 +199,7 @@ describe('ScrollService', () => {
198
199
platformLocation . hash = '' ;
199
200
200
201
const topOfPage = new MockElement ( ) ;
201
- document . getElementById . and
202
- . callFake ( ( id : string ) => id === 'top-of-page' ? topOfPage : null ) ;
202
+ document . getElementById . and . callFake ( ( id : string ) => id === 'top-of-page' ? topOfPage : null ) ;
203
203
204
204
scrollService . scroll ( ) ;
205
205
expect ( topOfPage . scrollIntoView ) . toHaveBeenCalled ( ) ;
@@ -227,7 +227,7 @@ describe('ScrollService', () => {
227
227
228
228
it ( 'should scroll to the element whose id matches the hash with encoded characters' , ( ) => {
229
229
const element = new MockElement ( ) ;
230
- platformLocation . hash = '%F0%9F%91%8D' ; // 👍
230
+ platformLocation . hash = '%F0%9F%91%8D' ; // 👍
231
231
document . getElementById . and . returnValue ( element ) ;
232
232
233
233
scrollService . scroll ( ) ;
@@ -289,8 +289,7 @@ describe('ScrollService', () => {
289
289
it ( 'should scroll to top' , ( ) => {
290
290
const topOfPageElement = new MockElement ( ) as any as Element ;
291
291
document . getElementById . and . callFake (
292
- ( id : string ) => id === 'top-of-page' ? topOfPageElement : null
293
- ) ;
292
+ ( id : string ) => id === 'top-of-page' ? topOfPageElement : null ) ;
294
293
295
294
scrollService . scrollToTop ( ) ;
296
295
expect ( topOfPageElement . scrollIntoView ) . toHaveBeenCalled ( ) ;
@@ -312,58 +311,55 @@ describe('ScrollService', () => {
312
311
313
312
describe ( '#needToFixScrollPosition' , async ( ) => {
314
313
it ( 'should return true when popState event was fired after a back navigation if the browser supports ' +
315
- 'scrollRestoration`. Otherwise, needToFixScrollPosition() returns false' , ( ) => {
316
-
317
- if ( scrollService . supportManualScrollRestoration ) {
318
- location . go ( '/initial-url1' ) ;
319
- // We simulate a scroll down
320
- location . replaceState ( '/initial-url1' , 'hack' , { scrollPosition : [ 2000 , 0 ] } ) ;
321
- location . go ( '/initial-url2' ) ;
322
- location . back ( ) ;
323
-
324
- expect ( scrollService . poppedStateScrollPosition ) . toEqual ( [ 2000 , 0 ] ) ;
325
- expect ( scrollService . needToFixScrollPosition ( ) ) . toBe ( true ) ;
326
- } else {
327
- location . go ( '/initial-url1' ) ;
328
- location . go ( '/initial-url2' ) ;
329
- location . back ( ) ;
330
-
331
- expect ( scrollService . poppedStateScrollPosition ) . toBe ( null ) ;
332
- expect ( scrollService . needToFixScrollPosition ( ) ) . toBe ( false ) ;
333
- }
334
-
335
- } ) ;
314
+ 'scrollRestoration`. Otherwise, needToFixScrollPosition() returns false' ,
315
+ ( ) => {
316
+ if ( scrollService . supportManualScrollRestoration ) {
317
+ location . go ( '/initial-url1' ) ;
318
+ // We simulate a scroll down
319
+ location . replaceState ( '/initial-url1' , 'hack' , { scrollPosition : [ 2000 , 0 ] } ) ;
320
+ location . go ( '/initial-url2' ) ;
321
+ location . back ( ) ;
322
+
323
+ expect ( scrollService . poppedStateScrollPosition ) . toEqual ( [ 2000 , 0 ] ) ;
324
+ expect ( scrollService . needToFixScrollPosition ( ) ) . toBe ( true ) ;
325
+ } else {
326
+ location . go ( '/initial-url1' ) ;
327
+ location . go ( '/initial-url2' ) ;
328
+ location . back ( ) ;
329
+
330
+ expect ( scrollService . poppedStateScrollPosition ) . toBe ( null ) ;
331
+ expect ( scrollService . needToFixScrollPosition ( ) ) . toBe ( false ) ;
332
+ }
333
+ } ) ;
336
334
337
335
it ( 'should return true when popState event was fired after a forward navigation if the browser supports ' +
338
- 'scrollRestoration`. Otherwise, needToFixScrollPosition() returns false' , ( ) => {
339
-
340
- if ( scrollService . supportManualScrollRestoration ) {
341
- location . go ( '/initial-url1' ) ;
342
- location . go ( '/initial-url2' ) ;
343
- // We simulate a scroll down
344
- location . replaceState ( '/initial-url1' , 'hack' , { scrollPosition : [ 2000 , 0 ] } ) ;
345
-
346
- location . back ( ) ;
347
- scrollService . poppedStateScrollPosition = [ 0 , 0 ] ;
348
- location . forward ( ) ;
349
-
350
- expect ( scrollService . poppedStateScrollPosition ) . toEqual ( [ 2000 , 0 ] ) ;
351
- expect ( scrollService . needToFixScrollPosition ( ) ) . toBe ( true ) ;
352
- } else {
353
- location . go ( '/initial-url1' ) ;
354
- location . go ( '/initial-url2' ) ;
355
- location . back ( ) ;
356
- location . forward ( ) ;
357
-
358
- expect ( scrollService . poppedStateScrollPosition ) . toBe ( null ) ;
359
- expect ( scrollService . needToFixScrollPosition ( ) ) . toBe ( false ) ;
360
- }
361
-
362
- } ) ;
336
+ 'scrollRestoration`. Otherwise, needToFixScrollPosition() returns false' ,
337
+ ( ) => {
338
+ if ( scrollService . supportManualScrollRestoration ) {
339
+ location . go ( '/initial-url1' ) ;
340
+ location . go ( '/initial-url2' ) ;
341
+ // We simulate a scroll down
342
+ location . replaceState ( '/initial-url1' , 'hack' , { scrollPosition : [ 2000 , 0 ] } ) ;
343
+
344
+ location . back ( ) ;
345
+ scrollService . poppedStateScrollPosition = [ 0 , 0 ] ;
346
+ location . forward ( ) ;
347
+
348
+ expect ( scrollService . poppedStateScrollPosition ) . toEqual ( [ 2000 , 0 ] ) ;
349
+ expect ( scrollService . needToFixScrollPosition ( ) ) . toBe ( true ) ;
350
+ } else {
351
+ location . go ( '/initial-url1' ) ;
352
+ location . go ( '/initial-url2' ) ;
353
+ location . back ( ) ;
354
+ location . forward ( ) ;
355
+
356
+ expect ( scrollService . poppedStateScrollPosition ) . toBe ( null ) ;
357
+ expect ( scrollService . needToFixScrollPosition ( ) ) . toBe ( false ) ;
358
+ }
359
+ } ) ;
363
360
} ) ;
364
361
365
362
describe ( '#scrollAfterRender' , async ( ) => {
366
-
367
363
let scrollSpy : jasmine . Spy ;
368
364
let scrollToTopSpy : jasmine . Spy ;
369
365
let needToFixScrollPositionSpy : jasmine . Spy ;
@@ -383,69 +379,69 @@ describe('ScrollService', () => {
383
379
384
380
385
381
it ( 'should call `scroll` when we navigate to a location with anchor' , fakeAsync ( ( ) => {
386
- needToFixScrollPositionSpy . and . returnValue ( false ) ;
387
- getStoredScrollPositionSpy . and . returnValue ( null ) ;
388
- isLocationWithHashSpy . and . returnValue ( true ) ;
382
+ needToFixScrollPositionSpy . and . returnValue ( false ) ;
383
+ getStoredScrollPositionSpy . and . returnValue ( null ) ;
384
+ isLocationWithHashSpy . and . returnValue ( true ) ;
389
385
390
- scrollService . scrollAfterRender ( scrollDelay ) ;
386
+ scrollService . scrollAfterRender ( scrollDelay ) ;
391
387
392
- expect ( scrollSpy ) . not . toHaveBeenCalled ( ) ;
393
- tick ( scrollDelay ) ;
394
- expect ( scrollSpy ) . toHaveBeenCalled ( ) ;
395
- } ) ) ;
388
+ expect ( scrollSpy ) . not . toHaveBeenCalled ( ) ;
389
+ tick ( scrollDelay ) ;
390
+ expect ( scrollSpy ) . toHaveBeenCalled ( ) ;
391
+ } ) ) ;
396
392
397
393
it ( 'should call `scrollToTop` when we navigate to a location without anchor' , fakeAsync ( ( ) => {
398
- needToFixScrollPositionSpy . and . returnValue ( false ) ;
399
- getStoredScrollPositionSpy . and . returnValue ( null ) ;
400
- isLocationWithHashSpy . and . returnValue ( false ) ;
394
+ needToFixScrollPositionSpy . and . returnValue ( false ) ;
395
+ getStoredScrollPositionSpy . and . returnValue ( null ) ;
396
+ isLocationWithHashSpy . and . returnValue ( false ) ;
401
397
402
- scrollService . scrollAfterRender ( scrollDelay ) ;
398
+ scrollService . scrollAfterRender ( scrollDelay ) ;
403
399
404
- expect ( scrollToTopSpy ) . toHaveBeenCalled ( ) ;
405
- tick ( scrollDelay ) ;
406
- expect ( scrollSpy ) . not . toHaveBeenCalled ( ) ;
407
- } ) ) ;
400
+ expect ( scrollToTopSpy ) . toHaveBeenCalled ( ) ;
401
+ tick ( scrollDelay ) ;
402
+ expect ( scrollSpy ) . not . toHaveBeenCalled ( ) ;
403
+ } ) ) ;
408
404
409
405
it ( 'should call `viewportScroller.scrollToPosition` when we reload a page' , fakeAsync ( ( ) => {
410
- getStoredScrollPositionSpy . and . returnValue ( [ 0 , 1000 ] ) ;
406
+ getStoredScrollPositionSpy . and . returnValue ( [ 0 , 1000 ] ) ;
411
407
412
- scrollService . scrollAfterRender ( scrollDelay ) ;
408
+ scrollService . scrollAfterRender ( scrollDelay ) ;
413
409
414
- expect ( viewportScrollerStub . scrollToPosition ) . toHaveBeenCalled ( ) ;
415
- expect ( getStoredScrollPositionSpy ) . toHaveBeenCalled ( ) ;
416
- } ) ) ;
410
+ expect ( viewportScrollerStub . scrollToPosition ) . toHaveBeenCalled ( ) ;
411
+ expect ( getStoredScrollPositionSpy ) . toHaveBeenCalled ( ) ;
412
+ } ) ) ;
417
413
418
414
it ( 'should call `scrollToPosition` after a popState' , fakeAsync ( ( ) => {
419
- needToFixScrollPositionSpy . and . returnValue ( true ) ;
420
- getStoredScrollPositionSpy . and . returnValue ( null ) ;
421
- scrollService . scrollAfterRender ( scrollDelay ) ;
422
- expect ( scrollToPosition ) . toHaveBeenCalled ( ) ;
423
- tick ( scrollDelay ) ;
424
- expect ( scrollSpy ) . not . toHaveBeenCalled ( ) ;
425
- expect ( scrollToTopSpy ) . not . toHaveBeenCalled ( ) ;
426
- } ) ) ;
415
+ needToFixScrollPositionSpy . and . returnValue ( true ) ;
416
+ getStoredScrollPositionSpy . and . returnValue ( null ) ;
417
+ scrollService . scrollAfterRender ( scrollDelay ) ;
418
+ expect ( scrollToPosition ) . toHaveBeenCalled ( ) ;
419
+ tick ( scrollDelay ) ;
420
+ expect ( scrollSpy ) . not . toHaveBeenCalled ( ) ;
421
+ expect ( scrollToTopSpy ) . not . toHaveBeenCalled ( ) ;
422
+ } ) ) ;
427
423
} ) ;
428
424
429
425
describe ( 'once destroyed' , ( ) => {
430
426
it ( 'should stop updating scroll position' , fakeAsync ( ( ) => {
431
- const updateScrollPositionInHistorySpy =
432
- spyOn ( scrollService , 'updateScrollPositionInHistory' ) ;
427
+ const updateScrollPositionInHistorySpy =
428
+ spyOn ( scrollService , 'updateScrollPositionInHistory' ) ;
433
429
434
- window . dispatchEvent ( new Event ( 'scroll' ) ) ;
435
- tick ( 250 ) ;
436
- expect ( updateScrollPositionInHistorySpy ) . toHaveBeenCalledTimes ( 1 ) ;
430
+ window . dispatchEvent ( new Event ( 'scroll' ) ) ;
431
+ tick ( 250 ) ;
432
+ expect ( updateScrollPositionInHistorySpy ) . toHaveBeenCalledTimes ( 1 ) ;
437
433
438
- window . dispatchEvent ( new Event ( 'scroll' ) ) ;
439
- tick ( 250 ) ;
440
- expect ( updateScrollPositionInHistorySpy ) . toHaveBeenCalledTimes ( 2 ) ;
434
+ window . dispatchEvent ( new Event ( 'scroll' ) ) ;
435
+ tick ( 250 ) ;
436
+ expect ( updateScrollPositionInHistorySpy ) . toHaveBeenCalledTimes ( 2 ) ;
441
437
442
- updateScrollPositionInHistorySpy . calls . reset ( ) ;
443
- scrollService . ngOnDestroy ( ) ;
438
+ updateScrollPositionInHistorySpy . calls . reset ( ) ;
439
+ scrollService . ngOnDestroy ( ) ;
444
440
445
- window . dispatchEvent ( new Event ( 'scroll' ) ) ;
446
- tick ( 250 ) ;
447
- expect ( updateScrollPositionInHistorySpy ) . not . toHaveBeenCalled ( ) ;
448
- } ) ) ;
441
+ window . dispatchEvent ( new Event ( 'scroll' ) ) ;
442
+ tick ( 250 ) ;
443
+ expect ( updateScrollPositionInHistorySpy ) . not . toHaveBeenCalled ( ) ;
444
+ } ) ) ;
449
445
450
446
it ( 'should stop updating the stored location href' , ( ) => {
451
447
const updateScrollLocationHrefSpy = spyOn ( scrollService , 'updateScrollLocationHref' ) ;
0 commit comments