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

Commit b3b753a

Browse files
committed
add test and cleanup mapState implementation
1 parent 6008042 commit b3b753a

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/__tests__/index.browser.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,78 @@ 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(JSON.parse(body), {arg: 1, state: 2, prop: 3}, 'sends correct body');
119+
t.equal(options.method, 'POST', 'makes POST request');
120+
return Promise.resolve(
121+
new Response(
122+
JSON.stringify({
123+
status: 'success',
124+
data: {
125+
a: 'b',
126+
},
127+
})
128+
)
129+
);
130+
};
131+
132+
const expectedActions = [
133+
{type: initActionPattern},
134+
{type: /TEST_START/, payload: {arg: 1, state: 2, prop: 3}},
135+
{type: /TEST_SUCCESS/, payload: {a: 'b'}},
136+
];
137+
const store = createStore((state, action) => {
138+
const fixture = expectedActions.shift();
139+
t.ok(fixture.type.test(action.type), 'dispatches expected action type');
140+
t.deepLooseEqual(
141+
action.payload,
142+
// $FlowFixMe
143+
fixture.payload,
144+
'dispatches expected action payload'
145+
);
146+
return {...state, ...action.payload};
147+
}, {state: 2});
148+
149+
const Component = props => {
150+
t.equal(typeof props.test, 'function', 'passes correct prop to component');
151+
return React.createElement('span', null, 'hello world');
152+
};
153+
154+
const mapStateToParams = (state, args, props) => {
155+
return {...state, ...args, ...props};
156+
};
157+
158+
const withTest = compose(
159+
withRPCRedux('test', {mapStateToParams}),
160+
connect(s => s),
161+
prepared(props =>
162+
props.a ? Promise.resolve() : props.test({arg: 1})
163+
)
164+
)(Component);
165+
166+
const element = React.createElement(
167+
Provider,
168+
{store},
169+
React.createElement(withTest, {prop: 3})
170+
);
171+
const app = new App(element);
172+
app.register(Plugin);
173+
app.register(FetchToken, fetch);
174+
await getSimulator(app).render('/');
175+
t.equal(expectedActions.length, 0, 'dispatches all actions');
176+
177+
teardown();
178+
t.end();
179+
});
180+
109181
test('browser plugin integration test withRPCRedux - failure', async t => {
110182
setup();
111183
const fetch = (url, options) => {

src/hoc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,16 @@ 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,
6569
store,
6670
actions,
67-
mapStateToParams: (state, args) => mapStateToParams(state, args, this.props),
71+
mapStateToParams,
6872
transformParams,
6973
});
7074
const props = {

0 commit comments

Comments
 (0)