/
dedaMazai
/
TestServer
Обзор
Документация
Войти
/
dedaMazai
/
TestServer
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/shared/lib/tests/TestAsyncThunk/TestAsyncThunk.ts
45 строк
1 KB
Andrey Chipizubov
fix
23 апр 2023, 17:32
23 апр 2023, 17:32
cc424e9
Код
Авторство
О чём код?
import { AsyncThunkAction } from '@reduxjs/toolkit'; import axios, { AxiosStatic } from 'axios'; import { StateSchema } from '@/app/providers/StoreProvider'; type ActionCreatorType<Return, Arg, RejectedValue> = ( arg: Arg, ) => AsyncThunkAction<Return, Arg, { rejectValue: RejectedValue }>; jest.mock('axios'); const mockedAxios = jest.mocked(axios, true); export class TestAsyncThunk<Return, Arg, RejectedValue> { dispatch: jest.MockedFn<any>; getState: () => StateSchema; actionCreator: ActionCreatorType<Return, Arg, RejectedValue>; api: jest.MockedFunctionDeep<AxiosStatic>; navigate: jest.MockedFn<any>; constructor( actionCreator: ActionCreatorType<Return, Arg, RejectedValue>, state?: DeepPartial<StateSchema>, ) { this.actionCreator = actionCreator; this.dispatch = jest.fn(); this.getState = jest.fn(() => state as StateSchema); this.api = mockedAxios; this.navigate = jest.fn(); } async callThunk(arg: Arg) { const action = this.actionCreator(arg); const result = await action(this.dispatch, this.getState, { api: this.api, navigate: this.navigate, }); return result; } }