import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { ExpandableSection, ExpandableSectionVariant } from '../ExpandableSection';
import styles from '@patternfly/react-styles/css/components/ExpandableSection/expandable-section';
import RhUiNotificationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-notification-fill-icon';
const props = { contentId: 'content-id', toggleId: 'toggle-id' };
test('ExpandableSection', () => {
const { asFragment } = render(test );
expect(asFragment()).toMatchSnapshot();
});
test('Renders ExpandableSection expanded', () => {
const { asFragment } = render(
{' '}
test{' '}
);
expect(asFragment()).toMatchSnapshot();
});
test('Calls onToggle when clicked', async () => {
const mockfn = jest.fn();
const user = userEvent.setup();
render( test );
await user.click(screen.getByRole('button'));
expect(mockfn.mock.calls).toHaveLength(1);
});
test('Does not call onToggle when not clicked', async () => {
const mockfn = jest.fn();
const user = userEvent.setup();
render(
<>
test
>
);
await user.click(screen.getByRole('button', { name: 'Test clicker' }));
expect(mockfn).not.toHaveBeenCalled();
});
test('Renders Uncontrolled ExpandableSection', () => {
const { asFragment } = render(
{' '}
test{' '}
);
expect(asFragment()).toMatchSnapshot();
});
test('Disclosure ExpandableSection', () => {
const { asFragment } = render(
test{' '}
);
expect(asFragment()).toMatchSnapshot();
});
test('Renders ExpandableSection indented', () => {
const { asFragment } = render(
{' '}
test{' '}
);
expect(asFragment()).toMatchSnapshot();
});
test(`Does not render with ${styles.modifiers.truncate} class when variant is not passed`, () => {
render(test);
expect(screen.getByText('test').parentElement).not.toHaveClass(styles.modifiers.truncate);
});
test(`Does not render with ${styles.modifiers.truncate} class when variant is not truncate`, () => {
render(test);
expect(screen.getByText('test').parentElement).not.toHaveClass(styles.modifiers.truncate);
});
test(`Renders with ${styles.modifiers.truncate} class when variant is truncate`, () => {
render(test);
expect(screen.getByText('test').parentElement).toHaveClass(styles.modifiers.truncate);
});
test('Renders with value passed to contentId', () => {
render(
Test
);
const wrapper = screen.getByTestId('test-id');
const content = wrapper.querySelector('#custom-id');
expect(content).toBeInTheDocument();
});
test('Renders with value passed to toggleId', () => {
render(
Test
);
const wrapper = screen.getByTestId('test-id');
const toggle = wrapper.querySelector('#custom-id');
expect(toggle).toBeVisible();
});
test('Renders with ARIA attributes when contentId and toggleId are passed', () => {
render(
Test
);
const wrapper = screen.getByTestId('test-id');
expect(wrapper).toContainHTML('aria-labelledby="toggle-id"');
expect(wrapper).toContainHTML('aria-controls="content-id"');
});
test(`Does not render with classes ${styles.modifiers.expandTop} nor ${styles.modifiers.expandBottom} by default`, () => {
render(Test content);
expect(screen.getByText('Test content').parentElement).not.toHaveClass('pf-m-expand-top');
expect(screen.getByText('Test content').parentElement).not.toHaveClass('pf-m-expand-bottom');
});
test(`Does not render with classes ${styles.modifiers.expandTop} nor ${styles.modifiers.expandBottom} when only isDetached is true`, () => {
render(Test content);
expect(screen.getByText('Test content').parentElement).not.toHaveClass('pf-m-expand-top');
expect(screen.getByText('Test content').parentElement).not.toHaveClass('pf-m-expand-bottom');
});
test(`Does not render with class ${styles.modifiers.expandTop} when direction="up" and isDetached is false`, () => {
render(Test content);
expect(screen.getByText('Test content').parentElement).not.toHaveClass('pf-m-expand-top');
});
test(`Does not render with class ${styles.modifiers.expandBottom} when direction="down" and isDetached is false`, () => {
render(Test content);
expect(screen.getByText('Test content').parentElement).not.toHaveClass('pf-m-expand-bottom');
});
test(`Renders with class ${styles.modifiers.expandTop} when isDetached is true and direction="up"`, () => {
render(
Test content
);
expect(screen.getByText('Test content').parentElement).toHaveClass('pf-m-expand-top');
});
test(`Renders with class ${styles.modifiers.expandBottom} when isDetached is true and direction="down"`, () => {
render(
Test content
);
expect(screen.getByText('Test content').parentElement).toHaveClass('pf-m-expand-bottom');
});
test('Does not render with class pf-m-detached when isDetached is true and direction is not passed', () => {
render(Test content);
expect(screen.getByText('Test content').parentElement).not.toHaveClass('pf-m-detached');
});
test('Renders with class pf-m-detached when isDetached is true and direction is passed', () => {
render(
Test content
);
expect(screen.getByText('Test content').parentElement).toHaveClass('pf-m-detached');
});
test('Renders with aria-label when toggleAriaLabel is passed', () => {
render();
expect(screen.getByRole('button')).toHaveAccessibleName('Test label');
});
test('Renders with aria-labelledby when toggleAriaLabelledBy is passed', () => {
render(
<>
Test label
>
);
expect(screen.getByRole('button')).toHaveAccessibleName('Test label');
});
test('Renders toggleContent as a function in uncontrolled mode (collapsed)', () => {
render(
(isExpanded ? 'Hide details' : 'Show details')}>
Test content
);
expect(screen.getByRole('button', { name: 'Show details' })).toBeInTheDocument();
});
test('Renders toggleContent as a function in uncontrolled mode (expanded after click)', async () => {
const user = userEvent.setup();
render(
(isExpanded ? 'Hide details' : 'Show details')}>
Test content
);
const button = screen.getByRole('button', { name: 'Show details' });
await user.click(button);
expect(screen.getByRole('button', { name: 'Hide details' })).toBeInTheDocument();
});
test('Renders toggleContent as a function in controlled mode', () => {
render(
(isExpanded ? 'Collapse' : 'Expand')}>
Test content
);
expect(screen.getByRole('button', { name: 'Collapse' })).toBeInTheDocument();
});
test('Renders with default div wrapper when toggleWrapper is not specified', () => {
render(Test content);
const toggle = screen.getByRole('button').parentElement;
expect(toggle?.tagName).toBe('DIV');
});
test('Renders with h2 wrapper when toggleWrapper="h2"', () => {
render(
Test content
);
expect(screen.getByRole('heading', { level: 2 })).toBeInTheDocument();
});
test('Renders with div wrapper when toggleWrapper="div"', () => {
render(
Test content
);
const toggle = screen.getByRole('button').parentElement;
expect(toggle?.tagName).toBe('DIV');
});
test('Can render custom toggle icon', () => {
render(
}>
Test content
);
expect(screen.getByTestId('bell-icon')).toBeInTheDocument();
});
test('Does not render toggle icon when hasToggleIcon is false', () => {
render(Test content);
const button = screen.getByRole('button');
expect(button.querySelector('.pf-v6-c-expandable-section__toggle-icon')).not.toBeInTheDocument();
});
test('Does not render custom toggle icon when hasToggleIcon is false', () => {
render(
} hasToggleIcon={false}>
Test content
);
expect(screen.queryByTestId('bell-icon')).not.toBeInTheDocument();
});
test('Renders toggle icon by default when hasToggleIcon is true', () => {
render(Test content);
const button = screen.getByRole('button');
expect(button.querySelector('.pf-v6-c-expandable-section__toggle-icon')).toBeInTheDocument();
});