import React from 'react';
import { screen } from '@testing-library/react';
import { createIdentity, renderInAppContext } from '@theorchard/suite-testing';
import { contractDetails as contractDetailResponse } from 'src/__fixtures__/graphql/contract-details';
import {
    ContractPhysicalReserve,
    PhysicalReserveProps,
} from 'src/components/contract-detail/contract-physical-reserve';
import { ABACUS_PROFILE, NO_PHYSICAL_RESERVES } from 'src/constants';

describe('<ContractPhysicalReserve>', () => {
    const render = (props: PhysicalReserveProps) =>
        renderInAppContext(<ContractPhysicalReserve {...props} />, {
            identity: createIdentity({ profileType: ABACUS_PROFILE }),
        });

    const props = {
        contractId: '111',
        physicalReserve: null,
    };

    it('renders ContractPhysicalReserve', () => {
        render(props);
        expect(screen.getByText('Physical Reserves')).toBeDefined();
    });

    it('renders "No Physical Reserve" text when a contract does not have a reserve', () => {
        render(props);
        expect(screen.getByText(NO_PHYSICAL_RESERVES)).toBeDefined();
    });

    it('links to a create form when a contract does not have a reserve', () => {
        render(props);
        const editButton: any = screen.getByTestId(
            'physicalReserveEditBtnTestId'
        );
        expect(editButton.href).toContain('/contract/111/physical-reserve/new');
    });

    it('links to an update form when a contract already has a reserve', () => {
        const mockReserve = contractDetailResponse.abacusContract.reserve;
        render({ ...props, physicalReserve: mockReserve });
        const editButton: any = screen.getByTestId(
            'physicalReserveEditBtnTestId'
        );
        expect(editButton.href).toContain(
            '/contract/111/physical-reserve/edit'
        );
    });
});
