Skip to content

Commit

Permalink
State getter should return state when it is nested in an immutable ob…
Browse files Browse the repository at this point in the history
…ject (#153)

State getter returns state when it is nested in an immutable object and has immutable state, update docs to reflect usage.
  • Loading branch information
florisvink authored and bencripps committed Apr 11, 2017
1 parent 25a7510 commit 9df45af
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/USING_GRID_REDUCERS.md
Expand Up @@ -109,15 +109,15 @@ If you want to hide grid reducers from the root of your state:

````js
import { combineReducers } from 'redux';
import { GridRootReducer } from 'react-redux-grid';
import { Reducers as rootReducer } from 'react-redux-grid';

import myAppReducer from './customReducers/app';
import myDataReducer from './customReducers/data';

export const rootReducer = combineReducers({
myAppReducer,
myDataReducer,
nested: GridRootReducer
nested: rootReducer
});

export default rootReducer;
Expand Down
6 changes: 5 additions & 1 deletion src/util/stateGetter.js
Expand Up @@ -12,7 +12,11 @@ export const stateGetter = (state, props, key, entry) => {

if (props && props.reducerKeys) {
if (typeof props.reducerKeys === 'string') {
return get(state[props.reducerKeys], key, entry);
const nestedInImmutable = typeof state.get === 'function';
const nestedState = nestedInImmutable
? state.get(props.reducerKeys)
: state[props.reducerKeys];
return get(nestedState , key, entry);
}
else if (typeof props.reducerKeys === 'object'
&& Object.keys(props.reducerKeys).length > 0
Expand Down
16 changes: 16 additions & 0 deletions test/util/stateGetter.test.js
Expand Up @@ -106,6 +106,22 @@ describe('State Getter Function', () => {
}));
});

it(['Should return state when it is nested in an immutable object ',
'and has immutable state'].join(''), () => {
const state = Map({
nested: { filterState: { get: getStateWithImmutable } }
});

const props = {
reducerKeys: 'nested'
};
expect(
stateGetter(state, props, 'filterState', 'someProp')
).toEqual(Map({
x: 1
}));
});

it('Should return null if state is nested and not registered', () => {
const state = { nested: {} };
const props = {
Expand Down

0 comments on commit 9df45af

Please sign in to comment.