Skip to content

Commit f05e49d

Browse files
committed
Expose scroll behaviors, add test for ScrollToTopBehavior
1 parent d5da1df commit f05e49d

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

modules/__tests__/Router-test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var React = require('react');
44
var Route = require('../components/Route');
55
var RouteHandler = require('../components/RouteHandler');
66
var TestLocation = require('../locations/TestLocation');
7+
var ScrollToTopBehavior = require('../behaviors/ScrollToTopBehavior');
78
var getWindowScrollPosition = require('../utils/getWindowScrollPosition');
89
var Router = require('../index');
910

@@ -211,6 +212,75 @@ describe('Router.run', function () {
211212
});
212213
});
213214

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+
214284
describe('ImitateBrowserBehavior scrolling', function () {
215285
var BigPage = React.createClass({
216286
render: function () {

modules/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ exports.HashLocation = require('./locations/HashLocation');
99
exports.HistoryLocation = require('./locations/HistoryLocation');
1010
exports.RefreshLocation = require('./locations/RefreshLocation');
1111

12+
exports.ImitateBrowserBehavior = require('./behaviors/ImitateBrowserBehavior');
13+
exports.ScrollToTopBehavior = require('./behaviors/ScrollToTopBehavior');
14+
1215
exports.Navigation = require('./mixins/Navigation');
1316
exports.State = require('./mixins/State');
1417

0 commit comments

Comments
 (0)