81 lines
2.1 KiB
TypeScript
81 lines
2.1 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 { AddIncomeItemFormProps, AddIncomeItemFormValues } from '@/entity/incomes/item';
|
|
|
|
import { AddIncomeItemForm } from '@/entity/incomes/item';
|
|
import { DataTestId } from '@/shared/constants';
|
|
|
|
const renderForm = (props?: Partial<AddIncomeItemFormProps>) => {
|
|
const onSubmit = jest.fn();
|
|
|
|
render(
|
|
<AddIncomeItemForm onSubmit={onSubmit} {...props} />,
|
|
{ wrapper: createAppWrapper() },
|
|
);
|
|
|
|
return {
|
|
onSubmit,
|
|
};
|
|
};
|
|
|
|
const getFormElements = () => {
|
|
const formElem = screen.getByTestId(DataTestId.ADD_INCOME_ITEM_FORM);
|
|
const titleInputElem = screen.getByLabelText(/Название/i);
|
|
const descriptionInputElem = screen.getByLabelText(/Описание/);
|
|
const dateInputElem = screen.getAllByLabelText(/Дата/i)[1];
|
|
const amountInputElem = screen.getByLabelText(/Сумма/i);
|
|
const submitButtonElem = screen.getByTestId(DataTestId.ADD_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<AddIncomeItemFormValues, '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 AddIncomeItemFormHelper = {
|
|
fillForm,
|
|
getFormElements,
|
|
renderForm,
|
|
submitForm,
|
|
};
|