@@ -12,7 +12,6 @@ import {
1212 normalizeRouteRecord ,
1313} from '../src/experimental'
1414import { nextNavigation } from './utils'
15- import { scrollPositions } from '../src/scrollBehavior'
1615
1716const routes = [ '/' , '/b' , '/c' ] . map ( path => ( { path, component : { } } ) )
1817const resolver = createFixedResolver (
@@ -45,7 +44,7 @@ describe.each([
4544 let top = 0
4645
4746 beforeEach ( ( ) => {
48- scrollPositions . clear ( )
47+ sessionStorage . clear ( )
4948 window . history . replaceState ( null , '' , '/' )
5049 top = 0
5150 vi . spyOn ( window , 'scrollX' , 'get' ) . mockReturnValue ( 0 )
@@ -59,7 +58,7 @@ describe.each([
5958
6059 afterEach ( ( ) => {
6160 history ?. destroy ( )
62- scrollPositions . clear ( )
61+ sessionStorage . clear ( )
6362 vi . restoreAllMocks ( )
6463 } )
6564
@@ -69,7 +68,8 @@ describe.each([
6968 ( _to , _from , saved ) => saved || { left : 0 , top : 0 }
7069 )
7170 const router = create ( history , scrollBehavior )
72- await router . push ( '/' )
71+ // the current url, to support reloads
72+ await router . push ( history . location )
7373 await nextTick ( )
7474
7575 async function push ( path : string ) {
@@ -87,6 +87,47 @@ describe.each([
8787 return { router, scrollBehavior, push, go }
8888 }
8989
90+ it ( 'restores the position saved on pagehide after a reload' , async ( ) => {
91+ const { push } = await setup ( )
92+ await push ( '/b' )
93+ top = 1000
94+ const stateBefore = window . history . state
95+ const replaceState = vi . spyOn ( window . history , 'replaceState' )
96+ window . dispatchEvent ( new Event ( 'pagehide' ) )
97+ // replaceState() during pagehide can break the bfcache
98+ expect ( replaceState ) . not . toHaveBeenCalled ( )
99+ expect ( window . history . state ) . toEqual ( stateBefore )
100+ replaceState . mockRestore ( )
101+
102+ // reload: a new document keeps history.state and sessionStorage
103+ history . destroy ( )
104+ top = 0
105+ const { scrollBehavior } = await setup ( )
106+
107+ expect ( scrollBehavior . mock . lastCall ?. [ 2 ] ) . toEqual ( { left : 0 , top : 1000 } )
108+ expect ( top ) . toBe ( 1000 )
109+ } )
110+
111+ it ( 'restores the position saved on push in a new document' , async ( ) => {
112+ const { push } = await setup ( )
113+ await push ( '/b' )
114+ top = 1000
115+ await push ( '/c' )
116+
117+ // a new document at the /b entry, e.g. after leaving the app
118+ history . destroy ( )
119+ const popped = new Promise ( resolve =>
120+ window . addEventListener ( 'popstate' , resolve , { once : true } )
121+ )
122+ window . history . back ( )
123+ await popped
124+ top = 0
125+ const { scrollBehavior } = await setup ( )
126+
127+ expect ( scrollBehavior . mock . lastCall ?. [ 2 ] ) . toEqual ( { left : 0 , top : 1000 } )
128+ expect ( top ) . toBe ( 1000 )
129+ } )
130+
90131 it ( 'restores the new entry after replacing the forward branch' , async ( ) => {
91132 const { scrollBehavior, push, go } = await setup ( )
92133 await push ( '/b' )
@@ -97,7 +138,6 @@ describe.each([
97138 await push ( '/' )
98139 await go ( - 1 )
99140
100- expect ( window . history . state . scroll . top ) . toBe ( 2000 )
101141 expect ( scrollBehavior . mock . lastCall ?. [ 2 ] ) . toEqual ( { left : 0 , top : 2000 } )
102142 expect ( top ) . toBe ( 2000 )
103143 } )
@@ -138,7 +178,6 @@ describe.each([
138178 if ( operation === 'push' ) await go ( - 1 )
139179 await go ( - 1 )
140180
141- expect ( window . history . state . scroll . top ) . toBe ( 1000 )
142181 expect ( scrollBehavior . mock . lastCall ?. [ 2 ] ) . toEqual ( {
143182 left : 0 ,
144183 top : 2000 ,
@@ -179,7 +218,6 @@ describe.each([
179218 await push ( '/c' )
180219 await go ( - 1 )
181220
182- expect ( window . history . state . scroll . top ) . toBe ( 2000 )
183221 expect ( scrollBehavior . mock . lastCall ?. [ 2 ] ) . toEqual ( { left : 0 , top : 2000 } )
184222 expect ( top ) . toBe ( 2000 )
185223 } )
0 commit comments