146 lines
3.8 KiB
TypeScript

import { getBankCardList, getCashList } from '@mocks/browser/handlers';
import { selectOption, typeDateField, typeTextField } from '@mocks/jest/helpers';
import { server } from '@mocks/jest/server';
import { createAppWrapper } from '@mocks/jest/wrappers';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import type { EditIncomeItemFormProps, EditIncomeItemFormValues } from '@/entity/incomes/item';
import { EditIncomeItemForm } from '@/entity/incomes/item';
import { DataTestId } from '@/shared/constants';
const incomeItem = {
amount: 12,
count: 12,
createdAt: '2025-08-29T07:57:40.503Z',
currency: 'RUB',
date: '2025-08-22T00:00:00.000Z',
description: 'Mock description',
id: '51a44dc0-597b-49b9-85bb-e0bcd42db3f6',
incomeListId: '10cc093c-3875-4d33-a40a-df526266e262',
target: null,
title: 'Mock item',
updatedAt: '2025-08-29T07:57:40.503Z',
};
const renderForm = (props?: Partial<EditIncomeItemFormProps>) => {
server.use(getBankCardList, getCashList);
const onSubmit = jest.fn();
render(
<EditIncomeItemForm
item={incomeItem}
onSubmit={onSubmit}
{...props}
/>,
{ wrapper: createAppWrapper() },
);
return {
onSubmit,
};
};
const getFormElements = () => {
const formElem = screen.getByTestId(DataTestId.EDIT_INCOME_ITEM_FORM);
const titleInputElem = screen.getByLabelText(/Название/i);
const descriptionInputElem = screen.getByLabelText(/Описание/i);
const dateInputElem = screen.getAllByLabelText(/Дата/i)[1];
const amountInputElem = screen.getByLabelText(/Сумма/i);
const submitButtonElem = screen.getByTestId(DataTestId.EDIT_INCOME_ITEM_SUBMIT_BUTTON);
const targetTypeSelect = screen.getByTestId('target-type-select');
const bankCardSelect = screen.queryByTestId('bank-card-select');
const bankCardSelectInput = screen.queryByTestId('bank-card-select-input');
const cashSelect = screen.queryByTestId('cash-select');
const cashSelectInput = screen.queryByTestId('cash-select-input');
return {
amountInputElem,
bankCardSelect,
bankCardSelectInput,
cashSelect,
cashSelectInput,
dateInputElem,
descriptionInputElem,
formElem,
submitButtonElem,
targetTypeSelect,
titleInputElem,
};
};
const submitForm = async () => {
const { submitButtonElem } = getFormElements();
await userEvent.click(submitButtonElem);
};
const fillForm = async (
data: Partial<Omit<EditIncomeItemFormValues, 'currency' | 'date'> & { date: string }>,
options: { autoSubmit?: boolean } = {},
) => {
const {
amount,
'bank-card': bankCard,
cash,
date,
description,
targetType,
title,
} = data;
const { autoSubmit = true } = options;
const {
amountInputElem,
dateInputElem,
descriptionInputElem,
targetTypeSelect,
titleInputElem,
} = getFormElements();
await typeTextField(titleInputElem, title);
await typeTextField(descriptionInputElem, description);
await typeDateField(dateInputElem, date);
await typeTextField(amountInputElem, amount?.toString());
if (targetType && targetType !== 'none') {
switch (targetType) {
case 'BANK_CARD':
await selectOption(targetTypeSelect, 'BANK_CARD');
break;
case 'CASH':
await selectOption(targetTypeSelect, 'CASH');
break;
default:
break;
}
if (!bankCard && !cash) {
return;
}
const { bankCardSelect, cashSelect } = getFormElements();
switch (targetType) {
case 'BANK_CARD':
await selectOption(bankCardSelect, bankCard);
break;
case 'CASH':
await selectOption(cashSelect, cash);
break;
default:
break;
}
}
if (autoSubmit) {
await submitForm();
}
};
export const EditIncomeItemFormHelper = {
fillForm,
getFormElements,
renderForm,
submitForm,
};