import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import * as React from 'react';
import { Expander } from '../Expander';
import { ExpanderItem } from '../ExpanderItem';
import { ExpanderProps } from '../../types/expander';
import { ComponentClassNames } from '../../shared/constants';
function UncontrolledExpander(props: ExpanderProps) {
return (
content 1
content 2
content 3
);
}
function ControlledExpander({ value: initialValue, ...rest }: ExpanderProps) {
const [value, setValue] = React.useState(initialValue);
return (
content 1
content 2
content 3
);
}
describe('Expander', () => {
it('should set default and custom classnames', async () => {
const testId = 'expander';
const className = 'class-test';
render(
);
const expander = await screen.findByTestId(testId);
expect(expander).toHaveClass(ComponentClassNames.Expander, className);
});
it('should be collapsible', async () => {
render(
);
const buttons = await screen.findAllByRole('button');
expect(buttons[0]).toHaveAttribute('aria-expanded', 'true');
userEvent.click(buttons[0]);
expect(buttons[0]).toHaveAttribute('aria-expanded', 'false');
});
it('should work as uncontrolled component with type single', async () => {
render();
const buttons = await screen.findAllByRole('button');
expect(buttons[0]).toHaveAttribute('aria-expanded', 'true');
expect(buttons[1]).toHaveAttribute('aria-expanded', 'false');
expect(buttons[2]).toHaveAttribute('aria-expanded', 'false');
userEvent.click(buttons[1]);
expect(buttons[0]).toHaveAttribute('aria-expanded', 'false');
expect(buttons[1]).toHaveAttribute('aria-expanded', 'true');
expect(buttons[2]).toHaveAttribute('aria-expanded', 'false');
userEvent.click(buttons[2]);
expect(buttons[0]).toHaveAttribute('aria-expanded', 'false');
expect(buttons[1]).toHaveAttribute('aria-expanded', 'false');
expect(buttons[2]).toHaveAttribute('aria-expanded', 'true');
});
it('should work as uncontrolled component with type multiple', async () => {
render();
const buttons = await screen.findAllByRole('button');
expect(buttons[0]).toHaveAttribute('aria-expanded', 'true');
expect(buttons[1]).toHaveAttribute('aria-expanded', 'false');
expect(buttons[2]).toHaveAttribute('aria-expanded', 'false');
userEvent.click(buttons[1]);
expect(buttons[0]).toHaveAttribute('aria-expanded', 'true');
expect(buttons[1]).toHaveAttribute('aria-expanded', 'true');
expect(buttons[2]).toHaveAttribute('aria-expanded', 'false');
userEvent.click(buttons[2]);
expect(buttons[0]).toHaveAttribute('aria-expanded', 'true');
expect(buttons[1]).toHaveAttribute('aria-expanded', 'true');
expect(buttons[2]).toHaveAttribute('aria-expanded', 'true');
});
it('should work as controlled component with type single', async () => {
render();
const buttons = await screen.findAllByRole('button');
expect(buttons[0]).toHaveAttribute('aria-expanded', 'true');
expect(buttons[1]).toHaveAttribute('aria-expanded', 'false');
expect(buttons[2]).toHaveAttribute('aria-expanded', 'false');
userEvent.click(buttons[1]);
expect(buttons[0]).toHaveAttribute('aria-expanded', 'false');
expect(buttons[1]).toHaveAttribute('aria-expanded', 'true');
expect(buttons[2]).toHaveAttribute('aria-expanded', 'false');
userEvent.click(buttons[2]);
expect(buttons[0]).toHaveAttribute('aria-expanded', 'false');
expect(buttons[1]).toHaveAttribute('aria-expanded', 'false');
expect(buttons[2]).toHaveAttribute('aria-expanded', 'true');
});
it('should work as controlled component with type multiple', async () => {
render();
const buttons = await screen.findAllByRole('button');
expect(buttons[0]).toHaveAttribute('aria-expanded', 'true');
expect(buttons[1]).toHaveAttribute('aria-expanded', 'false');
expect(buttons[2]).toHaveAttribute('aria-expanded', 'false');
userEvent.click(buttons[1]);
expect(buttons[0]).toHaveAttribute('aria-expanded', 'true');
expect(buttons[1]).toHaveAttribute('aria-expanded', 'true');
expect(buttons[2]).toHaveAttribute('aria-expanded', 'false');
userEvent.click(buttons[2]);
expect(buttons[0]).toHaveAttribute('aria-expanded', 'true');
expect(buttons[1]).toHaveAttribute('aria-expanded', 'true');
expect(buttons[2]).toHaveAttribute('aria-expanded', 'true');
});
it('should forward ref support on DOM element', async () => {
const testId = 'expander';
const ref = React.createRef();
render(
content 1
content 2
content 3
);
await screen.findByTestId(testId);
expect(ref.current?.nodeName).toBe('DIV');
});
});