import React from 'react';
import { screen } from '@testing-library/react';
import { renderInAppContext } from '@theorchard/suite-testing';
import { AbacusContractLifecycleScheduleRenewalType } from 'src/apollo/definitions/globalTypes';
import {
    SubsequentPeriodFormFields,
    SubsequentPeriodFormFieldsPropTypes,
} from '../subsequent-period-form-fields';

describe('<SubsequentPeriodFormFields />', () => {
    const defaultProps: SubsequentPeriodFormFieldsPropTypes = {
        currentSchedule: {
            renewalType: '',
        },
        contractLifecycleSchedules: [],
        defaultFormValues: {
            renewalType: '',
        },
        errors: {},
        setContractLifecycleSchedules: jest.fn(),
        setErrors: jest.fn(),
    };

    const renderComponent = (props = defaultProps) =>
        renderInAppContext(<SubsequentPeriodFormFields {...props} />);

    describe('Continuously Active renewal rule', () => {
        it('renders the Termination Notice Period when selected', () => {
            const anotherSchedule = {
                renewalType:
                    AbacusContractLifecycleScheduleRenewalType.RENEW_AFTER_CERTAIN_DATE as string,
            };
            const currentSchedule = {
                renewalType:
                    AbacusContractLifecycleScheduleRenewalType.CONTINUOUSLY_ACTIVE as string,
            };
            const contractLifecycleSchedules = [
                anotherSchedule,
                currentSchedule,
            ];
            renderComponent({
                ...defaultProps,
                contractLifecycleSchedules,
                currentSchedule,
            });

            const terminationNoticeLabel = screen.queryByText(
                'Termination Notice Period'
            );
            const terminationNoticeInput = screen.queryByTestId(
                'termination-notice-interval'
            );
            const terminationNoticeType = screen.queryByTestId(
                'termination-notice-type'
            );

            expect(terminationNoticeLabel).toBeInTheDocument();
            expect(terminationNoticeInput).toBeInTheDocument();
            expect(terminationNoticeType).toBeInTheDocument();
        });
    });

    describe('Renew Periodically renewal rule', () => {
        beforeEach(() => {
            const anotherSchedule = {
                renewalType:
                    AbacusContractLifecycleScheduleRenewalType.RENEW_AFTER_CERTAIN_DATE as string,
            };
            const currentSchedule = {
                renewalType:
                    AbacusContractLifecycleScheduleRenewalType.RENEW_PERIODICALLY as string,
            };
            const contractLifecycleSchedules = [
                anotherSchedule,
                currentSchedule,
            ];
            renderComponent({
                ...defaultProps,
                contractLifecycleSchedules,
                currentSchedule,
            });
        });

        it('renders Renew After when selected', () => {
            const renewAfterLabel = screen.getByText('Renew After');
            const renewAfterInput = screen.getByTestId(
                'renewal-offset-interval'
            );
            const renewAfterType = screen.getByTestId('renewal-offset-type');

            expect(renewAfterLabel).toBeInTheDocument();
            expect(renewAfterInput).toBeInTheDocument();
            expect(renewAfterType).toBeInTheDocument();
            expect(renewAfterInput.innerHTML).toContain('e.g. 1');
            expect(renewAfterType.textContent).toEqual('MonthsYears');
        });

        it('renders the Termination Notice Period when selected', () => {
            const terminationNoticeLabel = screen.queryByText(
                'Termination Notice Period'
            );
            const terminationNoticeInput = screen.queryByTestId(
                'termination-notice-interval'
            );
            const terminationNoticeType = screen.queryByTestId(
                'termination-notice-type'
            );

            expect(terminationNoticeLabel).toBeInTheDocument();
            expect(terminationNoticeInput).toBeInTheDocument();
            expect(terminationNoticeType).toBeInTheDocument();
        });
    });
});
