import { renderWithClient } from '../../../../utils/test/react-query';
import CreateSettleRequestForm from '../create-settle-request-form';
import userEvent from '@testing-library/user-event';
import { mockedIBan } from '../../../../mocks/data';
import { waitFor } from '@testing-library/react';
import * as settleRequestAPI from '../../../../api/settle-request';
import { MemoryRouter } from 'react-router-dom';

describe('CreateSettleRequestForm component', () => {
  test('successful render component', async () => {
    const result = renderWithClient(<CreateSettleRequestForm />, MemoryRouter);
    expect(await result.findByTestId('create-settle-request-form')).toBeInTheDocument();
  });

  test('call create introducer function with correct arguments', async () => {
    const createIntroducerSpy = jest.spyOn(settleRequestAPI, 'createSettleRequest');
    const data = { iban: mockedIBan };

    const result = renderWithClient(<CreateSettleRequestForm />, MemoryRouter);

    const user = userEvent.setup();
    await user.type(result.getByLabelText(/شبا/), data.iban);
    await user.click(await result.getByRole('button', { name: /درخواست تسویه حساب/i }));

    await waitFor(() => expect(createIntroducerSpy).toHaveBeenCalledWith(data));
    createIntroducerSpy.mockRestore();
  });
});
