Created
April 21, 2017 16:30
-
-
Save khanghoang/9d8d47859c35031fddde9acb6ddac4cf to your computer and use it in GitHub Desktop.
Mock Fetch Action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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