Skip to content

Commit 4e85471

Browse files
committed
testing POST with custom endpiont
1 parent 9fde55d commit 4e85471

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/__tests__/createAPIAction-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,5 +240,27 @@ describe('createAPIAction()', () => {
240240
});
241241
});
242242

243+
it('test POST with Custom Endpoint', () => {
244+
const customEndpoint = (params) => {
245+
return `/user/${params.id}/ronald/${params.name}`;
246+
};
247+
const createItem = createAPIAction(type, 'POST', customEndpoint);
248+
const payload = { id: 10, name: 'james' };
249+
expect(createItem(payload)).to.deep.equal({
250+
type,
251+
payload: payload,
252+
meta: {
253+
api: true,
254+
method: 'POST',
255+
endpoint: '/user/10/ronald/james',
256+
types: [
257+
type.concat('_POST_REQUEST'),
258+
type.concat('_POST_SUCCESS'),
259+
type.concat('_POST_FAILURE')
260+
]
261+
}
262+
});
263+
});
264+
243265
});
244266
});

src/createAPIAction.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,19 @@ function getEndpoint(method, endpoint, params) {
2929
*
3030
* In an error case, we just return the error.
3131
**/
32-
function getPayload(method, itemIDorPayload, data) {
33-
if (itemIDorPayload instanceof Error) {
34-
return itemIDorPayload;
32+
function getPayload(method, params) {
33+
34+
const [firstParam, ...others] = params;
35+
36+
if (firstParam instanceof Error) {
37+
return firstParam;
3538
}
3639

3740
switch (method) {
3841
case 'POST':
39-
return itemIDorPayload;
42+
return firstParam;
4043
case 'PUT':
41-
return data;
44+
return others[0];
4245
default:
4346
return {};
4447
}
@@ -53,7 +56,7 @@ export default function createAPIAction(type, method, endpoint, actionCreator, m
5356

5457
const action = {
5558
type,
56-
payload: getPayload(method, firstParam, ...others)
59+
payload: getPayload(method, params)
5760
};
5861

5962
if (action.payload instanceof Error) {

0 commit comments

Comments
 (0)