Skip to content

Commit

Permalink
fix(Router): runInterceptors was wrongly skipping some paths
Browse files Browse the repository at this point in the history
  • Loading branch information
AoDev committed Jun 20, 2019
1 parent 387f0c5 commit a6ec32f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,15 @@ export default class Router {
*/
static runInterceptors (router, request) {
const {routes} = router
let updatedRequest = Router.copyRequest(request)
let requestCopy = Router.copyRequest(request)
let segments = splitPath(request.route)

while (segments.length > 0) {
const routeConfig = routes[segments[0]]
const currentSegment = segments[0]
const routeConfig = routes[currentSegment]

if (!routeConfig) {
return updatedRequest
return requestCopy
}

segments = segments.slice(1)
Expand All @@ -133,21 +134,21 @@ export default class Router {
/**
* Copy the request before passing it to the interceptor to avoid trouble when user modifies it.
*/
const interceptorReq = interceptor(router, Router.copyRequest(updatedRequest))
const interceptorReq = interceptor(router, Router.copyRequest(requestCopy))
if (interceptorReq.params) {
Object.assign(updatedRequest.params, interceptorReq.params)
Object.assign(requestCopy.params, interceptorReq.params)
}
/**
* In case of redirect, we look for the point of intersection and continue from there.
* If the old and new path have segments in common, we should not repeat the ones already done.
*/
if (typeof interceptorReq.route === 'string' && interceptorReq.route !== updatedRequest.route) {
segments = diffPaths(splitPath(updatedRequest.route), splitPath(interceptorReq.route))
updatedRequest.route = interceptorReq.route
if (typeof interceptorReq.route === 'string' && interceptorReq.route !== requestCopy.route) {
segments = diffPaths(splitPath(currentSegment), splitPath(interceptorReq.route))
requestCopy.route = interceptorReq.route
}
}
}
return updatedRequest
return requestCopy
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/Router.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,29 @@ describe.only('Router', () => {
})
})

it('should handle complex redirect situation', () => {
// This is a test case that models an actual router bug
// where runInterceptor was wrongly skipping some paths.
const routes = {
'/': {},
'/a': {},
'/a/b': {
intercept: jest.fn().mockImplementation(() => ({route: '/a/b/c/d', params: {}}))
},
'/a/b/c': {
intercept: jest.fn().mockImplementation((router, request) => request)
},
'/a/b/c/d': {
intercept: jest.fn().mockImplementation((router, request) => request)
},
}
router.set('routes', routes)
runInterceptors(router, {route: '/a/b/c'})
expect(routes['/a/b'].intercept).toHaveBeenCalled()
expect(routes['/a/b/c'].intercept).toHaveBeenCalled()
expect(routes['/a/b/c/d'].intercept).toHaveBeenCalled()
})

test.todo('must pass a COPY of the request to the user interceptor')
})

Expand Down

0 comments on commit a6ec32f

Please sign in to comment.