import React from 'react';
import { MockedProvider } from '@apollo/client/testing';
import { renderHook, act } from '@testing-library/react-hooks';
import rejectMutation from '../RejectMutation.gql';
import { useRejectProductMutation } from '../rejectProductReview';

describe('useRejectProductMutation', () => {
    it('should trigger the reject mutation with trimmed note and call the onSubmitCallback', async () => {
        const onSubmitCallback = jest.fn();
        const mocks = [
            {
                request: {
                    query: rejectMutation,
                    variables: {
                        reviewQueueId: 99,
                        cannedResponseIds: ['1', '2'],
                        note: 'test note',
                    },
                },
                result: {
                    data: {
                        reject: {
                            status: 'ok',
                        },
                    },
                },
            },
        ];

        const Wrapper: React.FC<{ children: React.ReactNode }> = ({
            children,
        }) => (
            <MockedProvider mocks={mocks} addTypename={false}>
                {children}
            </MockedProvider>
        );

        const { result, waitForNextUpdate } = renderHook(
            () =>
                useRejectProductMutation(
                    99,
                    ' test note\n ',
                    ['1', '2'],
                    onSubmitCallback
                ),
            {
                wrapper: Wrapper,
            }
        );

        void act(() => {
            void result.current();
        });

        await waitForNextUpdate();

        expect(onSubmitCallback).toHaveBeenCalledWith('ok', 'Rejection');
    });

    it('should call the onSubmitCallback with "error" when the mutation returns an error', async () => {
        const onSubmitCallback = jest.fn();
        const mocks = [
            {
                request: {
                    query: rejectMutation,
                    variables: {
                        reviewQueueId: 99,
                        note: 'test note',
                    },
                },
                error: new Error('An error occurred'),
            },
        ];

        const Wrapper: React.FC<{ children: React.ReactNode }> = ({
            children,
        }) => (
            <MockedProvider mocks={mocks} addTypename={false}>
                {children}
            </MockedProvider>
        );

        const { result, waitForNextUpdate } = renderHook(
            () =>
                useRejectProductMutation(
                    99,
                    'test note',
                    ['1', '2'],
                    onSubmitCallback
                ),
            {
                wrapper: Wrapper,
            }
        );

        void act(() => {
            void result.current();
        });

        await waitForNextUpdate();

        expect(onSubmitCallback).toHaveBeenCalledWith('error', 'Rejection');
    });
});
