hovercard.spec.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import {act, render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import {Hovercard} from 'sentry/components/hovercard';
  3. describe('Hovercard', () => {
  4. beforeEach(() => {
  5. jest.useFakeTimers();
  6. jest.clearAllMocks();
  7. });
  8. afterEach(() => {
  9. jest.useRealTimers();
  10. });
  11. it('Displays card', async () => {
  12. render(
  13. <Hovercard
  14. position="top"
  15. body="Hovercard Body"
  16. header="Hovercard Header"
  17. displayTimeout={0}
  18. >
  19. Hovercard Trigger
  20. </Hovercard>
  21. );
  22. userEvent.hover(screen.getByText('Hovercard Trigger'));
  23. expect(await screen.findByText(/Hovercard Body/)).toBeInTheDocument();
  24. expect(await screen.findByText(/Hovercard Header/)).toBeInTheDocument();
  25. });
  26. it('Does not display card', () => {
  27. render(
  28. <Hovercard
  29. position="top"
  30. body="Hovercard Body"
  31. header="Hovercard Header"
  32. displayTimeout={0}
  33. forceVisible={false}
  34. >
  35. Hovercard Trigger
  36. </Hovercard>
  37. );
  38. userEvent.hover(screen.getByText('Hovercard Trigger'));
  39. act(() => void jest.runAllTimers());
  40. expect(screen.queryByText(/Hovercard Body/)).not.toBeInTheDocument();
  41. expect(screen.queryByText(/Hovercard Header/)).not.toBeInTheDocument();
  42. });
  43. it('Always displays card', () => {
  44. render(
  45. <Hovercard
  46. position="top"
  47. body="Hovercard Body"
  48. header="Hovercard Header"
  49. displayTimeout={0}
  50. forceVisible
  51. >
  52. Hovercard Trigger
  53. </Hovercard>
  54. );
  55. expect(screen.getByText(/Hovercard Body/)).toBeInTheDocument();
  56. expect(screen.getByText(/Hovercard Header/)).toBeInTheDocument();
  57. });
  58. it('Respects displayTimeout displays card', async () => {
  59. const DISPLAY_TIMEOUT = 100;
  60. render(
  61. <Hovercard
  62. position="top"
  63. body="Hovercard Body"
  64. header="Hovercard Header"
  65. delay={DISPLAY_TIMEOUT}
  66. displayTimeout={DISPLAY_TIMEOUT}
  67. >
  68. Hovercard Trigger
  69. </Hovercard>
  70. );
  71. userEvent.hover(screen.getByText('Hovercard Trigger'));
  72. act(() => void jest.advanceTimersByTime(DISPLAY_TIMEOUT - 1));
  73. expect(screen.queryByText(/Hovercard Body/)).not.toBeInTheDocument();
  74. expect(screen.queryByText(/Hovercard Header/)).not.toBeInTheDocument();
  75. expect(await screen.findByText(/Hovercard Body/)).toBeInTheDocument();
  76. expect(await screen.findByText(/Hovercard Header/)).toBeInTheDocument();
  77. });
  78. it('Doesnt leak timeout', () => {
  79. const DISPLAY_TIMEOUT = 100;
  80. render(
  81. <Hovercard
  82. position="top"
  83. body="Hovercard Body"
  84. header="Hovercard Header"
  85. delay={DISPLAY_TIMEOUT}
  86. displayTimeout={DISPLAY_TIMEOUT}
  87. >
  88. Hovercard Trigger
  89. </Hovercard>
  90. );
  91. userEvent.hover(screen.getByText('Hovercard Trigger'));
  92. act(() => void jest.advanceTimersByTime(DISPLAY_TIMEOUT - 1));
  93. expect(screen.queryByText(/Hovercard Body/)).not.toBeInTheDocument();
  94. expect(screen.queryByText(/Hovercard Header/)).not.toBeInTheDocument();
  95. userEvent.unhover(screen.getByText('Hovercard Trigger'));
  96. act(() => void jest.advanceTimersByTime(1));
  97. expect(screen.queryByText(/Hovercard Body/)).not.toBeInTheDocument();
  98. expect(screen.queryByText(/Hovercard Header/)).not.toBeInTheDocument();
  99. });
  100. });