Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////users-thunk-test.ts
- import {actions, follow, unfollow} from "./users-reducer";
- import {usersAPI} from "../api/users-api";
- import {IResponseAPI, ResultCodesEnum} from "../api/api";
- jest.mock("../api/users-api")
- const usersAPIMock = usersAPI as jest.Mocked<typeof usersAPI>
- const result: IResponseAPI = {
- resultCode: ResultCodesEnum.Success,
- messages: [],
- data: {}
- }
- usersAPIMock.follow.mockReturnValue(Promise.resolve(result))
- test('follow user', async () => {
- const dispatch = jest.fn();
- const getState = jest.fn();
- await follow(1)(dispatch, getState, {});
- expect(dispatch).toBeCalledWith(actions.toggleIsFollowingAC(true, 1))
- expect(dispatch).toBeCalledWith(actions.followSuccess(1)); ////////// Вот тут валится ошибка
- expect(dispatch).toBeCalledWith(actions.toggleIsFollowingAC(false, 1));
- })
- ///////// Функция, которую тестирую
- export const follow = (userId: number): ThunkActionType => {
- return async (dispatch) => {
- dispatch(actions.toggleIsFollowingAC(true, userId))
- const data = await usersAPI.follow(userId) //////// Вот тут получаю undefined
- try {
- if (data.resultCode === 0) {
- dispatch(actions.followSuccess(userId))
- }
- dispatch(actions.toggleIsFollowingAC(false, userId))
- } catch (error) {
- dispatch(actions.toggleIsFollowingAC(false, userId))
- }
- }
- }
- /// REQUEST TO API
- export const instanceAxios = axios.create({
- baseURL: 'https://social-network.samuraijs.com/api/1.0/',
- withCredentials: true,
- headers: {
- 'API-KEY': '91cc5a84-d625-478b-a406-09acdffa3140'
- }
- })
- export interface IResponseAPI {
- data: object,
- resultCode: ResultCodesEnum,
- messages: Array<string>
- }
- export const usersAPI = {
- follow(userId: number) {
- return instanceAxios.post<IResponseAPI>(`follow/${userId}`)
- .then(response => {
- return response.data
- })
- },
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement