58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import { typeTextField } from '@mocks/jest/helpers';
|
|
import { render, screen } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
|
|
import type { AddIncomeListFormProps, AddIncomeListFormValues } from '@/entity/incomes/list';
|
|
|
|
import { AddIncomeListForm } from '@/entity/incomes/list';
|
|
import { DataTestId } from '@/shared/constants';
|
|
|
|
const renderForm = (props?: Partial<AddIncomeListFormProps>) => {
|
|
const onSubmit = jest.fn();
|
|
|
|
render(<AddIncomeListForm onSubmit={onSubmit} {...props} />);
|
|
|
|
return {
|
|
onSubmit,
|
|
};
|
|
};
|
|
|
|
const getFormElements = () => {
|
|
const formElem = screen.getByTestId(DataTestId.ADD_INCOME_LIST_FORM);
|
|
const titleInputElem = screen.getByLabelText(/Название/);
|
|
const submitButtonElem = screen.getByTestId(DataTestId.ADD_INCOME_LIST_SUBMIT_BUTTON);
|
|
|
|
return {
|
|
formElem,
|
|
submitButtonElem,
|
|
titleInputElem,
|
|
};
|
|
};
|
|
|
|
const submitForm = async () => {
|
|
const { submitButtonElem } = getFormElements();
|
|
await userEvent.click(submitButtonElem);
|
|
};
|
|
|
|
const fillForm = async (
|
|
data: AddIncomeListFormValues,
|
|
options: { autoSubmit?: boolean } = {},
|
|
) => {
|
|
const { title } = data;
|
|
const { autoSubmit = true } = options;
|
|
const { titleInputElem } = getFormElements();
|
|
|
|
await typeTextField(titleInputElem, title);
|
|
|
|
if (autoSubmit) {
|
|
await submitForm();
|
|
}
|
|
};
|
|
|
|
export const AddIncomeListFormHelper = {
|
|
fillForm,
|
|
getFormElements,
|
|
renderForm,
|
|
submitForm,
|
|
};
|