Advertisement
Guest User

Untitled

a guest
Dec 26th, 2020
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////users-thunk-test.ts
  2.  
  3. import {actions, follow, unfollow} from "./users-reducer";
  4. import {usersAPI} from "../api/users-api";
  5. import {IResponseAPI, ResultCodesEnum} from "../api/api";
  6.  
  7. jest.mock("../api/users-api")
  8. const usersAPIMock = usersAPI as jest.Mocked<typeof usersAPI>
  9.  
  10. const result: IResponseAPI = {
  11.   resultCode: ResultCodesEnum.Success,
  12.   messages: [],
  13.   data: {}
  14. }
  15. usersAPIMock.follow.mockReturnValue(Promise.resolve(result))
  16.  
  17. test('follow user', async () => {
  18.  
  19.   const dispatch = jest.fn();
  20.   const getState = jest.fn();
  21.  
  22.   await follow(1)(dispatch, getState, {});
  23.  
  24.   expect(dispatch).toBeCalledWith(actions.toggleIsFollowingAC(true, 1))
  25.   expect(dispatch).toBeCalledWith(actions.followSuccess(1)); ////////// Вот тут валится ошибка
  26.   expect(dispatch).toBeCalledWith(actions.toggleIsFollowingAC(false, 1));
  27. })
  28.  
  29. ///////// Функция, которую тестирую
  30.  
  31. export const follow = (userId: number): ThunkActionType => {
  32.   return async (dispatch) => {
  33.     dispatch(actions.toggleIsFollowingAC(true, userId))
  34.     const data = await usersAPI.follow(userId) //////// Вот тут получаю undefined
  35.     try {
  36.       if (data.resultCode === 0) {
  37.         dispatch(actions.followSuccess(userId))
  38.       }
  39.       dispatch(actions.toggleIsFollowingAC(false, userId))
  40.     } catch (error) {
  41.       dispatch(actions.toggleIsFollowingAC(false, userId))
  42.     }
  43.   }
  44. }
  45.  
  46.  
  47. /// REQUEST TO API
  48.  
  49. export const instanceAxios = axios.create({
  50.   baseURL: 'https://social-network.samuraijs.com/api/1.0/',
  51.   withCredentials: true,
  52.   headers: {
  53.     'API-KEY': '91cc5a84-d625-478b-a406-09acdffa3140'
  54.   }
  55. })
  56.  
  57. export interface IResponseAPI {
  58.   data: object,
  59.   resultCode: ResultCodesEnum,
  60.   messages: Array<string>
  61. }
  62.  
  63. export const usersAPI = {
  64.   follow(userId: number) {
  65.     return instanceAxios.post<IResponseAPI>(`follow/${userId}`)
  66.       .then(response => {
  67.         return response.data
  68.       })
  69.   },
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement