You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

positioning-test.ts 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { PopupPlacement, popupPositioning } from '../positioning';
  21. const toggleRect = {
  22. getBoundingClientRect: jest.fn().mockReturnValue({
  23. left: 400,
  24. top: 200,
  25. width: 50,
  26. height: 20,
  27. }),
  28. } as any;
  29. const popupRect = {
  30. getBoundingClientRect: jest.fn().mockReturnValue({
  31. width: 200,
  32. height: 100,
  33. }),
  34. } as any;
  35. beforeAll(() => {
  36. Object.defineProperties(document.documentElement, {
  37. clientWidth: {
  38. configurable: true,
  39. value: 1000,
  40. },
  41. clientHeight: {
  42. configurable: true,
  43. value: 1000,
  44. },
  45. });
  46. });
  47. it('should calculate positioning based on placement', () => {
  48. const fixes = { leftFix: 0, topFix: 0 };
  49. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.Bottom)).toMatchObject({
  50. left: 325,
  51. top: 220,
  52. ...fixes,
  53. });
  54. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.BottomLeft)).toMatchObject({
  55. left: 400,
  56. top: 220,
  57. ...fixes,
  58. });
  59. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.BottomRight)).toMatchObject({
  60. left: 250,
  61. top: 220,
  62. ...fixes,
  63. });
  64. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.Top)).toMatchObject({
  65. left: 325,
  66. top: 100,
  67. ...fixes,
  68. });
  69. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.TopLeft)).toMatchObject({
  70. left: 400,
  71. top: 100,
  72. ...fixes,
  73. });
  74. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.TopRight)).toMatchObject({
  75. left: 250,
  76. top: 100,
  77. ...fixes,
  78. });
  79. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.Left)).toMatchObject({
  80. left: 200,
  81. top: 160,
  82. ...fixes,
  83. });
  84. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.LeftBottom)).toMatchObject({
  85. left: 200,
  86. top: 120,
  87. ...fixes,
  88. });
  89. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.LeftTop)).toMatchObject({
  90. left: 200,
  91. top: 200,
  92. ...fixes,
  93. });
  94. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.Right)).toMatchObject({
  95. left: 450,
  96. top: 160,
  97. ...fixes,
  98. });
  99. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.RightBottom)).toMatchObject({
  100. left: 450,
  101. top: 120,
  102. ...fixes,
  103. });
  104. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.RightTop)).toMatchObject({
  105. left: 450,
  106. top: 200,
  107. ...fixes,
  108. });
  109. });
  110. it('should position the element in the boundaries of the screen', () => {
  111. toggleRect.getBoundingClientRect.mockReturnValueOnce({
  112. left: 0,
  113. top: 850,
  114. width: 50,
  115. height: 50,
  116. });
  117. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.Bottom)).toMatchObject({
  118. left: 4,
  119. leftFix: 79,
  120. top: 896,
  121. topFix: -4,
  122. });
  123. toggleRect.getBoundingClientRect.mockReturnValueOnce({
  124. left: 900,
  125. top: 0,
  126. width: 50,
  127. height: 50,
  128. });
  129. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.Top)).toMatchObject({
  130. left: 796,
  131. leftFix: -29,
  132. top: 4,
  133. topFix: 104,
  134. });
  135. });
  136. it('should position the element outside the boundaries of the screen when the toggle is outside', () => {
  137. toggleRect.getBoundingClientRect.mockReturnValueOnce({
  138. left: -100,
  139. top: 1100,
  140. width: 50,
  141. height: 50,
  142. });
  143. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.Bottom)).toMatchObject({
  144. left: -75,
  145. leftFix: 100,
  146. top: 1025,
  147. topFix: -125,
  148. });
  149. toggleRect.getBoundingClientRect.mockReturnValueOnce({
  150. left: 1500,
  151. top: -200,
  152. width: 50,
  153. height: 50,
  154. });
  155. expect(popupPositioning(toggleRect, popupRect, PopupPlacement.Top)).toMatchObject({
  156. left: 1325,
  157. leftFix: -100,
  158. top: -175,
  159. topFix: 125,
  160. });
  161. });