@@ -4,6 +4,7 @@ var React = require('react');
4
4
var Route = require ( '../components/Route' ) ;
5
5
var RouteHandler = require ( '../components/RouteHandler' ) ;
6
6
var TestLocation = require ( '../locations/TestLocation' ) ;
7
+ var ScrollToTopBehavior = require ( '../behaviors/ScrollToTopBehavior' ) ;
7
8
var getWindowScrollPosition = require ( '../utils/getWindowScrollPosition' ) ;
8
9
var Router = require ( '../index' ) ;
9
10
@@ -211,6 +212,75 @@ describe('Router.run', function () {
211
212
} ) ;
212
213
} ) ;
213
214
215
+ describe ( 'ScrollToTop scrolling' , function ( ) {
216
+ var BigPage = React . createClass ( {
217
+ render : function ( ) {
218
+ return < div style = { { width : 10000 , height : 10000 , background : 'green' } } /> ;
219
+ }
220
+ } ) ;
221
+
222
+ var routes = [
223
+ < Route name = "one" handler = { BigPage } /> ,
224
+ < Route name = "two" handler = { BigPage } />
225
+ ] ;
226
+
227
+ describe ( 'when a page is scrolled' , function ( ) {
228
+ var position , div , renderCount ;
229
+ beforeEach ( function ( done ) {
230
+ TestLocation . history = [ '/one' ] ;
231
+
232
+ div = document . createElement ( 'div' ) ;
233
+ document . body . appendChild ( div ) ;
234
+
235
+ renderCount = 0 ;
236
+
237
+ Router . create ( {
238
+ routes : routes ,
239
+ location : TestLocation ,
240
+ scrollBehavior : ScrollToTopBehavior
241
+ } ) . run ( function ( Handler ) {
242
+ React . render ( < Handler /> , div , function ( ) {
243
+ if ( renderCount === 0 ) {
244
+ position = { x : 20 , y : 50 } ;
245
+ window . scrollTo ( position . x , position . y ) ;
246
+
247
+ setTimeout ( function ( ) {
248
+ expect ( getWindowScrollPosition ( ) ) . toEqual ( position ) ;
249
+ done ( ) ;
250
+ } , 20 ) ;
251
+ }
252
+
253
+ renderCount += 1 ;
254
+ } ) ;
255
+ } ) ;
256
+ } ) ;
257
+
258
+ afterEach ( function ( ) {
259
+ div . parentNode . removeChild ( div ) ;
260
+ } ) ;
261
+
262
+ describe ( 'navigating to a new page' , function ( ) {
263
+ beforeEach ( function ( ) {
264
+ TestLocation . push ( '/two' ) ;
265
+ } ) ;
266
+
267
+ it ( 'resets the scroll position' , function ( ) {
268
+ expect ( getWindowScrollPosition ( ) ) . toEqual ( { x : 0 , y : 0 } ) ;
269
+ } ) ;
270
+
271
+ describe ( 'then returning to the previous page' , function ( ) {
272
+ beforeEach ( function ( ) {
273
+ TestLocation . pop ( ) ;
274
+ } ) ;
275
+
276
+ it ( 'resets the scroll position' , function ( ) {
277
+ expect ( getWindowScrollPosition ( ) ) . toEqual ( { x : 0 , y : 0 } ) ;
278
+ } ) ;
279
+ } ) ;
280
+ } ) ;
281
+ } ) ;
282
+ } ) ;
283
+
214
284
describe ( 'ImitateBrowserBehavior scrolling' , function ( ) {
215
285
var BigPage = React . createClass ( {
216
286
render : function ( ) {
0 commit comments