Skip to content

Commit

Permalink
Make sure scrollRestoration is writable. Fixes #126
Browse files Browse the repository at this point in the history
  • Loading branch information
vizath committed Jan 16, 2018
1 parent 493c9b0 commit 0d212a9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export default class ScrollBehavior {
/* istanbul ignore else: Travis browsers all support this */
if ('scrollRestoration' in window.history) {
this._oldScrollRestoration = window.history.scrollRestoration;
window.history.scrollRestoration = 'manual';
try {
window.history.scrollRestoration = 'manual';
} catch () {
this._oldScrollRestoration = null;
}
} else {
this._oldScrollRestoration = null;
}
Expand Down
23 changes: 23 additions & 0 deletions test/ScrollBehavior.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ describe('ScrollBehavior', () => {
},
]);
});

it('should not crash when history is not available', (done) => {
Object.defineProperty(window.history, 'scrollRestoration', {
value: 'auto',
// See https://github.com/taion/scroll-behavior/issues/126
writable: false,
enumerable: true,
configurable: true,
});

const history = withRoutes(withScroll(createHistory()));

unlisten = run(history, [
() => {
expect(scrollTop(window)).to.equal(0);

delete window.history.scrollRestoration;
window.history.scrollRestoration = 'auto';

done();
},
]);
});
});

describe('custom behavior', () => {
Expand Down

0 comments on commit 0d212a9

Please sign in to comment.