60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
import { render, screen } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
|
|
import type { AddExpenseListFormProps, AddExpenseListFormValues } from '@/entity/expenses/list';
|
|
|
|
import { AddExpensesListForm } from '@/entity/expenses/list';
|
|
import { DataTestId } from '@/shared/constants';
|
|
|
|
const renderForm = (props?: Partial<AddExpenseListFormProps>) => {
|
|
const onSubmit = jest.fn();
|
|
|
|
render(<AddExpensesListForm onSubmit={onSubmit} {...props} />);
|
|
|
|
return {
|
|
onSubmit,
|
|
};
|
|
};
|
|
|
|
const getFormElements = () => {
|
|
const formElem = screen.getByTestId(DataTestId.ADD_EXPENSES_LIST_FORM);
|
|
const titleInputElem = screen.getByLabelText(/Название/i);
|
|
const submitButtonElem = screen.getByTestId(DataTestId.ADD_EXPENSES_LIST_SUBMIT_BUTTON);
|
|
|
|
return {
|
|
formElem,
|
|
submitButtonElem,
|
|
titleInputElem,
|
|
};
|
|
};
|
|
|
|
const submitForm = async () => {
|
|
const { submitButtonElem } = getFormElements();
|
|
await userEvent.click(submitButtonElem);
|
|
};
|
|
|
|
const fillForm = async (
|
|
data: AddExpenseListFormValues,
|
|
options: { autoSubmit?: boolean } = {},
|
|
) => {
|
|
const { title } = data;
|
|
const { autoSubmit = true } = options;
|
|
const { titleInputElem } = getFormElements();
|
|
|
|
if (title) {
|
|
await userEvent.clear(titleInputElem);
|
|
await userEvent.type(titleInputElem, title);
|
|
}
|
|
|
|
if (autoSubmit) {
|
|
await submitForm();
|
|
}
|
|
};
|
|
|
|
export const AddExpensesListFormHelper = {
|
|
fillForm,
|
|
getFormElements,
|
|
renderForm,
|
|
submitForm,
|
|
};
|