import React from 'react';
import { screen } from '@testing-library/react';
import { createIdentity, renderInAppContext } from '@theorchard/suite-testing';
import {
    ContractNotes,
    ContractNoteProps,
} from 'src/components/contract-detail/contract-notes';
import { ABACUS_PROFILE, NO_GENERAL_NOTE } from 'src/constants';

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

    const props = {
        contractId: '111',
        generalNote: 'testing note',
    };
    0;

    it('renders ContractNotes', () => {
        render(props);
        expect(screen.getByText('Additional Notes')).toBeDefined();
    });

    it('renders "No Contract Notes" text when a contract does not have notes', () => {
        props.generalNote = '';
        render(props);
        expect(screen.getByText(NO_GENERAL_NOTE)).toBeDefined();
    });

    it('renders "No Contract Notes" text and edit link when a contract does not have notes', () => {
        props.generalNote = '';
        render(props);
        expect(screen.getByText(NO_GENERAL_NOTE)).toBeDefined();

        const link = screen.getByRole('link', { name: 'Add' });
        expect(link).toHaveAttribute('href', '/contract/111/note/edit');
    });

    it('links to a edit form when a contract does have a note', () => {
        props.generalNote = 'this is a test note';
        render(props);
        const editButton: any = screen.getByTestId('contractNoteEditBtnTestId');
        expect(editButton.href).toContain('/contract/111/note/edit');
    });

    it('a note appears when a contract does have a note', () => {
        props.generalNote = 'this is a test note';
        render(props);
        expect(screen.getByText('this is a test note')).toBeDefined();
    });
});
