我试图在失败时测试我的行为。调用 API 的操作。
我有这个:
export const loadTips = (tips: TipsModel): LoadTips => ({ tips, type: LOAD_TIPS });
export const fetchTips: ActionCreator<ThunkType> = () => async (dispatch) => {
return ApiService.getTips(
tips => dispatch(loadTips(tips)),
() => dispatch(triggerToast('Tips are not loading. Try again later!', true))
);
};
这些是我到目前为止正确通过的测试:
import * as actions from './tipsActions';
import { LOAD_TIPS, LoadTips, CLEAR_TIPS, ClearTips, DisableOrSnoozedTips, DISABLE_SNOOZ_TIP } from '../types/tipsTypes';
import getStore from '../services/mockGlobalStore';
import { mocked } from 'ts-jest/utils';
import ApiService from '../services/apiService';
jest.mock('../services/apiService');
const mockedApiService = mocked(ApiService, true);
describe('tips actions on API', () => {
beforeEach(() => {
mockedApiService.mockClear();
});
const store = getStore();
it('fetchTips makes API call', () => {
store.dispatch(actions.fetchTips());
expect(mockedApiService.getTips).toHaveBeenCalledWith(expect.any(Function), expect.any(Function));
});
});
describe('tips actions', () => {
it('creates a loadTips action', () => {
const expectedAction: LoadTips = { type: LOAD_TIPS, tips: dummyTips };
expect(actions.loadTips(dummyTips)).toEqual(expectedAction);
});
});
所以我想知道我可以做些什么来测试,例如当动作失败时它会分派动作:
dispatch(triggerToast('Tips are not loading. Try again later!', true))
那么我该如何测试那部分呢?
猛跑小猪
梵蒂冈之花
相关分类