@@ -5,6 +5,7 @@ import {Injector} from '@angular/core';
55import { fakeAsync , tick } from '@angular/core/testing' ;
66
77import { ScrollService , topMargin } from './scroll.service' ;
8+ import { SessionStorage , NoopStorage } from './storage.service' ;
89
910describe ( 'ScrollService' , ( ) => {
1011 const scrollServiceInstances : ScrollService [ ] = [ ] ;
@@ -20,6 +21,7 @@ describe('ScrollService', () => {
2021 let platformLocation : MockPlatformLocation ;
2122 let scrollService : ScrollService ;
2223 let location : SpyLocation ;
24+ let sessionStorage : Storage ;
2325
2426 class MockPlatformLocation {
2527 hash : string ;
@@ -46,27 +48,28 @@ describe('ScrollService', () => {
4648 {
4749 provide : ScrollService ,
4850 useFactory : createScrollService ,
49- deps : [ DOCUMENT , PlatformLocation , ViewportScroller , Location ] ,
51+ deps : [ DOCUMENT , PlatformLocation , ViewportScroller , Location , SessionStorage ] ,
5052 } ,
51- { provide : Location , useClass : SpyLocation , deps : [ ] } ,
53+ { provide : Location , useClass : SpyLocation , deps : [ ] } ,
5254 { provide : DOCUMENT , useClass : MockDocument , deps : [ ] } ,
5355 { provide : PlatformLocation , useClass : MockPlatformLocation , deps : [ ] } ,
5456 { provide : ViewportScroller , useValue : viewportScrollerStub } ,
55- { provide : LocationStrategy , useClass : MockLocationStrategy , deps : [ ] }
57+ { provide : LocationStrategy , useClass : MockLocationStrategy , deps : [ ] } ,
58+ { provide : SessionStorage , useValue : new NoopStorage ( ) } ,
5659 ]
5760 } ) ;
5861
5962 platformLocation = injector . get ( PlatformLocation ) ;
6063 document = injector . get ( DOCUMENT ) as unknown as MockDocument ;
6164 scrollService = injector . get ( ScrollService ) ;
6265 location = injector . get ( Location ) as unknown as SpyLocation ;
66+ sessionStorage = injector . get ( SessionStorage ) ;
6367
6468 spyOn ( window , 'scrollBy' ) ;
6569 } ) ;
6670
6771 afterEach ( ( ) => {
6872 scrollServiceInstances . forEach ( instance => instance . ngOnDestroy ( ) ) ;
69- window . sessionStorage . clear ( ) ;
7073 } ) ;
7174
7275 it ( 'should debounce `updateScrollPositonInHistory()`' , fakeAsync ( ( ) => {
@@ -92,7 +95,8 @@ describe('ScrollService', () => {
9295 configurable : true ,
9396 } ) ;
9497 scrollService = createScrollService (
95- document , platformLocation as PlatformLocation , viewportScrollerStub , location ) ;
98+ document , platformLocation as PlatformLocation , viewportScrollerStub , location ,
99+ sessionStorage ) ;
96100
97101 expect ( scrollService . supportManualScrollRestoration ) . toBe ( false ) ;
98102 } finally {
@@ -112,32 +116,6 @@ describe('ScrollService', () => {
112116 }
113117 } ) ;
114118
115- it ( 'should not break when cookies are disabled in the browser' , ( ) => {
116- expect ( ( ) => {
117- const originalSessionStorage = Object . getOwnPropertyDescriptor ( window , 'sessionStorage' ) as PropertyDescriptor ;
118-
119- try {
120- // Simulate `window.sessionStorage` being inaccessible, when cookies are disabled.
121- Object . defineProperty ( window , 'sessionStorage' , {
122- get ( ) {
123- throw new Error ( 'The operation is insecure' ) ;
124- } ,
125- } ) ;
126-
127- const platformLoc = platformLocation as PlatformLocation ;
128- const service = createScrollService ( document , platformLoc , viewportScrollerStub , location ) ;
129-
130- service . updateScrollLocationHref ( ) ;
131- expect ( service . getStoredScrollLocationHref ( ) ) . toBeNull ( ) ;
132-
133- service . removeStoredScrollInfo ( ) ;
134- expect ( service . getStoredScrollPosition ( ) ) . toBeNull ( ) ;
135- } finally {
136- Object . defineProperty ( window , 'sessionStorage' , originalSessionStorage ) ;
137- }
138- } ) . not . toThrow ( ) ;
139- } ) ;
140-
141119 describe ( '#topOffset' , ( ) => {
142120 it ( 'should query for the top-bar by CSS selector' , ( ) => {
143121 expect ( document . querySelector ) . not . toHaveBeenCalled ( ) ;
0 commit comments