I am making a dispatch
call to an API as almost the first action of my component, and different things will render depending on the what is returned, as part of the usual Redux methodology.
I'm just not sure on how I can test this/how to fake a response such that the Action/Reducer will give me for example, the fail path of this API call.
Component
const mapStateToProps = ({apiResult}) => ({
apiResult: !!apiResult.apiResult,
loading: !!apiResult.loading
});
const mapDispatchToProps = dispatch => ({
queryApi: (data) => {
dispatch(queryApi(data));
}
});
const MyComponent = ({apiResult, loading, queryApi}) => {
if (loading) {
return <LoadingSpinner />;
}
return apiResult ? <h1>Api result good</h1> : <h1>Api result bad</h1>
}
export default connect(mapStateToProps, mapDispatchToProps)(MyComponent);
Action
import agent from '../agent';
export const QUERY_API = 'QUERY_API';
export const QUERY_API_SUCCESS = 'QUERY_API_SUCCESS';
export const QUERY_API_ERROR = 'QUERY_API_ERROR';
export const queryApi = (data) => {
return {
type: QUERY_API,
agent: agent.QueryApi.get,
agentData: data
}
};
Reducer
FYI: I know that I shouldn't be using the clone here, but that is not an issue for the time being.
import {
QUERY_API,
QUERY_API_SUCCESS,
QUERY_API_ERROR
} from '../actions/apiQuery';
export default (state = {}, action) => {
let newState = deepClone(state);
switch (action.type) {
case QUERY_API:
newState.apiResult = false;
newState.loading = true;
break;
case QUERY_API_SUCCESS:
newState.apiResult = true;
newState.loading = false;
break;
case QUERY_API_ERROR:
newState.apiResult = false;
newState.loading = false;
break;
}
return newState
}
Agent
const getFromApi: path => {
return superagent
.get(`my-base.url/${path}`)
.use(acceptJsonHeaders)
.then(responseBody)
.catch(errorHandler)
}
export const QueryApi = {
get: (data) => requests.getFromApi(`${data`)
}
How can I force my dispatch
seen in my Component to go the FAIL route?
Aucun commentaire:
Enregistrer un commentaire