98 lines
2.5 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 { 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:03.000Z',
description: 'Mock description',
id: '51a44dc0-597b-49b9-85bb-e0bcd42db3f6',
incomeListId: '10cc093c-3875-4d33-a40a-df526266e262',
title: 'Mock item',
updatedAt: '2025-08-29T07:57:40.503Z',
};
const renderForm = (props?: Partial<EditIncomeItemFormProps>) => {
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);
return {
amountInputElem,
dateInputElem,
descriptionInputElem,
formElem,
submitButtonElem,
titleInputElem,
};
};
const submitForm = async () => {
const { submitButtonElem } = getFormElements();
await userEvent.click(submitButtonElem);
};
const fillForm = async (
data: Omit<EditIncomeItemFormValues, 'currency'>,
options: { autoSubmit?: boolean } = {},
) => {
const {
amount,
date,
description,
title,
} = data;
const { autoSubmit = true } = options;
const {
amountInputElem,
dateInputElem,
descriptionInputElem,
titleInputElem,
} = getFormElements();
await typeTextField(titleInputElem, title);
await typeTextField(descriptionInputElem, description);
await typeDateField(dateInputElem, date);
await typeTextField(amountInputElem, amount.toString());
if (autoSubmit) {
await submitForm();
}
};
export const EditIncomeItemFormHelper = {
fillForm,
getFormElements,
renderForm,
submitForm,
};