import React from 'react';
import { renderInAppContextLite } from 'lib/test-utils/render';
import { BannersContainer, CLASSNAME } from '../bannersContainer';
import type { Identity } from '@theorchard/suite-identity';

describe('<BannersContainer>', () => {
    const renderComponent = (identity?: Partial<Identity>) => {
        return renderInAppContextLite(<BannersContainer />, { identity });
    };

    beforeEach(() => {
        vi.clearAllMocks();
    });

    describe('render container', () => {
        test('should not render banners by default', () => {
            const { queryByTestId } = renderComponent();
            const component = queryByTestId(CLASSNAME);

            expect(component).toBeInTheDocument();
            expect(queryByTestId('ApplicationBanner')).not.toBeInTheDocument();
            expect(queryByTestId('NotificationBanner')).not.toBeInTheDocument();
        });

        test('should render impersonation banner', () => {
            const { getByTestId, getByText } = renderComponent({
                impersonatedBy: {
                    id: '123',
                    name: 'John Doe',
                },
            });

            expect(getByTestId('ImpersonationBanner')).toBeInTheDocument();
            expect(getByText('John Doe', { exact: false })).toBeInTheDocument();
        });
    });

    describe('ends impersonation', () => {
        test('should redirect to end impersonation url', () => {
            const { getByTestId } = renderComponent({
                impersonatedBy: {
                    id: '123',
                    name: 'John Doe',
                },
            });

            const endButton = getByTestId('ApplicationBanner-defaultBtn');

            expect(endButton).toBeInTheDocument();

            endButton.click();

            expect(window.location.assign).toHaveBeenCalledWith('/?impersonate=end');
        });
    });
});
