import React from 'react';
import { render } from '@testing-library/react';
import * as suiteIdentity from '@theorchard/suite-identity';
import { testComponent } from 'lib/test-utils/common';
import { mockIdentity, mockHandsOffResource } from '../../../__mocks__';
import { ResourceCard, CLASS_NAME } from '../resourceCard';
import type { BrandResource } from '../../../types';

vi.mock('@theorchard/suite-identity');

describe('<ResourceCard>', () => {
    const renderComponent = (resource: BrandResource) =>
        render(<ResourceCard resource={resource} />);

    beforeAll(() => {
        vi.spyOn(suiteIdentity, 'useIdentity').mockReturnValue(mockIdentity);
    });

    testComponent(ResourceCard, { resource: mockHandsOffResource }, CLASS_NAME);

    test('renders correctly', () => {
        const { container, getByTestId, queryByTestId, getByRole } =
            renderComponent(mockHandsOffResource);

        expect(getByTestId(CLASS_NAME)).toBeVisible();
        expect(getByRole('heading', { level: 4 })).toHaveTextContent(
            'HANDS OFF TEST ACCOUNT (Jacob Fowler) · 25824'
        );
        expect(container.getElementsByClassName(`${CLASS_NAME}-type`)[0]).toHaveTextContent(
            'Account'
        );
        expect(queryByTestId('InternalUserCard')).not.toBeInTheDocument();
    });

    test('renders InternalUserCard when its an employee', () => {
        vi.spyOn(suiteIdentity, 'useIdentity').mockReturnValue({
            ...mockIdentity,
            isEmployee: true,
        });

        const { getByText, getByTestId } = renderComponent(mockHandsOffResource);

        expect(getByTestId('InternalUserCard')).toBeVisible();

        expect(getByText('The Orchard')).toBeVisible();
        expect(getByText('Untiered')).toBeVisible();
    });
});
