import React from 'react';
import * as apollo from '@apollo/client';
import { render, fireEvent } from '@testing-library/react';
import { useCreateNrContributorMutation } from '../createNrContributor';

describe('useCreateNrContributorMutation', () => {
    const buttonId = 'BTN';
    const initVariables = {
        vendorId: 1,
        forename: 'forename',
        surname: 'surname',
        middleNames: ['middleNames'],
    };
    const TestApp = () => {
        const { createNrContributor } =
            useCreateNrContributorMutation(initVariables);

        return (
            <button
                type="button"
                data-testid={buttonId}
                onClick={() => {
                    void createNrContributor();
                }}
            />
        );
    };

    const renderWrapper = () => render(<TestApp />);

    const mock = jest.fn();

    beforeEach(() => {
        jest.spyOn(apollo, 'useMutation').mockReturnValue([
            mock,
            {} as apollo.MutationResult,
        ]);
        mock.mockClear();
    });

    test('executes mutation', () => {
        const { getByTestId } = renderWrapper();
        const btn = getByTestId(buttonId);
        fireEvent.click(btn);

        expect(mock).toHaveBeenCalled();
        expect(mock).toHaveBeenCalledTimes(1);
    });
});
