import React from 'react';
import { screen } from '@testing-library/react';
import { renderInAppContext } from '@theorchard/suite-testing';
import AttachedContracts, {
    type AttachedContractsProps,
} from 'src/components/account-detail/attached-contracts';
import { CONTRACT_TYPE_MAP } from 'src/constants';
import { getContractDetail } from 'src/urls/frontend-royalties';

describe('<AttachedContracts />', () => {
    const mockContracts = [
        {
            contractId: 3,
            contractName: 'Test Contract Name',
            contractType: 'distribution' as keyof typeof CONTRACT_TYPE_MAP,
            runController: { runControllerName: 'RC Name' },
        },
    ];
    const defaultProps: AttachedContractsProps = {
        contracts: mockContracts,
        isLoading: false,
    };

    const renderComponent = (props: AttachedContractsProps) =>
        renderInAppContext(<AttachedContracts {...props} />);

    it('renders', () => {
        renderComponent(defaultProps);
        expect(screen.getByTestId('paymentDetailContracts')).toBeDefined();
    });

    it('displays a message if the payee has no contracts', () => {
        const propsNoContract = { ...defaultProps, contracts: [] };
        renderComponent(propsNoContract);

        expect(
            screen.getByTestId('paymentDetailContracts').textContent
        ).toContain('No contracts');
    });

    it('displays contract data', () => {
        renderComponent(defaultProps);

        const contractText = screen.getByTestId(
            'paymentDetailContracts'
        ).textContent;
        const contractNameLink = screen.getByText(
            mockContracts[0].contractName
        );

        expect(contractText).toContain(mockContracts[0].contractName);
        expect(contractText).toContain(
            mockContracts[0].runController.runControllerName
        );
        expect(contractText).toContain(
            CONTRACT_TYPE_MAP[mockContracts[0].contractType]
        );
        // @ts-expect-error: href does exist on link element
        expect(contractNameLink.href).toContain(
            `${getContractDetail(mockContracts[0].contractId)}`
        );
    });
});
