collapsible.spec.jsx 2.4 KB

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