import type { ActionButtonCode } from '@sber-pprb-efsdf/rapid-ui-core'; import {…
- User
import type { ActionButtonCode } from '@sber-pprb-efsdf/rapid-ui-core';
import { createAppAsyncThunk } from '$ufs-common';
import { setActionButtonPending } from '../slices';
export interface CallActionButtonProps { actionCode: ActionButtonCode; action: () => Promise; }
export const [useCallActionButton, callActionButton] = createAppAsyncThunk< void, CallActionButtonProps
('callActionButton', async ({ actionCode, action }, { dispatch }) => { try { dispatch( setActionButtonPending({ actionCode, isFetching: true, }), );
codeawait action(); dispatch( setActionButtonPending({ actionCode, isFetching: false, }), ); } catch (error) { dispatch( setActionButtonPending({ actionCode, isFetching: false, }), ); }}); import { TestThunk } from '$tests';
import { setActionButtonPending } from '../../slices'; import { callActionButton, CallActionButtonProps } from '../callActionButton';
jest.mock('../../slices', () => ({ setActionButtonPending: jest.fn(), }));
describe('callActionButton', () => { const defaultProps: CallActionButtonProps = { action: jest.fn(), actionCode: 'CLONE', };
codetest('should set pending to true, call action and set pending to false', async () => { const thunk = new TestThunk(callActionButton); await thunk.callThunk(defaultProps); expect(setActionButtonPending).toHaveBeenNthCalledWith(1, { actionCode: defaultProps.actionCode, isFetching: true, }); expect(defaultProps.action).toHaveBeenCalled(); expect(setActionButtonPending).toHaveBeenNthCalledWith(2, { actionCode: defaultProps.actionCode, isFetching: false, }); }); test('should set pending to false when action is failed', async () => { const thunk = new TestThunk(callActionButton); await thunk.callThunk({ ...defaultProps, action: jest.fn().mockRejectedValue(null) }); expect(setActionButtonPending).toHaveBeenNthCalledWith(1, { actionCode: defaultProps.actionCode, isFetching: false, }); }); - Assistant
В текущем тесте для проверки случая ошибки в
callActionButtonпроблема в том, что вы ожидаете, чтоsetActionButtonPendingбудет вызван первым сisFetching: false, а на самом деле в блокеtryсначала вызывается сisFetching: true. То есть первый вызов должен быть сisFetching: true, а второй (в блокеcatch) с .