collapsible.spec.jsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import Button from 'sentry/components/button';
  3. import Collapsible from 'sentry/components/collapsible';
  4. const items = [1, 2, 3, 4, 5, 6, 7].map(i => <div key={i}>Item {i}</div>);
  5. describe('Collapsible', function () {
  6. it('collapses items', function () {
  7. render(<Collapsible>{items}</Collapsible>);
  8. expect(screen.getAllByText(/Item/)).toHaveLength(5);
  9. expect(screen.getAllByText(/Item/)[2].innerHTML).toBe('Item 3');
  10. expect(screen.getByLabelText('Show 2 hidden items')).toBeInTheDocument();
  11. expect(screen.queryByLabelText('Collapse')).not.toBeInTheDocument();
  12. });
  13. it('expands items', function () {
  14. render(<Collapsible>{items}</Collapsible>);
  15. // expand
  16. userEvent.click(screen.getByLabelText('Show 2 hidden items'));
  17. expect(screen.getAllByText(/Item/)).toHaveLength(7);
  18. // collapse back
  19. userEvent.click(screen.getByLabelText('Collapse'));
  20. expect(screen.getAllByText(/Item/)).toHaveLength(5);
  21. });
  22. it('respects maxVisibleItems prop', function () {
  23. render(<Collapsible maxVisibleItems={2}>{items}</Collapsible>);
  24. expect(screen.getAllByText(/Item/)).toHaveLength(2);
  25. });
  26. it('does not collapse items below threshold', function () {
  27. render(<Collapsible maxVisibleItems={100}>{items}</Collapsible>);
  28. expect(screen.getAllByText(/Item/)).toHaveLength(7);
  29. expect(screen.queryByLabelText(/hidden item/)).not.toBeInTheDocument();
  30. });
  31. it('takes custom buttons', function () {
  32. render(
  33. <Collapsible
  34. collapseButton={({onCollapse}) => (
  35. <Button onClick={onCollapse}>Custom Collapse</Button>
  36. )}
  37. expandButton={({onExpand, numberOfCollapsedItems}) => (
  38. <Button onClick={onExpand} aria-label="Expand">
  39. Custom Expand {numberOfCollapsedItems}
  40. </Button>
  41. )}
  42. >
  43. {items}
  44. </Collapsible>
  45. );
  46. expect(screen.getByText(/Custom/)).toBeInTheDocument();
  47. // custom expand
  48. userEvent.click(screen.getByLabelText('Expand'));
  49. expect(screen.getAllByText(/Item/)).toHaveLength(7);
  50. // custom collapse back
  51. userEvent.click(screen.getByText('Custom Collapse'));
  52. expect(screen.getAllByText(/Item/)).toHaveLength(5);
  53. });
  54. });