import React from 'react';
import { screen, fireEvent } from '@testing-library/react';
import { createIdentity, renderInAppContext } from '@theorchard/suite-testing';
import { contractPartyList } from 'src/__fixtures__/graphql/contract-party';
import {
    CONTRIBUTORS_HEADERS,
    NO_CONTRIBUTORS_FOUND,
} from 'src/apollo/type-constants/contract';
import { ContributorsList } from 'src/components/contract-contributors-form/contributors-list';
import { ABACUS_PROFILE } from 'src/constants';

describe('<ContributorList>', () => {
    const props = {
        className: 'ContractContributors-list',
        contractPartyListLoading: false,
        contractContributors: [],
        noContributorsFoundText: <div>{NO_CONTRIBUTORS_FOUND}</div>,
        contributorsListHeader: CONTRIBUTORS_HEADERS,
    };
    const contractContributors = contractPartyList.abacusContractParties.items;
    const render = (props: any) =>
        renderInAppContext(<ContributorsList {...props} />, {
            identity: createIdentity({ profileType: ABACUS_PROFILE }),
        });

    it('shows a message when no contributors have been added', () => {
        render(props);
        expect(screen.getByText(NO_CONTRIBUTORS_FOUND)).toBeDefined();
    });

    it('renders table headers', () => {
        render(props);
        const headersText = screen
            .getAllByTestId('tableBasicHeaderCellTestId')
            .map((th: any) => th.textContent);
        expect(headersText).toEqual(CONTRIBUTORS_HEADERS);
    });

    it('renders a list of contract contributors', () => {
        render({ ...props, contractContributors: contractContributors });
        const bodyText = screen
            .getAllByTestId('tableBasicBodyCellTestId')
            .map(td => td.textContent);
        expect(bodyText).toEqual([
            'Thomas Edward Yorke3be31516-bde0-4968-9fcf-421ebfc40a8f',
            'Test Schedule - (1)',
            '',
            'Bell, Robert E582f9577-73cb-4885-b8eb-c2ce70ac9ecb',
            'Test Schedule 3 - (2)Auto add newTest Schedule 2 - (1)',
            '',
            'Haim, Este Arielle9ad6e907-ce55-4429-ba7e-70fb90391f8f',
            'No Schedules',
            '',
        ]);

        const deleteIcons = screen.getAllByTestId('TrashGlyphIcon');
        expect(deleteIcons.length).toEqual(3);
    });

    it('renders links to contributor detail page', () => {
        render({ ...props, contractContributors: contractContributors });
        const links = screen.getAllByRole('link');
        expect(links[0].getAttribute('href')).toContain(
            `/contributor/${contractContributors[0]['contractPartyObject']['id']}`
        );
        expect(links[1].getAttribute('href')).toContain(
            `/contributor/${contractContributors[1]['contractPartyObject']['id']}`
        );
    });

    it('calls setDeletedContractParty state on click of delete icon', () => {
        const newProps = {
            ...props,
            addingContributorId: '',
            contractContributors: contractContributors,
            setDeletedContractParty: jest.fn(),
        };
        render(newProps);
        const bodyText = screen
            .getAllByTestId('tableBasicBodyCellTestId')
            .map(td => td.textContent);
        expect(bodyText).toEqual([
            'Thomas Edward Yorke3be31516-bde0-4968-9fcf-421ebfc40a8f',
            'Test Schedule - (1)',
            '',
            'Bell, Robert E582f9577-73cb-4885-b8eb-c2ce70ac9ecb',
            'Test Schedule 3 - (2)Auto add newTest Schedule 2 - (1)',
            '',
            'Haim, Este Arielle9ad6e907-ce55-4429-ba7e-70fb90391f8f',
            'No Schedules',
            '',
        ]);

        const deleteIcons = screen.getAllByTestId('TrashGlyphIcon');
        fireEvent.click(deleteIcons[0]);
        expect(newProps.setDeletedContractParty).toHaveBeenCalled();
    });

    it('disables delete icons of existing contract contributors while adding a new contributor to contract', () => {
        const newProps = {
            ...props,
            addingContributorId: '6571-232-121-1',
            contractContributors: contractContributors,
            setDeletedContractParty: jest.fn(),
        };

        render(newProps);
        const bodyText = screen
            .getAllByTestId('tableBasicBodyCellTestId')
            .map(td => td.textContent);
        expect(bodyText).toEqual([
            'Thomas Edward Yorke3be31516-bde0-4968-9fcf-421ebfc40a8f',
            'Test Schedule - (1)',
            '',
            'Bell, Robert E582f9577-73cb-4885-b8eb-c2ce70ac9ecb',
            'Test Schedule 3 - (2)Auto add newTest Schedule 2 - (1)',
            '',
            'Haim, Este Arielle9ad6e907-ce55-4429-ba7e-70fb90391f8f',
            'No Schedules',
            '',
        ]);

        const deleteIcons = screen.getAllByTestId('remove-contributor');
        deleteIcons.forEach(deleteIcon => {
            expect(deleteIcon.classList).toContain('disabled');
        });
    });
});
