70 lines
1.6 KiB
TypeScript
70 lines
1.6 KiB
TypeScript
import { cashList } from '@mocks/browser/data';
|
|
import { typeTextField } from '@mocks/jest/helpers';
|
|
import { render, screen } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
|
|
import type { EditCashFormProps, EditCashFormValues } from '@/entity/cash';
|
|
|
|
import { EditCashForm } from '@/entity/cash';
|
|
import { DataTestId } from '@/shared/constants';
|
|
|
|
const cash = cashList[0];
|
|
|
|
const renderForm = (props?: Partial<EditCashFormProps>) => {
|
|
const onSubmit = jest.fn();
|
|
|
|
render(
|
|
<EditCashForm
|
|
cash={cash}
|
|
onSubmit={onSubmit}
|
|
{...props}
|
|
/>,
|
|
);
|
|
|
|
return {
|
|
onSubmit,
|
|
};
|
|
};
|
|
|
|
const getFormElements = () => {
|
|
const formElem = screen.getByTestId(DataTestId.EDIT_CASH_FORM);
|
|
const nameInputElem = screen.getByLabelText(/Название/);
|
|
const balanceInputElem = screen.getByLabelText(/Баланс/);
|
|
const submitButtonElem = screen.getByRole('button', { name: /Сохранить/i });
|
|
|
|
return {
|
|
balanceInputElem,
|
|
formElem,
|
|
nameInputElem,
|
|
submitButtonElem,
|
|
};
|
|
};
|
|
|
|
const submitForm = async () => {
|
|
const { submitButtonElem } = getFormElements();
|
|
await userEvent.click(submitButtonElem);
|
|
};
|
|
|
|
const fillForm = async (
|
|
data: EditCashFormValues,
|
|
options: { autoSubmit?: boolean } = {},
|
|
) => {
|
|
const { balance, name } = data;
|
|
const { autoSubmit = true } = options;
|
|
const { balanceInputElem, nameInputElem } = getFormElements();
|
|
|
|
await typeTextField(balanceInputElem, balance.toString());
|
|
await typeTextField(nameInputElem, name);
|
|
|
|
if (autoSubmit) {
|
|
await submitForm();
|
|
}
|
|
};
|
|
|
|
export const EditCashFormHelper = {
|
|
fillForm,
|
|
getFormElements,
|
|
renderForm,
|
|
submitForm,
|
|
};
|