/* eslint-disable jest/require-top-level-describe */
import { render, fireEvent } from '@testing-library/react';
import React, { useState } from 'react';
import { Popover } from './popover';
import { PopoverPosition } from './popover.types';
describe('Popover', () => {
it('should render popover element correctly', () => {
const { getByTestId, getByText, container } = render(
Popover
,
);
expect(getByText('Popover')).toBeDefined();
expect(container.querySelector('popover')).toBeDefined();
expect(getByTestId('popover')).toHaveClass('mm-popover');
expect(container).toMatchSnapshot();
});
it('should NOT render popover', () => {
const { queryByTestId } = render(
<>
Popover not open
>,
);
expect(queryByTestId('popover')).not.toBeInTheDocument();
});
it('should render popover children', () => {
const { getByText } = render(
Popover content goes here,
);
expect(getByText('Popover content goes here')).toBeDefined();
});
it('should render popover position', () => {
const { getByTestId } = render(
<>
{PopoverPosition.Auto}
{PopoverPosition.Top}
{PopoverPosition.TopStart}
{PopoverPosition.TopEnd}
{PopoverPosition.Right}
{PopoverPosition.RightStart}
{PopoverPosition.RightEnd}
{PopoverPosition.Bottom}
{PopoverPosition.BottomStart}
{PopoverPosition.BottomEnd}
{PopoverPosition.Left}
{PopoverPosition.LeftStart}
{PopoverPosition.LeftEnd}
>,
);
expect(getByTestId(PopoverPosition.Auto)).toBeDefined();
expect(getByTestId(PopoverPosition.Top)).toBeDefined();
expect(getByTestId(PopoverPosition.TopStart)).toBeDefined();
expect(getByTestId(PopoverPosition.TopEnd)).toBeDefined();
expect(getByTestId(PopoverPosition.Right)).toBeDefined();
expect(getByTestId(PopoverPosition.RightStart)).toBeDefined();
expect(getByTestId(PopoverPosition.RightEnd)).toBeDefined();
expect(getByTestId(PopoverPosition.Bottom)).toBeDefined();
expect(getByTestId(PopoverPosition.BottomStart)).toBeDefined();
expect(getByTestId(PopoverPosition.BottomEnd)).toBeDefined();
expect(getByTestId(PopoverPosition.Left)).toBeDefined();
expect(getByTestId(PopoverPosition.LeftStart)).toBeDefined();
expect(getByTestId(PopoverPosition.LeftEnd)).toBeDefined();
});
it('should render an arrow on popover', () => {
const { getByTestId } = render(
Popover
,
);
const arrowElement =
getByTestId('popover').querySelector('.mm-popover__arrow');
expect(arrowElement).toHaveClass('mm-popover__arrow');
});
});
test('should render Popover with isPortal set to false', () => {
const { getByTestId } = render(
,
);
// Check that the Popover is rendered inside the body DOM
expect(getByTestId('popover')).toBeInTheDocument();
});
test('should render Popover with isPortal set to true', () => {
const { getByTestId } = render(
,
);
expect(getByTestId('popover')).toBeTruthy();
});
test('should add reference-hidden classname when referenceHidden prop is true', () => {
const { getByTestId } = render(
,
);
expect(getByTestId('popover')).toHaveClass('mm-popover--reference-hidden');
});
const EscKeyTestComponent = () => {
const [isOpen, setIsOpen] = useState(true);
return (
setIsOpen(false)}
>
Press esc key to close
);
};
test('Press esc key to close fires', () => {
// Render the component
const { getByText, queryByText } = render();
// Assert that the popover is initially visible
expect(getByText('Press esc key to close')).toBeVisible();
// Trigger the "Escape" key press event
fireEvent.keyDown(document, { key: 'Escape' });
// Assert that the popover closes
expect(queryByText('Press esc key to close')).not.toBeInTheDocument();
});