import React from 'react';
import { fireEvent } from '@testing-library/react';
import { renderInAppContext } from '@theorchard/suite-testing';
import * as prefetchQueries from 'src/apollo/utils/usePrefetchQuery';
import { ABACUS_QA_LINK } from 'src/constants/route';
import { GO_TO_ABACUS_TERM } from 'src/constants/staticFormatMessages';
import Modal, { CLASSNAME } from '../invitedModal';

jest.mock('src/apollo/queries/account/accountDetails', () => ({
    query: '',
}));

describe('<InvitedModal>', () => {
    const renderWrapper = (id: number = 1) =>
        renderInAppContext(<Modal accountId={id} />);

    beforeEach(() => {
        jest.spyOn(prefetchQueries, 'usePrefetchQuery').mockImplementation(
            jest.fn()
        );
    });

    test('renders default component', () => {
        const { getByTestId } = renderWrapper();

        expect(getByTestId(CLASSNAME)).toBeVisible();
    });

    test('handles confirmClick', () => {
        const openMock = jest
            .spyOn(window, 'open')
            .mockImplementation(() => null);
        const id = 777;
        const { getByText } = renderWrapper(id);

        const button = getByText(GO_TO_ABACUS_TERM);

        expect(button).toBeVisible();

        fireEvent.click(button);

        expect(openMock).toHaveBeenCalledWith(`${ABACUS_QA_LINK}account/${id}`);
    });
});
