import React from 'react';
import { screen } from '@testing-library/react';
import { renderInAppContext } from '@theorchard/suite-testing';
import {
    AbacusContractMechDeductionAdminType,
    AbacusContractMechDeductionTerritory,
} from 'src/apollo/definitions/globalTypes';
import {
    ContractMechanicalDeductionAdminFee,
    ContractMechanicalDeductionAdminFeePropTypes,
} from 'src/components/contract-mechanical-deduction-shared/contract-mechanical-deduction-admin-fee';

describe('<ContractMechanicalDeductionAdminFee />', () => {
    const defaultProps: ContractMechanicalDeductionAdminFeePropTypes = {
        contractMechanicalDeduction: {
            adminFee: '90.10',
            adminType: AbacusContractMechDeductionAdminType.CUSTOMER,
            isAdminFeeSelected: false,
            mechanicalType: [],
            territory: AbacusContractMechDeductionTerritory.USA,
        },
        errors: {},
        onAdminFeeSelect: jest.fn(),
        handleFormChange: jest.fn(),
    };

    const render = (
        props: ContractMechanicalDeductionAdminFeePropTypes = defaultProps
    ) => renderInAppContext(<ContractMechanicalDeductionAdminFee {...props} />);

    it('renders Admin Fee options', () => {
        render();
        expect(screen.getByText('Admin Fee')).toBeInTheDocument();

        expect(screen.getByText('No')).toBeInTheDocument();
        expect(screen.getByText('Yes')).toBeInTheDocument();
    });

    it('does not render input box if "No" option is selected', () => {
        render();

        const adminFeeOptions = screen.getAllByTestId('AdminFeeOptions');

        expect(adminFeeOptions[0]).toBeChecked();
        expect(adminFeeOptions[1]).not.toBeChecked();
        expect(screen.queryByText('Fee')).not.toBeInTheDocument();
        expect(screen.queryByTestId('AdminFeeInput')).not.toBeInTheDocument();
    });

    it('renders input box if "Yes" option is selected', () => {
        render({
            ...defaultProps,
            contractMechanicalDeduction: {
                ...defaultProps.contractMechanicalDeduction,
                isAdminFeeSelected: true,
            },
        });

        const adminFeeOptions = screen.getAllByTestId('AdminFeeOptions');

        expect(adminFeeOptions[1]).toBeChecked();
        expect(screen.getByText('Fee')).toBeInTheDocument();
        expect(screen.getByTestId('AdminFeeInput')).toBeInTheDocument();
        expect(screen.getByTestId('AdminFeeInput')).toHaveValue('90.10');
    });

    it('renders error message if any', () => {
        const errorMsg = 'Admin fee between 0.1 and 100.';
        render({
            ...defaultProps,
            contractMechanicalDeduction: {
                ...defaultProps.contractMechanicalDeduction,
                isAdminFeeSelected: true,
            },
            errors: { adminFee: errorMsg },
        });

        expect(screen.getByText(errorMsg)).toBeInTheDocument();
    });
});
