Skip to content

Commit

Permalink
fix($notFound): refine path not found information
Browse files Browse the repository at this point in the history
  • Loading branch information
faceyspacey committed Jun 26, 2017
1 parent a30eb58 commit 0cf28ac
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 30 deletions.
8 changes: 4 additions & 4 deletions __tests__/__snapshots__/connectRoutes.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,16 @@ Object {
"meta": Object {
"location": Object {
"current": Object {
"pathname": "/",
"pathname": "/not-found",
"payload": Object {},
"type": "@@redux-first-router/NOT_FOUND",
},
"history": undefined,
"kind": undefined,
"kind": "load",
"prev": Object {
"pathname": "",
"pathname": "/first",
"payload": Object {},
"type": "",
"type": "FIRST",
},
},
},
Expand Down
20 changes: 2 additions & 18 deletions __tests__/connectRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,35 +100,19 @@ describe('middleware', () => {
})

it('user dispatches NOT_FOUND and middleware adds missing info to action', () => {
const { middleware, reducer } = setup()
const { middleware, reducer, enhancer } = setup('/first')
const middlewares = applyMiddleware(middleware)

const rootReducer = (state = {}, action = {}) => ({
location: reducer(state.location, action),
title: action.type
})

const store = createStore(rootReducer, middlewares)
const store = createStore(rootReducer, middlewares, enhancer)
const action = store.dispatch({ type: NOT_FOUND }) /*? $.meta */

store.getState() /*? $.location */

expect(action).toMatchObject({
type: '@@redux-first-router/NOT_FOUND',
payload: {},
meta: {
location: {
current: {
pathname: '/',
type: '@@redux-first-router/NOT_FOUND',
payload: {}
},
prev: { pathname: '', type: '', payload: {} },
kind: undefined
}
}
})

expect(action).toMatchSnapshot()
})

Expand Down
20 changes: 14 additions & 6 deletions src/connectRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
RoutesMap,
Route,
Options,
Action,
ActionMetaLocation,
ReceivedAction,
Location,
Expand Down Expand Up @@ -203,15 +204,20 @@ export default (
const { location } = store.getState()
const { payload } = action

const notFoundPath = action.meta && action.meta.notFoundPath
const meta = action.meta
const prevPath = location.pathname
const pathname = notFoundPath || prevPath
const pathname = (meta && meta.notFoundPath) || notFoundPath || prevPath
const kind =
(meta && meta.location && meta.location.kind) || // possibly kind === 'redirect'
(location.kind === 'load' && location.kind) ||
'push'

action = nestAction(
pathname,
{ type: NOT_FOUND, payload },
prevLocation,
location.history
history,
kind
)
}
else if (route && !isLocationAction(action)) {
Expand All @@ -234,7 +240,7 @@ export default (

if ((route || action.type === NOT_FOUND) && action.meta) {
// satisify flow with `action.meta` check
_beforeRouteChange(store, next, history, action.meta.location)
_beforeRouteChange(store, next, history, action)
}

const nextAction = next(action) // DISPATCH
Expand All @@ -250,11 +256,13 @@ export default (
store: Store,
next: Next,
history: History,
location: ActionMetaLocation
action: Action
) => {
const location = action.meta.location

if (onBeforeChange) {
const dispatch = middleware(store)(next) // re-create middleware's position in chain
onBeforeChange(dispatch, store.getState)
onBeforeChange(dispatch, store.getState, action)
}

prevState = store.getState()[locationKey]
Expand Down
6 changes: 5 additions & 1 deletion src/flow-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export type Options = {
location?: string,
notFoundPath?: string,
scrollTop?: boolean,
onBeforeChange?: (dispatch: Dispatch, getState: GetState) => void,
onBeforeChange?: (
dispatch: Dispatch,
getState: GetState,
action: Action
) => void,
onAfterChange?: (dispatch: Dispatch, getState: GetState) => void,
onBackNext?: (dispatch: Dispatch, getState: GetState) => void,
restoreScroll?: History => ScrollBehavior,
Expand Down
2 changes: 1 addition & 1 deletion src/pure-utils/nestAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default (
pathname: string,
receivedAction: ReceivedAction,
prev: Location,
history: History,
history: History, // not used currently, but be brought back
kind: ?string
): Action => {
const { type, payload = {}, meta } = receivedAction
Expand Down

0 comments on commit 0cf28ac

Please sign in to comment.