tooltip.spec.tsx 3.1 KB

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