72 lines
1.7 KiB
TypeScript
72 lines
1.7 KiB
TypeScript
import { render, screen } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
|
|
import type { EditExpenseListFormValues, EditExpensesListFormProps } from '@/entity/expenses/list';
|
|
|
|
import { EditExpensesListForm } from '@/entity/expenses/list';
|
|
import { DataTestId } from '@/shared/constants';
|
|
|
|
const expenseList = {
|
|
createdAt: '2025-08-29T07:57:40.503Z',
|
|
id: '10cc093c-3875-4d33-a40a-df526266e262',
|
|
items: [],
|
|
title: 'Mock list',
|
|
updatedAt: '2025-08-29T07:57:40.503Z',
|
|
};
|
|
|
|
const renderForm = (props?: Partial<EditExpensesListFormProps>) => {
|
|
const onSubmit = jest.fn();
|
|
|
|
render(
|
|
<EditExpensesListForm
|
|
list={expenseList}
|
|
onSubmit={onSubmit}
|
|
{...props}
|
|
/>,
|
|
);
|
|
|
|
return {
|
|
onSubmit,
|
|
};
|
|
};
|
|
|
|
const getFormElements = () => {
|
|
const formElem = screen.getByTestId(DataTestId.EDIT_EXPENSES_LIST_FORM);
|
|
const titleInputElem = screen.getByLabelText(/Название/);
|
|
const submitButtonElem = screen.getByRole('button', { name: /Сохранить/ });
|
|
|
|
return {
|
|
formElem,
|
|
submitButtonElem,
|
|
titleInputElem,
|
|
};
|
|
};
|
|
|
|
const submitForm = async () => {
|
|
const { submitButtonElem } = getFormElements();
|
|
await userEvent.click(submitButtonElem);
|
|
};
|
|
|
|
export const fillForm = async (
|
|
data: EditExpenseListFormValues,
|
|
options: { autoSubmit?: boolean } = {},
|
|
) => {
|
|
const { title } = data;
|
|
const { autoSubmit = true } = options;
|
|
const { titleInputElem } = getFormElements();
|
|
|
|
await userEvent.clear(titleInputElem);
|
|
await userEvent.type(titleInputElem, title);
|
|
|
|
if (autoSubmit) {
|
|
await submitForm();
|
|
}
|
|
};
|
|
|
|
export const EditExpensesListFormHelper = {
|
|
fillForm,
|
|
getFormElements,
|
|
renderForm,
|
|
submitForm,
|
|
};
|