Comparing version 2.0.3 to 2.0.4
@@ -47,7 +47,4 @@ import expect from 'expect'; | ||
describe('store enhancer', () => { | ||
const reducer = (state = {}, action) => { | ||
lastAction = action; | ||
return state; | ||
}; | ||
describe('store middleware', () => { | ||
const reducer = (state = [], action) => [action, ...state]; | ||
const routes = { | ||
@@ -58,6 +55,5 @@ '/posts/:id': 'VIEW_POST', | ||
}; | ||
let store, history, lastAction; | ||
let store, history; | ||
beforeEach(() => { | ||
lastAction = null; | ||
history = createHistory(); | ||
@@ -69,2 +65,3 @@ store = createStore(reducer, applyMiddleware(middleware(history)(routes))); | ||
history.push('/posts'); | ||
const [ lastAction ] = store.getState(); | ||
expect(lastAction).toEqual({ type: 'VIEW_POSTS' }); | ||
@@ -75,2 +72,3 @@ }); | ||
history.push('/posts/1'); | ||
const [ lastAction ] = store.getState(); | ||
expect(lastAction).toEqual({ type: 'VIEW_POST', id: '1' }); | ||
@@ -84,2 +82,11 @@ }); | ||
it('does not dispatch more than one action when history is updated', () => { | ||
const action = { type: 'VIEW_POST', id: 3 }; | ||
const previousState = store.getState(); | ||
const expectedState = [action, ...previousState]; | ||
store.dispatch(action); | ||
const currentState = store.getState(); | ||
expect(currentState).toEqual(expectedState); | ||
}); | ||
it('skips updating history when route is not found', () => { | ||
@@ -92,7 +99,8 @@ const lastPathname = history.location.pathname; | ||
it('does not dispatch action for unknown history location', () => { | ||
const previousAction = lastAction; | ||
const previousState = store.getState(); | ||
history.push('/unknown'); | ||
expect(lastAction).toEqual(previousAction); | ||
const currentState = store.getState(); | ||
expect(currentState).toEqual(previousState); | ||
}); | ||
}); | ||
}); |
@@ -55,10 +55,16 @@ import generatePath from './generatePath'; | ||
return ({ dispatch, getState }) => { | ||
history.listen(location => { | ||
const action = actionFor(location.pathname); | ||
let unlisten; | ||
if (action) { | ||
dispatch(action); | ||
} | ||
}); | ||
const listen = () => { | ||
unlisten = history.listen(location => { | ||
const action = actionFor(location.pathname); | ||
if (action) { | ||
dispatch(action); | ||
} | ||
}); | ||
}; | ||
listen(); | ||
return next => action => { | ||
@@ -68,3 +74,5 @@ const path = pathFor(action); | ||
if (path !== null && history.location.pathname !== path) { | ||
unlisten(); | ||
history[action.replace ? 'replace' : 'push'](path); | ||
listen(); | ||
} | ||
@@ -71,0 +79,0 @@ |
{ | ||
"name": "dhistory", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Dispatches actions on history state changes (ie. push state or navigate).", | ||
@@ -5,0 +5,0 @@ "readme": "README.md", |
29315
585