ScrollableBodySpec.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. describe("Scrollable body test", function() {
  2. var testSlider;
  3. var sliderHandleTopPos;
  4. var sliderHandleLeftPos;
  5. describe("Vertical scrolled body", function() {
  6. beforeEach(function() {
  7. testSlider = new Slider("#veryLowPositionedSlider", {
  8. id: "scrollTestSliderId",
  9. orientation: "vertical",
  10. min: 0,
  11. max: 20,
  12. value: 10,
  13. step: 1
  14. });
  15. document.body.scrollTop = 2000;
  16. var sliderHandleEl = document.querySelector("#scrollTestSliderId .slider-handle");
  17. var sliderHandleBoundingBoxInfo = sliderHandleEl.getBoundingClientRect();
  18. sliderHandleTopPos = sliderHandleBoundingBoxInfo.top;
  19. sliderHandleLeftPos = sliderHandleBoundingBoxInfo.left;
  20. });
  21. afterEach(function() {
  22. if(testSlider) {
  23. testSlider.destroy();
  24. }
  25. document.body.scrollTop = 0;
  26. });
  27. // The difference between sliderHandleTopPos and mousemoveY is equal to 50 in both cases,
  28. // but difference between initial and final slider value is not equal (6 and 4).
  29. // It happens because we don't 'hit' the center of handle but the top left corner.
  30. it("slides up when handle moves upwards after scroll page down", function() {
  31. var mousemove = document.createEvent('MouseEvents');
  32. var mousemoveX = sliderHandleLeftPos;
  33. var mousemoveY = sliderHandleTopPos - 50;
  34. var newSliderValue;
  35. mousemove.initMouseEvent(
  36. "mousedown",
  37. true /* bubble */,
  38. true /* cancelable */,
  39. window,
  40. null,
  41. 0, 0, mousemoveX, mousemoveY, /* coordinates */
  42. false, false, false, false, /* modifier keys */
  43. 0 /*left*/,
  44. null
  45. );
  46. testSlider.sliderElem.dispatchEvent(mousemove);
  47. newSliderValue = testSlider.getValue();
  48. expect(newSliderValue).toEqual(4);
  49. });
  50. it("slides down when handle moves downwards after scroll page down", function() {
  51. var mousemove = document.createEvent('MouseEvents');
  52. var mousemoveX = sliderHandleLeftPos;
  53. var mousemoveY = sliderHandleTopPos + 50;
  54. var newSliderValue;
  55. mousemove.initMouseEvent(
  56. "mousedown",
  57. true /* bubble */,
  58. true /* cancelable */,
  59. window,
  60. null,
  61. 0, 0, mousemoveX, mousemoveY, /* coordinates */
  62. false, false, false, false, /* modifier keys */
  63. 0 /*left*/,
  64. null
  65. );
  66. testSlider.sliderElem.dispatchEvent(mousemove);
  67. newSliderValue = testSlider.getValue();
  68. expect(newSliderValue).toEqual(14);
  69. });
  70. });
  71. describe('Horizontal scrolled body', function() {
  72. beforeEach(function() {
  73. testSlider = new Slider('#offRightEdgeSliderInput', {
  74. id: 'offRightEdgeSlider',
  75. orientation: 'horizontal',
  76. min: 0,
  77. max: 20,
  78. value: 10,
  79. step: 1,
  80. });
  81. testSlider.sliderElem.scrollIntoView();
  82. var handle = document.querySelector('#offRightEdgeSlider .slider-handle');
  83. var handleRect = handle.getBoundingClientRect();
  84. sliderHandleTopPos = handleRect.top;
  85. sliderHandleLeftPos = handleRect.left;
  86. });
  87. afterEach(function() {
  88. if (testSlider) {
  89. testSlider.destroy();
  90. }
  91. window.scrollTo(0, 0);
  92. });
  93. it('slides left when clicked on the left of the handle', function() {
  94. var x = sliderHandleLeftPos - 50;
  95. var y = sliderHandleTopPos;
  96. var mousedown, newSliderValue;
  97. mousedown = createMouseDownEvent(x, y);
  98. testSlider.sliderElem.dispatchEvent(mousedown);
  99. newSliderValue = testSlider.getValue();
  100. expect(newSliderValue).toEqual(4);
  101. });
  102. it('slides right when clicked on the left of the handle', function() {
  103. var x = sliderHandleLeftPos + 50;
  104. var y = sliderHandleTopPos;
  105. var mousedown, newSliderValue;
  106. mousedown = createMouseDownEvent(x, y);
  107. testSlider.sliderElem.dispatchEvent(mousedown);
  108. newSliderValue = testSlider.getValue();
  109. expect(newSliderValue).toEqual(14);
  110. });
  111. function createMouseDownEvent(x, y) {
  112. var mousedown = document.createEvent('MouseEvents');
  113. mousedown.initMouseEvent(
  114. 'mousedown',
  115. false /* bubble */,
  116. true /* cancelable */,
  117. window, /* view */
  118. null, /* detail */
  119. 0, 0, x, y, /* coordinates */
  120. false, false, false, false, /* modifier keys */
  121. 0, /* button: left */
  122. null /* relatedTarget */
  123. );
  124. return mousedown;
  125. }
  126. });
  127. });