80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import { server } from '@mocks/jest/server';
|
|
import { createReactQueryWrapper } from '@mocks/jest/wrappers';
|
|
import { render, screen, within } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
import { http, HttpResponse } from 'msw';
|
|
|
|
import type { DeleteExpenseItemPath } from '@/entity/expenses/item';
|
|
|
|
import DeleteExpenseItemButton from './DeleteExpenseItemButton.ui';
|
|
|
|
const deletedItem = {
|
|
amount: 12,
|
|
count: 12,
|
|
createdAt: '2025-08-29T07:57:40.503Z',
|
|
currency: 'RUB',
|
|
date: '2025-08-22T00:00:03.000Z',
|
|
description: 'Mock description',
|
|
expenseListId: '10cc093c-3875-4d33-a40a-df526266e262',
|
|
id: '51a44dc0-597b-49b9-85bb-e0bcd42db3f6',
|
|
payment: null,
|
|
title: 'Mock item',
|
|
updatedAt: '2025-08-29T07:57:40.503Z',
|
|
};
|
|
|
|
const renderButton = () => {
|
|
render(
|
|
<DeleteExpenseItemButton data-testid="delete-expense-item-button" item={deletedItem} />,
|
|
{ wrapper: createReactQueryWrapper() },
|
|
);
|
|
|
|
const buttonElem = screen.getByTestId('delete-expense-item-button');
|
|
|
|
return {
|
|
buttonElem,
|
|
};
|
|
};
|
|
|
|
const openModal = async () => {
|
|
const { buttonElem } = renderButton();
|
|
await userEvent.click(buttonElem);
|
|
const modalElem = screen.getByRole('dialog');
|
|
|
|
return {
|
|
modalElem,
|
|
};
|
|
};
|
|
|
|
const submitHandler = http.delete<DeleteExpenseItemPath>(`/expenses/list/${deletedItem.expenseListId}/items/${deletedItem.id}`, () => HttpResponse.json(deletedItem));
|
|
|
|
describe('Test DeleteExpenseItemButton', () => {
|
|
test('should correct render button', () => {
|
|
const { buttonElem } = renderButton();
|
|
|
|
expect(buttonElem).toBeInTheDocument();
|
|
expect(buttonElem).toBeVisible();
|
|
expect(buttonElem).toBeEnabled();
|
|
expect(buttonElem).toHaveTextContent('Удалить');
|
|
|
|
const modal = screen.queryByRole('dialog');
|
|
expect(modal).not.toBeInTheDocument();
|
|
});
|
|
|
|
test('should open modal after click', async () => {
|
|
const { modalElem } = await openModal();
|
|
|
|
expect(modalElem).toBeInTheDocument();
|
|
expect(modalElem).toBeVisible();
|
|
});
|
|
|
|
test('should close modal after submit', async () => {
|
|
server.use(submitHandler);
|
|
const { modalElem } = await openModal();
|
|
const submitButtonElem = within(modalElem).getByText('Удалить');
|
|
|
|
expect(submitButtonElem).toBeEnabled();
|
|
await userEvent.click(submitButtonElem);
|
|
expect(modalElem).not.toBeInTheDocument();
|
|
});
|
|
});
|