Skip to content

Commit

Permalink
Fix a bug when calling evaluateCurrent('/')
Browse files Browse the repository at this point in the history
When the current route was `/` and you would call `evaluateCurrent('/')`, nothing would happen.

Reported in TehShrike/abstract-state-router#116

Bug added in #08f93795
  • Loading branch information
TehShrike committed Nov 16, 2017
1 parent 02db623 commit 56c207f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function getPathParts(path) {
var chunks = path.split('?')
return {
path: chunks.shift(),
queryString: qs.parse(chunks.join(''))
queryString: qs.parse(chunks.join('')),
}
}

Expand Down Expand Up @@ -91,10 +91,11 @@ function add(routes, routeString, routeFunction) {
}

function evaluateCurrentPathOrGoToDefault(routes, hashLocation, reverse, onNotFound, defaultPath) {
const currentLocation = hashLocation.get()
if (currentLocation && currentLocation !== '/') {
var routesCopy = routes.slice()
var currentLocation = hashLocation.get()
var canUseCurrentLocation = currentLocation && (currentLocation !== '/' || defaultPath === '/')

if (canUseCurrentLocation) {
var routesCopy = routes.slice()
evaluateCurrentPath(routesCopy, hashLocation, reverse, onNotFound)
} else {
hashLocation.go(defaultPath)
Expand Down
26 changes: 26 additions & 0 deletions test/all-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,32 @@ module.exports = function tests(locationHash, delayAfterInitialRouteChange) {
t.timeoutAfter(4000)
})

test('evaluateCurrent fires the / route when the url is currently /', function(t) {
t.plan(1)

startTest(function(getRoute) {
var route = getRoute()

route.on('not found', function(path, parameters) {
t.fail('Should not fire the not found route')

route.stop()
t.end()
})

route.add('/', function(parameters) {
t.deepEqual(parameters, {})

route.stop()
t.end()
})

route.evaluateCurrent('/')
}, '/')

t.timeoutAfter(4000)
})

test('replacing a url', function(t) {
startTest(function(getRoute) {
var route = getRoute()
Expand Down

0 comments on commit 56c207f

Please sign in to comment.