tooltip.spec.jsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {
  2. mountWithTheme,
  3. screen,
  4. userEvent,
  5. waitForElementToBeRemoved,
  6. } from 'sentry-test/reactTestingLibrary';
  7. import Tooltip from 'sentry/components/tooltip';
  8. import * as utils from 'sentry/utils/tooltip';
  9. describe('Tooltip', function () {
  10. beforeEach(() => {
  11. jest.clearAllMocks();
  12. });
  13. it('renders', function () {
  14. const {container} = mountWithTheme(
  15. <Tooltip delay={0} title="test">
  16. <span>My Button</span>
  17. </Tooltip>
  18. );
  19. expect(container).toSnapshot();
  20. });
  21. it('updates title', async function () {
  22. const {rerender} = mountWithTheme(
  23. <Tooltip delay={0} title="test">
  24. <span>My Button</span>
  25. </Tooltip>
  26. );
  27. // Change title
  28. rerender(
  29. <Tooltip delay={0} title="bar">
  30. <span>My Button</span>
  31. </Tooltip>
  32. );
  33. userEvent.hover(screen.getByText('My Button'));
  34. expect(screen.getByText('bar')).toBeInTheDocument();
  35. userEvent.unhover(screen.getByText('My Button'));
  36. await waitForElementToBeRemoved(() => screen.queryByText('bar'));
  37. });
  38. it('disables and does not render', function () {
  39. mountWithTheme(
  40. <Tooltip delay={0} title="test" disabled>
  41. <span>My Button</span>
  42. </Tooltip>
  43. );
  44. userEvent.hover(screen.getByText('My Button'));
  45. expect(screen.queryByText('test')).not.toBeInTheDocument();
  46. userEvent.unhover(screen.getByText('My Button'));
  47. });
  48. it('does not render an empty tooltip', function () {
  49. mountWithTheme(
  50. <Tooltip delay={0} title="">
  51. <span>My Button</span>
  52. </Tooltip>
  53. );
  54. userEvent.hover(screen.getByText('My Button'));
  55. expect(screen.getByText('My Button')).not.toHaveAttribute('aria-describedby');
  56. userEvent.unhover(screen.getByText('My Button'));
  57. });
  58. it('displays a tooltip if the content overflows with showOnlyOnOverflow', async function () {
  59. // Mock this to return true because scrollWidth and clientWidth are 0 in JSDOM
  60. jest.spyOn(utils, 'isOverflown').mockReturnValue(true);
  61. mountWithTheme(
  62. <Tooltip delay={0} title="test" showOnlyOnOverflow>
  63. <div>This text overflows</div>
  64. </Tooltip>
  65. );
  66. userEvent.hover(screen.getByText('This text overflows'));
  67. expect(screen.getByText('test')).toBeInTheDocument();
  68. userEvent.unhover(screen.getByText('This text overflows'));
  69. });
  70. it('does not display a tooltip if the content does not overflow with showOnlyOnOverflow', function () {
  71. jest.spyOn(utils, 'isOverflown').mockReturnValue(false);
  72. mountWithTheme(
  73. <Tooltip delay={0} title="test" showOnlyOnOverflow>
  74. <div>This text does not overflow</div>
  75. </Tooltip>
  76. );
  77. userEvent.hover(screen.getByText('This text does not overflow'));
  78. expect(screen.queryByText('test')).not.toBeInTheDocument();
  79. });
  80. });