Skip to content

Instantly share code, notes, and snippets.

@khanghoang
Created April 21, 2017 16:30
Show Gist options
  • Save khanghoang/9d8d47859c35031fddde9acb6ddac4cf to your computer and use it in GitHub Desktop.
Save khanghoang/9d8d47859c35031fddde9acb6ddac4cf to your computer and use it in GitHub Desktop.
Mock Fetch Action
import { ACTIONS } from 'redux-api-call';
export default (makeFetchAction, data) => (name, apiFn) => {
const {
dataSelector,
actionCreator,
isFetchingSelector,
errorSelector,
updater,
} = makeFetchAction(name, apiFn);
const mockActionsCreator = () => {
const symbol = Object.getOwnPropertySymbols(actionCreator())[0];
const apiAction = actionCreator()[symbol];
return [
{
type: ACTIONS.START,
payload: {
...apiAction,
requestedAt: (new Date()).getTime(),
},
},
{
type: ACTIONS.COMPLETE,
payload: {
...apiAction,
respondedAt: (new Date()).getTime(),
json: data,
},
},
];
};
return {
dataSelector,
actionCreator: mockActionsCreator,
isFetchingSelector,
errorSelector,
updater,
};
};
// Test
it('dispatchs complete action with mock data', async () => {
const store = createMockStore({});
const response = { foo: 'bar' };
const mockMakeFetchAction = mockFetch(makeFetchAction, response)('foo', () => ({
endpoint: 'http://foo.bar',
}));
store.dispatch(mockMakeFetchAction.actionCreator());
const completeAction = store.getActions().find(a => a.type === ACTIONS.COMPLETE);
expect(completeAction.payload.json).toEqual(response);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment