import React from 'react';
import * as apollo from '@apollo/client';
import { render, fireEvent } from '@testing-library/react';
import { ToastProvider } from '@theorchard/suite-frontend';
import { useRenameArtistMutation } from '../renameArtist';

describe('useRenameArtistMutation', () => {
    const buttonId = 'BTN';
    const initVariables = {
        uuid: '1',
        name: 'test-rename-artist',
    };
    const TestApp = () => {
        const { renameLabelParticipant } =
            useRenameArtistMutation(initVariables);

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

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

    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);
    });
});
