Toolltip.test.jsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { h, createRef } from 'preact';
  2. import Tooltip from '../Tooltip';
  3. import { render, screen } from '@testing-library/preact';
  4. describe('Tooltip', () => {
  5. test('renders in a relative position', async () => {
  6. jest
  7. .spyOn(window.HTMLElement.prototype, 'getBoundingClientRect')
  8. // relativeTo
  9. .mockReturnValueOnce({
  10. x: 100,
  11. y: 100,
  12. width: 50,
  13. height: 10,
  14. })
  15. // tooltip
  16. .mockReturnValueOnce({ width: 40, height: 15 });
  17. const ref = createRef();
  18. render(
  19. <div>
  20. <div ref={ref} />
  21. <Tooltip relativeTo={ref} text="hello" />
  22. </div>
  23. );
  24. const tooltip = await screen.findByRole('tooltip');
  25. const style = window.getComputedStyle(tooltip);
  26. expect(style.left).toEqual('105px');
  27. expect(style.top).toEqual('70px');
  28. });
  29. test('if too far right, renders to the left', async () => {
  30. window.innerWidth = 1024;
  31. jest
  32. .spyOn(window.HTMLElement.prototype, 'getBoundingClientRect')
  33. // relativeTo
  34. .mockReturnValueOnce({
  35. x: 1000,
  36. y: 100,
  37. width: 24,
  38. height: 10,
  39. })
  40. // tooltip
  41. .mockReturnValueOnce({ width: 50, height: 15 });
  42. const ref = createRef();
  43. render(
  44. <div>
  45. <div ref={ref} />
  46. <Tooltip relativeTo={ref} text="hello" />
  47. </div>
  48. );
  49. const tooltip = await screen.findByRole('tooltip');
  50. const style = window.getComputedStyle(tooltip);
  51. expect(style.left).toEqual('942px');
  52. expect(style.top).toEqual('97px');
  53. });
  54. test('if too far left, renders to the right', async () => {
  55. jest
  56. .spyOn(window.HTMLElement.prototype, 'getBoundingClientRect')
  57. // relativeTo
  58. .mockReturnValueOnce({
  59. x: 0,
  60. y: 100,
  61. width: 24,
  62. height: 10,
  63. })
  64. // tooltip
  65. .mockReturnValueOnce({ width: 50, height: 15 });
  66. const ref = createRef();
  67. render(
  68. <div>
  69. <div ref={ref} />
  70. <Tooltip relativeTo={ref} text="hello" />
  71. </div>
  72. );
  73. const tooltip = await screen.findByRole('tooltip');
  74. const style = window.getComputedStyle(tooltip);
  75. expect(style.left).toEqual('32px');
  76. expect(style.top).toEqual('97px');
  77. });
  78. test('if too close to top, renders to the bottom', async () => {
  79. window.scrollY = 90;
  80. jest
  81. .spyOn(window.HTMLElement.prototype, 'getBoundingClientRect')
  82. // relativeTo
  83. .mockReturnValueOnce({
  84. x: 100,
  85. y: 100,
  86. width: 24,
  87. height: 10,
  88. })
  89. // tooltip
  90. .mockReturnValueOnce({ width: 50, height: 15 });
  91. const ref = createRef();
  92. render(
  93. <div>
  94. <div ref={ref} />
  95. <Tooltip relativeTo={ref} text="hello" />
  96. </div>
  97. );
  98. const tooltip = await screen.findByRole('tooltip');
  99. const style = window.getComputedStyle(tooltip);
  100. expect(style.left).toEqual('87px');
  101. expect(style.top).toEqual('160px');
  102. });
  103. });