104 lines
2.7 KiB
TypeScript

import { typeDateField, typeTextField } from '@mocks/jest/helpers';
import { createAppWrapper } from '@mocks/jest/wrappers';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import type { EditExpenseItemFormProps, EditExpenseItemFormValues } from '@/entity/expenses/item';
import { EditExpenseItemForm } from '@/entity/expenses/item';
import { DataTestId } from '@/shared/constants';
const expenseItem = {
amount: 12,
count: 12,
createdAt: '2025-08-22T07:57:40.503Z',
currency: 'RUB',
date: '2025-08-22T00:00:03.000Z',
description: 'Mock description',
expenseListId: '10cc093c-3875-4d33-a40a-df526266e262',
id: '51a44dc0-597b-49b9-85bb-e0bcd42db3f6',
payment: null,
title: 'Mock item',
updatedAt: '2025-08-29T07:57:40.503Z',
};
const renderForm = (props?: Partial<EditExpenseItemFormProps>) => {
const onSubmit = jest.fn();
render(
<EditExpenseItemForm
item={expenseItem}
onSubmit={onSubmit}
{...props}
/>,
{ wrapper: createAppWrapper() },
);
return {
onSubmit,
};
};
const getFormElements = () => {
const formElem = screen.getByTestId(DataTestId.EDIT_EXPENSE_ITEM_FORM);
const titleInputElem = screen.getByLabelText(/Название/);
const descriptionInputElem = screen.getByLabelText(/Описание/);
const dateInputElem = screen.getAllByLabelText(/Дата/i)[1];
const amountInputElem = screen.getByLabelText(/Сумма/);
const countInputElem = screen.getByLabelText(/Количество/);
const submitButtonElem = screen.getByRole('button', { name: /Сохранить/i });
return {
amountInputElem,
countInputElem,
dateInputElem,
descriptionInputElem,
formElem,
submitButtonElem,
titleInputElem,
};
};
const submitForm = async () => {
const { submitButtonElem } = getFormElements();
await userEvent.click(submitButtonElem);
};
const fillForm = async (
data: Omit<EditExpenseItemFormValues, 'currency'>,
options: { autoSubmit?: boolean } = {},
) => {
const {
amount,
count,
date,
description,
title,
} = data;
const { autoSubmit = true } = options;
const {
amountInputElem,
countInputElem,
dateInputElem,
descriptionInputElem,
titleInputElem,
} = getFormElements();
await typeTextField(titleInputElem, title);
await typeTextField(descriptionInputElem, description);
await typeTextField(amountInputElem, amount.toString());
await typeTextField(countInputElem, count.toString());
await typeDateField(dateInputElem, date);
if (autoSubmit) {
await submitForm();
}
};
export const EditExpenseItemFormHelper = {
fillForm,
getFormElements,
renderForm,
submitForm,
};