import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
import Button from 'sentry/components/button';
import Collapsible from 'sentry/components/collapsible';
const items = [1, 2, 3, 4, 5, 6, 7].map(i =>
Item {i}
);
describe('Collapsible', function () {
it('collapses items', function () {
render({items});
expect(screen.getAllByText(/Item/)).toHaveLength(5);
expect(screen.getAllByText(/Item/)[2].innerHTML).toBe('Item 3');
expect(screen.getByLabelText('Show 2 hidden items')).toBeInTheDocument();
expect(screen.queryByLabelText('Collapse')).not.toBeInTheDocument();
});
it('expands items', function () {
render({items});
// expand
userEvent.click(screen.getByLabelText('Show 2 hidden items'));
expect(screen.getAllByText(/Item/)).toHaveLength(7);
// collapse back
userEvent.click(screen.getByLabelText('Collapse'));
expect(screen.getAllByText(/Item/)).toHaveLength(5);
});
it('respects maxVisibleItems prop', function () {
render({items});
expect(screen.getAllByText(/Item/)).toHaveLength(2);
});
it('does not collapse items below threshold', function () {
render({items});
expect(screen.getAllByText(/Item/)).toHaveLength(7);
expect(screen.queryByLabelText(/hidden item/)).not.toBeInTheDocument();
});
it('takes custom buttons', function () {
render(
(
)}
expandButton={({onExpand, numberOfCollapsedItems}) => (
)}
>
{items}
);
expect(screen.getByText(/Custom/)).toBeInTheDocument();
// custom expand
userEvent.click(screen.getByLabelText('Expand'));
expect(screen.getAllByText(/Item/)).toHaveLength(7);
// custom collapse back
userEvent.click(screen.getByText('Custom Collapse'));
expect(screen.getAllByText(/Item/)).toHaveLength(5);
});
});