Skip to content

Commit

Permalink
Merge pull request #37 from marcins/36-fix-interop-with-devtools
Browse files Browse the repository at this point in the history
change method for detemrining whether state is already wrapped in reducer

Fixes #36
  • Loading branch information
marcins authored Aug 2, 2017
2 parents d8b0fc3 + 58331db commit 68f6ab2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
16 changes: 16 additions & 0 deletions __tests__/index-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ensureState
} from '../src/index';
import {createStore, combineReducers} from 'redux';
import instrument from 'redux-devtools-instrument';

// while there isn't an Immutable dependency in the library anymore we want to verify backwards
// compat with immutable wrapped reducers
Expand Down Expand Up @@ -435,3 +436,18 @@ test('with redux and initialState without preloadState', t => {
t.fail(error.message)
}
});

test('works with preloadState and redux-devtools-instrumentation', t => {
const enhancedReducer = combineReducers({
counter: optimistic(counterReducer)
});
try {
const store = createStore(enhancedReducer, {
counter: 1
}, instrument((state, action) => state));
store.dispatch({type: 'INC'});
t.is(ensureState(store.getState().counter), 2);
} catch (error) {
t.fail(error.message)
}
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"immutable": "^3.8.1",
"nyc": "^6.6.1",
"redux": "^3.6.0",
"redux-devtools-instrument": "^1.8.2",
"rimraf": "^2.5.3"
},
"nyc": {
Expand Down
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ export const BEGIN = '@@optimist/BEGIN';
export const COMMIT = '@@optimist/COMMIT';
export const REVERT = '@@optimist/REVERT';

export const ensureState = state => {
if (state && Array.isArray(state.history)) {
return state.current;
}
return state;
};
const isOptimistState = state => state && Object.keys(state).length === 3
&& state.hasOwnProperty('beforeState')
&& state.hasOwnProperty('history')
&& state.hasOwnProperty('current');

export const ensureState = state => isOptimistState(state) ? state.current : state;

const createState = state => ({
beforeState: undefined,
Expand Down Expand Up @@ -98,7 +98,7 @@ export const optimistic = (reducer, rawConfig = {}) => {
}, rawConfig);

return (state, action) => {
if (state === undefined || action.type === '@@redux/INIT') {
if (state === undefined || !isOptimistState(state)) {
state = createState(reducer(ensureState(state), {}));
}
const historySize = state.history.length;
Expand Down

0 comments on commit 68f6ab2

Please sign in to comment.