Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
feat(holocronModule): add mapStateToProps to holocron api (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew-Mallimo authored Mar 8, 2023
1 parent 8c7940f commit 80194db
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ exports[`holocronModule should pass mergeProps to connect if it is specified 1`]
</div>
`;

exports[`holocronModule should provide state configured by mapStateToProps when a reducer is NOT proveded 1`] = `
<div>
mapStateToPropsProp
</div>
`;

exports[`holocronModule should provide state configured by mapStateToProps when a reducer is proveded 1`] = `
<div>
mapStateToPropsProp
</div>
`;

exports[`holocronModule should provide the module state as a plain JS prop if a reducer is provided 1`] = `
<div>
value
Expand Down
30 changes: 30 additions & 0 deletions packages/holocron/__tests__/holocronModule.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,36 @@ describe('holocronModule', () => {
expect(tree).toMatchSnapshot();
});

it('should provide state configured by mapStateToProps when a reducer is proveded', () => {
const reducer = (state) => state;
const Module = holocronModule({
name: 'mock-module',
reducer,
mapStateToProps: () => ({ mapStateToPropsProp: 'mapStateToPropsProp' }),
})(({ mapStateToPropsProp }) => <div>{mapStateToPropsProp}</div>);
const mockStore = createStore(
(state) => state,
fromJS({ modules: { 'mock-module': { key: 'value' } } })
);
const component = renderer.create(<Module store={mockStore} />);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('should provide state configured by mapStateToProps when a reducer is NOT proveded', () => {
const Module = holocronModule({
name: 'mock-module',
mapStateToProps: () => ({ mapStateToPropsProp: 'mapStateToPropsProp' }),
})(({ mapStateToPropsProp }) => <div>{mapStateToPropsProp}</div>);
const mockStore = createStore(
(state) => state,
fromJS({ modules: { 'mock-module': { key: 'value' } } })
);
const component = renderer.create(<Module store={mockStore} />);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('should not rerender if module state has not changed', () => {
const renderSpy = jest.fn();
const moduleReducer = (state) => state || {};
Expand Down
1 change: 1 addition & 0 deletions packages/holocron/docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ A [higher order component (HOC)] for registering a load function and/or reducer
| `loadModuleData` | `({ store, fetchClient, ownProps, module }) => Promise` | `false` | A function that fetches data required by your module |
| `shouldModuleReload` | `(oldProps, newProps) => Boolean` | `false` | A function to determine if your `loadModuleData` and or `load` function should be called again |
| `mergeProps` | `(stateProps, dispatchProps, ownProps) => Object` | `false` | Passed down to Redux connect |
| `mapStateToProps` | `(state) => Object` | `false` | Passed down to Redux connect. This enables `shouldModuleReload` to have access to state. |
| `options` | `Object` | `false` | Additional options |
##### Usage
Expand Down
9 changes: 5 additions & 4 deletions packages/holocron/src/holocronModule.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export default function holocronModule({
shouldModuleReload,
loadModuleData,
mergeProps,
mapStateToProps = () => ({}),
options = {},
} = {}) {
return function wrapWithHolocron(WrappedComponent) {
Expand Down Expand Up @@ -152,7 +153,7 @@ export default function holocronModule({
HolocronModuleWrapper[LOAD_KEY] = load;
}

let mapModuleStateToProps;
let mapModuleStateToProps = mapStateToProps;

if (reducer && !name) {
console.warn(`The Holocron Config in '${getModuleDisplayName(getModuleName(WrappedComponent, name))}' requires a 'name' when passing a 'reducer'.\nThe 'reducer' will not be added to the Redux Store without a 'name'.`);
Expand All @@ -170,10 +171,10 @@ export default function holocronModule({
(moduleState) => moduleState.toJS()
);

mapModuleStateToProps = ((state) => {
mapModuleStateToProps = (state, ownProps) => {
const moduleState = getModuleState(state);
return { moduleState };
});
return { ...mapStateToProps(state, ownProps), moduleState };
};
}
}

Expand Down

0 comments on commit 80194db

Please sign in to comment.