因此,我有一个动作,如果给定参数,则会在请求标头中添加“ Content-Type”。如何测试是否用jest-fetch-mock添加了“ Content-Type”?标头似乎无关紧要。
// action
export const someAction = (isFoo) => dispatch => {
const headers = new Headers();
if (isFoo) {
headers.append('Content-Type', 'application/json');
}
const request = new Request(`url/api/someEndpoint`, {
method: 'GET',
headers
});
return fetch(request).then(
response => {
dispatch({ type: types.SOME_ACTION_TYPE_SUCCESS, response });
return response;
},
error => {
dispatch({ type: types.SOME_ACTION_TYPE_FAILURE, error });
return { error };
}
);
};
// test
it('should run action successfully and trigger SOME_ACTION_TYPE_SUCCESS', () => {
fetch.mockResponseOnce(JSON.stringify({foo: 'bar'}));
const expectedActions = [
{ type: types.SOME_ACTION_TYPE_SUCCESS, response: {foo: 'bar'} }
];
const store = mockStore({});
return store.dispatch(actions.someAction(true)).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});