Skip to content
This repository was archived by the owner on May 17, 2019. It is now read-only.

Commit 82d878b

Browse files
akre54fusion-bot[bot]
authored andcommitted
Pass own props to mapStateToProps
#190 Co-authored-by: Adam Krebs <amk528@cs.nyu.edu>
1 parent cb55a06 commit 82d878b

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

src/__tests__/index.browser.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,83 @@ test('browser plugin integration test withRPCRedux', async t => {
106106
t.end();
107107
});
108108

109+
test('browser plugin integration test withRPCRedux and options', async t => {
110+
setup();
111+
const fetch = (url, options) => {
112+
if (!options || !options.body || typeof options.body !== 'string') {
113+
throw new Error(`Expected a string from options.body`);
114+
}
115+
const body: string = options.body;
116+
117+
t.equal(url, '/api/test', 'fetches to expected url');
118+
t.deepLooseEqual(
119+
JSON.parse(body),
120+
{arg: 1, state: 2, prop: 3},
121+
'sends correct body'
122+
);
123+
t.equal(options.method, 'POST', 'makes POST request');
124+
return Promise.resolve(
125+
new Response(
126+
JSON.stringify({
127+
status: 'success',
128+
data: {
129+
a: 'b',
130+
},
131+
})
132+
)
133+
);
134+
};
135+
136+
const expectedActions = [
137+
{type: initActionPattern},
138+
{type: /TEST_START/, payload: {arg: 1, state: 2, prop: 3}},
139+
{type: /TEST_SUCCESS/, payload: {a: 'b'}},
140+
];
141+
const store = createStore(
142+
(state, action) => {
143+
const fixture = expectedActions.shift();
144+
t.ok(fixture.type.test(action.type), 'dispatches expected action type');
145+
t.deepLooseEqual(
146+
action.payload,
147+
// $FlowFixMe
148+
fixture.payload,
149+
'dispatches expected action payload'
150+
);
151+
return {...state, ...action.payload};
152+
},
153+
{state: 2}
154+
);
155+
156+
const Component = props => {
157+
t.equal(typeof props.test, 'function', 'passes correct prop to component');
158+
return React.createElement('span', null, 'hello world');
159+
};
160+
161+
const mapStateToParams = (state, args, props) => {
162+
return {...state, ...args, ...props};
163+
};
164+
165+
const withTest = compose(
166+
withRPCRedux('test', {mapStateToParams}),
167+
connect(s => s),
168+
prepared(props => (props.a ? Promise.resolve() : props.test({arg: 1})))
169+
)(Component);
170+
171+
const element = React.createElement(
172+
Provider,
173+
{store},
174+
React.createElement(withTest, {prop: 3})
175+
);
176+
const app = new App(element);
177+
app.register(Plugin);
178+
app.register(FetchToken, fetch);
179+
await getSimulator(app).render('/');
180+
t.equal(expectedActions.length, 0, 'dispatches all actions');
181+
182+
teardown();
183+
t.end();
184+
});
185+
109186
test('browser plugin integration test withRPCRedux - failure', async t => {
110187
setup();
111188
const fetch = (url, options) => {

src/hoc.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const withRPCReactor = (
2626
}: {
2727
propName?: string,
2828
transformParams?: (params: any) => any,
29-
mapStateToParams?: (state: any) => any,
29+
mapStateToParams?: (state: any, args?: any, ownProps?: any) => any,
3030
} = {}
3131
) => {
3232
return withRPCRedux(rpcId, {
@@ -49,7 +49,7 @@ export function withRPCRedux(
4949
propName?: string,
5050
actions?: any,
5151
transformParams?: (params: any) => any,
52-
mapStateToParams?: (state: any) => any,
52+
mapStateToParams?: (state: any, args?: any, ownProps?: any) => any,
5353
} = {}
5454
): (React.ComponentType<*>) => React.ComponentType<*> {
5555
if (!propName) {
@@ -59,6 +59,10 @@ export function withRPCRedux(
5959
class withRPCRedux extends React.Component<*, *> {
6060
render() {
6161
const {rpc, store} = this.context;
62+
if (mapStateToParams) {
63+
const mapState = mapStateToParams;
64+
mapStateToParams = (state, args) => mapState(state, args, this.props);
65+
}
6266
const handler = createRPCHandler({
6367
rpcId,
6468
rpc,

0 commit comments

Comments
 (0)