import React from 'react';
import { screen } from '@testing-library/react';
import { createIdentity, renderInAppContext } from '@theorchard/suite-testing';
import { featureArrayToDic } from 'lib/test/features';
import { CREATE_CONTRIBUTOR_FF } from 'src/constants';
import IdLookupSidecar, { Props } from '../idLookupSidecar';

describe('<IdLookupSidecar>', () => {
    const defaultProps: Props = {
        open: true,
        closeSidecar: jest.fn(),
    };

    const renderWrapper = (props?: Props, featureNames?: string[]) => {
        const identity = createIdentity({
            features: featureArrayToDic(featureNames),
        });

        renderInAppContext(<IdLookupSidecar {...defaultProps} {...props} />, {
            identity,
        });
    };

    test('displays contributor search & participant search', async () => {
        renderWrapper();

        const dropdown = await screen.findByTestId(
            'Select_contributorSearchDropdown'
        );
        const participantDropdown = await screen.findByTestId(
            'IdLookupSidecar-participant-lookup'
        );

        expect(dropdown).toBeTruthy();
        expect(participantDropdown).toBeTruthy();
    });

    describe('when content_app_create_nr_contributor FF is ON', () => {
        const identityFeatures = [CREATE_CONTRIBUTOR_FF];

        test('renders create contributor component', async () => {
            renderWrapper(defaultProps, identityFeatures);

            const createContributor = await screen.findByTestId(
                'CreateContributorModal'
            );

            expect(createContributor).toBeTruthy();
        });
    });

    describe('when content_app_create_nr_contributor FF is off', () => {
        test('does not render create contributor component', () => {
            renderWrapper();

            const createContributor = screen.queryByTestId(
                'CreateContributorModal'
            );

            expect(createContributor).not.toBeTruthy();
        });
    });
});
