forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[changed] goBack returns a boolean value Users can use Router.History.length to determine if the router has any history. router.goBack() is a no-op when the router does not have any history and emits a warning. Fixes remix-run#408
- Loading branch information
Showing
8 changed files
with
129 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var invariant = require('react/lib/invariant'); | ||
var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM; | ||
|
||
var History = { | ||
|
||
/** | ||
* Sends the browser back one entry in the history, if one is available. | ||
*/ | ||
back: function () { | ||
invariant( | ||
canUseDOM, | ||
'Cannot use History.back without a DOM' | ||
); | ||
|
||
// Do this first so that History.length will | ||
// be accurate in location change listeners. | ||
History.length -= 1; | ||
|
||
window.history.back(); | ||
}, | ||
|
||
/** | ||
* The current number of entries in the history. | ||
*/ | ||
length: 1 | ||
|
||
}; | ||
|
||
module.exports = History; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
var expect = require('expect'); | ||
var React = require('react'); | ||
var { Foo, RedirectToFoo } = require('../../__tests__/TestHandlers'); | ||
var TestLocation = require('../../locations/TestLocation'); | ||
var Route = require('../../components/Route'); | ||
var Router = require('../../index'); | ||
var History = require('../History'); | ||
|
||
describe('History', function () { | ||
describe('on the initial page load', function () { | ||
it('has length 1', function () { | ||
expect(History.length).toEqual(1); | ||
}); | ||
}); | ||
|
||
describe('after navigating to a route', function () { | ||
beforeEach(function () { | ||
TestLocation.history = [ '/foo' ]; | ||
}); | ||
|
||
it('has length 2', function (done) { | ||
var routes = [ | ||
<Route name="foo" handler={Foo}/>, | ||
<Route name="about" handler={Foo}/> | ||
]; | ||
|
||
var count = 0; | ||
|
||
var router = Router.run(routes, TestLocation, function (Handler) { | ||
count += 1; | ||
|
||
if (count === 2) { | ||
expect(History.length).toEqual(2); | ||
done(); | ||
} | ||
}); | ||
|
||
router.transitionTo('about'); | ||
}); | ||
|
||
describe('that redirects to another route', function () { | ||
it('has length 2', function (done) { | ||
var routes = [ | ||
<Route name="foo" handler={Foo}/>, | ||
<Route name="about" handler={RedirectToFoo}/> | ||
]; | ||
|
||
var count = 0; | ||
|
||
var router = Router.run(routes, TestLocation, function (Handler) { | ||
count += 1; | ||
|
||
if (count === 2) { | ||
expect(History.length).toEqual(2); | ||
done(); | ||
} | ||
}); | ||
|
||
router.transitionTo('about'); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters