collapsible.spec.jsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import Button from 'app/components/button';
  3. import Collapsible from 'app/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. const wrapper = mountWithTheme(<Collapsible>{items}</Collapsible>);
  8. expect(wrapper.find('div').length).toBe(5);
  9. expect(wrapper.find('div').at(2).text()).toBe('Item 3');
  10. expect(wrapper.find('button[aria-label="Show 2 hidden items"]').exists()).toBe(true);
  11. expect(wrapper.find('button[aria-label="Collapse"]').exists()).toBeFalsy();
  12. });
  13. it('expands items', function () {
  14. const wrapper = mountWithTheme(<Collapsible>{items}</Collapsible>);
  15. // expand
  16. wrapper.find('button[aria-label="Show 2 hidden items"]').simulate('click');
  17. expect(wrapper.find('div').length).toBe(7);
  18. // collapse back
  19. wrapper.find('button[aria-label="Collapse"]').simulate('click');
  20. expect(wrapper.find('div').length).toBe(5);
  21. });
  22. it('respects maxVisibleItems prop', function () {
  23. const wrapper = mountWithTheme(
  24. <Collapsible maxVisibleItems={2}>{items}</Collapsible>
  25. );
  26. expect(wrapper.find('div').length).toBe(2);
  27. });
  28. it('does not collapse items below threshold', function () {
  29. const wrapper = mountWithTheme(
  30. <Collapsible maxVisibleItems={100}>{items}</Collapsible>
  31. );
  32. expect(wrapper.find('div').length).toBe(7);
  33. expect(wrapper.find('button').exists()).toBeFalsy();
  34. });
  35. it('takes custom buttons', function () {
  36. const wrapper = mountWithTheme(
  37. <Collapsible
  38. collapseButton={({onCollapse}) => (
  39. <Button onClick={onCollapse}>Custom Collapse</Button>
  40. )}
  41. expandButton={({onExpand, numberOfCollapsedItems}) => (
  42. <Button onClick={onExpand} aria-label="Expand">
  43. Custom Expand {numberOfCollapsedItems}
  44. </Button>
  45. )}
  46. >
  47. {items}
  48. </Collapsible>
  49. );
  50. expect(wrapper.find('button').length).toBe(1);
  51. // custom expand
  52. wrapper.find('button[aria-label="Expand"]').simulate('click');
  53. expect(wrapper.find('div').length).toBe(7);
  54. // custom collapse back
  55. wrapper.find('button[aria-label="Custom Collapse"]').simulate('click');
  56. expect(wrapper.find('div').length).toBe(5);
  57. });
  58. });