Skip to content

Commit

Permalink
fix index routes inside async pathless routes (#4147)
Browse files Browse the repository at this point in the history
  • Loading branch information
agundermann authored and timdorr committed Nov 7, 2016
1 parent 70a4272 commit cbd1a95
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
26 changes: 25 additions & 1 deletion modules/__tests__/matchRoutes-test.js
Expand Up @@ -12,7 +12,8 @@ describe('matchRoutes', function () {
let
RootRoute, UsersRoute, UsersIndexRoute, UserRoute, PostRoute, FilesRoute,
AboutRoute, TeamRoute, ProfileRoute, GreedyRoute, OptionalRoute,
OptionalRouteChild, CatchAllRoute
OptionalRouteChild, CatchAllRoute, ContactRoute, ContactContainerRoute,
ContactIndexRoute
let createLocation = createMemoryHistory().createLocation

beforeEach(function () {
Expand All @@ -30,6 +31,11 @@ describe('matchRoutes', function () {
<Route path="/(optional)">
<Route path="child" />
</Route>
<Route path="/contact">
<Route>
<IndexRoute />
</Route>
</Route>
<Route path="*" />
*/
routes = [
Expand Down Expand Up @@ -74,6 +80,14 @@ describe('matchRoutes', function () {
}
]
},
ContactRoute = {
path: '/contact',
childRoutes: [
ContactContainerRoute = {
indexRoute: (ContactIndexRoute = {})
}
]
},
CatchAllRoute = {
path: '*'
}
Expand All @@ -91,6 +105,16 @@ describe('matchRoutes', function () {
})
})

describe('when the location matches an index route inside a pathless route', function () {
it('matches the correct routes', function (done) {
matchRoutes(routes, createLocation('/contact'), function (error, match) {
expect(match).toExist()
expect(match.routes).toEqual([ ContactRoute, ContactContainerRoute, ContactIndexRoute ])
done()
})
})
})

describe('when the location matches a nested route with params', function () {
it('matches the correct routes and params', function (done) {
matchRoutes(routes, createLocation('/users/5'), function (error, match) {
Expand Down
50 changes: 32 additions & 18 deletions modules/matchRoutes.js
Expand Up @@ -59,26 +59,40 @@ function getIndexRoute(route, location, paramNames, paramValues, callback) {
indexRoute => callback(null, createRoutes(indexRoute)[0]),
callback
)
} else if (route.childRoutes) {
const pathless = route.childRoutes.filter(childRoute => !childRoute.path)
} else if (route.childRoutes || route.getChildRoutes) {
const onChildRoutes = (error, childRoutes) => {
if (error) {
callback(error)
return
}

loopAsync(pathless.length, function (index, next, done) {
getIndexRoute(
pathless[index], location, paramNames, paramValues,
function (error, indexRoute) {
if (error || indexRoute) {
const routes = [ pathless[index] ].concat(
Array.isArray(indexRoute) ? indexRoute : [ indexRoute ]
)
done(error, routes)
} else {
next()
const pathless = childRoutes.filter(childRoute => !childRoute.path)

loopAsync(pathless.length, function (index, next, done) {
getIndexRoute(
pathless[index], location, paramNames, paramValues,
function (error, indexRoute) {
if (error || indexRoute) {
const routes = [ pathless[index] ].concat(
Array.isArray(indexRoute) ? indexRoute : [ indexRoute ]
)
done(error, routes)
} else {
next()
}
}
}
)
}, function (err, routes) {
callback(null, routes)
})
)
}, function (err, routes) {
callback(null, routes)
})
}

const result = getChildRoutes(
route, location, paramNames, paramValues, onChildRoutes
)
if (result) {
onChildRoutes(...result)
}
} else {
callback()
}
Expand Down

0 comments on commit cbd1a95

Please sign in to comment.