import React from 'react';
import { render, screen } from '@testing-library/react';
import Banner from '../banner';

describe('Banner', () => {
    const Content = () => <div>Dummy content</div>;

    const renderComponent = (props?: Record<string, unknown>) =>
        render(
            <Banner cookieName="vanilla" dismissBanner={() => {}} {...props}>
                <Content />
            </Banner>
        );

    describe('when there is no cookie: [cookieName]=false', () => {
        test('should render the component', () => {
            renderComponent({ cookieName: 'test' });
            expect(screen.getByText('Dummy content')).toBeInTheDocument();
        });
    });

    describe('when there is a cookie: [cookieName]=false', () => {
        const cookieName = 'mybanner';

        beforeEach(() => {
            document.cookie = `${cookieName}=false`;
        });
        afterEach(() => {
            document.cookie = '';
        });

        test('should not render the component', () => {
            const { container } = renderComponent({ cookieName });
            expect(container.firstChild).toBeNull();
        });
    });

    test('can remove close', () => {
        renderComponent({ canClose: false });
        // CloseIcon renders an SVG, check the close button wrapper is not rendered
        expect(
            screen.queryByTestId('close-banner-button')
        ).not.toBeInTheDocument();
    });
});
