From 0cf28ac064a56b7ffb91a4f8bca3da81728b37a3 Mon Sep 17 00:00:00 2001 From: James Gillmore Date: Mon, 26 Jun 2017 00:05:24 -0700 Subject: [PATCH] fix($notFound): refine path not found information --- __tests__/__snapshots__/connectRoutes.js.snap | 8 ++++---- __tests__/connectRoutes.js | 20 ++----------------- src/connectRoutes.js | 20 +++++++++++++------ src/flow-types.js | 6 +++++- src/pure-utils/nestAction.js | 2 +- 5 files changed, 26 insertions(+), 30 deletions(-) diff --git a/__tests__/__snapshots__/connectRoutes.js.snap b/__tests__/__snapshots__/connectRoutes.js.snap index 45c6a04a..4dc1632b 100644 --- a/__tests__/__snapshots__/connectRoutes.js.snap +++ b/__tests__/__snapshots__/connectRoutes.js.snap @@ -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", }, }, }, diff --git a/__tests__/connectRoutes.js b/__tests__/connectRoutes.js index fdea1504..75c01bff 100644 --- a/__tests__/connectRoutes.js +++ b/__tests__/connectRoutes.js @@ -100,7 +100,7 @@ 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 = {}) => ({ @@ -108,27 +108,11 @@ describe('middleware', () => { 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() }) diff --git a/src/connectRoutes.js b/src/connectRoutes.js index 2cfc3e74..c192f03f 100644 --- a/src/connectRoutes.js +++ b/src/connectRoutes.js @@ -23,6 +23,7 @@ import type { RoutesMap, Route, Options, + Action, ActionMetaLocation, ReceivedAction, Location, @@ -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)) { @@ -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 @@ -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] diff --git a/src/flow-types.js b/src/flow-types.js index faea3373..298f3b9b 100644 --- a/src/flow-types.js +++ b/src/flow-types.js @@ -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, diff --git a/src/pure-utils/nestAction.js b/src/pure-utils/nestAction.js index bb287926..ff1d997b 100644 --- a/src/pure-utils/nestAction.js +++ b/src/pure-utils/nestAction.js @@ -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