TickLabelSpec.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. Tick label Render Tests - Tests that labels render in correct positions in both horizontal and vertical orientation
  3. */
  4. describe("Tick Label Render Tests", function() {
  5. var testSliderH;
  6. var testSliderV;
  7. //setup
  8. beforeEach(function() {
  9. testSliderH = $('#testSlider1').slider({
  10. id: 'slider1',
  11. ticks: [0, 1, 2],
  12. ticks_labels:['x', 'y', 'z'],
  13. orientation:'horizontal'
  14. });
  15. testSliderV = $('#testSlider2').slider({
  16. id: 'slider2',
  17. ticks: [0, 1, 2],
  18. ticks_labels:['x', 'y', 'z'],
  19. orientation:'vertical'
  20. });
  21. });
  22. //cleanup
  23. afterEach(function() {
  24. testSliderH.slider('destroy');
  25. testSliderH = null;
  26. testSliderV.slider('destroy');
  27. testSliderV = null;
  28. });
  29. //e.g. testOrientation('horizontal', 2) will test the horizontal
  30. //code path using control with the id testSlider2
  31. function testOrientation(orientation) {
  32. var sliderIndex = orientation.toLowerCase() === 'horizontal' ? 1 : 2;
  33. var isVertical = orientation.toLowerCase() === 'horizontal' ? false : true;
  34. var sliderId = '#slider' + sliderIndex;
  35. //check elements exist
  36. it("Tick labels are rendered - " + orientation, function() {
  37. expect($(sliderId).length).toBe(1);
  38. var length = $(sliderId + ' .slider-tick-label').length;
  39. expect(length).toBe(3);
  40. });
  41. //check elements exist within the bounds of the slider
  42. it("Tick labels render inside the slider's bounds" + orientation, function() {
  43. expect($(sliderId).length).toBe(1);
  44. var sliderRect = $(sliderId)[0].getBoundingClientRect();
  45. var tickLabels = $(sliderId + ' .slider-tick-label');
  46. for (var i = 0; i < tickLabels.length; i++) {
  47. var labelRect = tickLabels[i].getBoundingClientRect();
  48. if (isVertical) {
  49. expect(labelRect.left).toBeGreaterThan(sliderRect.left);
  50. expect(labelRect.top + 10 >= sliderRect.top).toBeTruthy();
  51. } else {
  52. expect(labelRect.top + 10 >= sliderRect.top).toBeTruthy();
  53. expect(labelRect.width / 2 + labelRect.left >= sliderRect.left).toBeTruthy();
  54. }
  55. }
  56. });
  57. }
  58. //test both horizontal and vertical orientations
  59. testOrientation('horizontal');
  60. testOrientation('vertical');
  61. });
  62. describe("Tick Labels 'is-selection' and 'in-selection' Tests", function() {
  63. var $inputSlider;
  64. var options;
  65. var keyboardEvent;
  66. var $slider;
  67. var $handle1;
  68. var $handle2;
  69. var $tickLabels;
  70. var tickLabelCount;
  71. // Setup
  72. beforeEach(function() {
  73. options = {
  74. id: 'slider1',
  75. ticks: [0, 1, 2, 3, 4],
  76. value: 2,
  77. ticks_labels:['$0', '$1', '$2', '$3', '$4'],
  78. };
  79. tickLabelCount = options.ticks_labels.length;
  80. // Create keyboard event
  81. keyboardEvent = document.createEvent('Event');
  82. keyboardEvent.initEvent('keydown', true, true);
  83. });
  84. // Cleanup
  85. afterEach(function() {
  86. $inputSlider.slider('destroy');
  87. $inputSlider = null;
  88. });
  89. describe("Tick Labels 'is-selection' Tests", function() {
  90. describe("'options.selection = 'before'", function() {
  91. beforeEach(function() {
  92. options.selection = 'before';
  93. $inputSlider = $('#testSlider1').slider(options);
  94. $slider = $('#slider1');
  95. $tickLabels = $slider.find('.slider-tick-label-container div.slider-tick-label');
  96. });
  97. it("Should show the correct tick labels as 'is-selection'", function() {
  98. // There should only be one tick label with the 'label-is-selection' class
  99. expect($slider.find('.label-is-selection').length).toBe(1);
  100. // Only the third tick label should have the 'label-is-selection' class
  101. expect($tickLabels.eq(2).hasClass('label-is-selection')).toBe(true);
  102. });
  103. it("Should show the correct tick labels as 'is-selection' when keying to the left", function(done) {
  104. $handle1 = $('#slider1').find('.slider-handle:first');
  105. expect($slider.find('.label-is-selection').length).toBe(1);
  106. expect($tickLabels.eq(2).hasClass('label-is-selection')).toBe(true);
  107. $handle1.on('keydown', function() {
  108. expect($slider.find('.label-is-selection').length).toBe(1);
  109. expect($tickLabels.eq(1).hasClass('label-is-selection')).toBe(true);
  110. done();
  111. });
  112. // Move handle1 to the left with keyboard
  113. $handle1.focus();
  114. keyboardEvent.keyCode = keyboardEvent.which = 37;
  115. $handle1[0].dispatchEvent(keyboardEvent);
  116. });
  117. });
  118. describe("'options.selection = 'after'", function() {
  119. beforeEach(function() {
  120. options.selection = 'after';
  121. $inputSlider = $('#testSlider1').slider(options);
  122. $slider = $('#slider1');
  123. $tickLabels = $slider.find('.slider-tick-label-container div.slider-tick-label');
  124. });
  125. it("Should show the correct tick labels as 'is-selection'" , function() {
  126. expect($slider.find('.label-is-selection').length).toBe(1);
  127. expect($tickLabels.eq(2).hasClass('label-is-selection')).toBe(true);
  128. });
  129. it("Should show the correct tick labels as 'is-selection' when keying to the right" , function(done) {
  130. $handle1 = $('#slider1').find('.slider-handle:first');
  131. expect($slider.find('.label-is-selection').length).toBe(1);
  132. expect($tickLabels.eq(2).hasClass('label-is-selection')).toBe(true);
  133. $handle1.on('keydown', function() {
  134. expect($slider.find('.label-is-selection').length).toBe(1);
  135. expect($tickLabels.eq(3).hasClass('label-is-selection')).toBe(true);
  136. done();
  137. });
  138. // Move handle1 to the right with keyboard
  139. $handle1.focus();
  140. keyboardEvent.keyCode = keyboardEvent.which = 39;
  141. $handle1[0].dispatchEvent(keyboardEvent);
  142. });
  143. });
  144. });
  145. describe("Tick Labels 'in-selection' Tests", function() {
  146. function checkTickLabels($labels, expectedLabels) {
  147. var next = 0;
  148. // There are only 5 tick labels.
  149. expect($labels.length).toBe(tickLabelCount);
  150. for (var i = 0; i < tickLabelCount; i++) {
  151. if (i === expectedLabels[next]) {
  152. expect($labels.eq(i).hasClass('label-in-selection')).toBe(true);
  153. next++;
  154. }
  155. else {
  156. expect($labels.eq(i).hasClass('label-in-selection')).toBe(false);
  157. }
  158. }
  159. }
  160. // Setup
  161. beforeEach(function() {
  162. options.value = [1, 3];
  163. $inputSlider = $('#testSlider1').slider(options);
  164. $slider = $('#slider1');
  165. $tickLabels = $slider.find('.slider-tick-label-container div.slider-tick-label');
  166. });
  167. it("Should show the correct tick labels as 'in-selection'", function() {
  168. expect($slider.find('.label-is-selection').length).toBe(3);
  169. checkTickLabels($tickLabels, [1, 2, 3]);
  170. });
  171. it("Should show the correct tick labels as 'in-selection' when keying to the left", function(done) {
  172. $handle1 = $('#slider1').find('.slider-handle:first');
  173. // There should be 3 tick labels with the 'label-in-selection' class
  174. expect($slider.find('.label-in-selection').length).toBe(3);
  175. // Check that the correct tick labels have the 'label-in-selection' class
  176. checkTickLabels($tickLabels, [1, 2, 3]);
  177. $handle1.on('keydown', function() {
  178. expect($slider.find('.label-in-selection').length).toBe(4);
  179. // Check the labels again
  180. checkTickLabels($tickLabels, [0, 1, 2, 3]);
  181. done();
  182. });
  183. // Move handle1 to the left with keyboard
  184. $handle1.focus();
  185. keyboardEvent.keyCode = keyboardEvent.which = 37;
  186. $handle1[0].dispatchEvent(keyboardEvent);
  187. });
  188. it("Should show the correct tick labels as 'in-selection' when keying to the right" , function(done) {
  189. $handle2 = $('#slider1').find('.slider-handle:last');
  190. expect($slider.find('.label-in-selection').length).toBe(3);
  191. checkTickLabels($tickLabels, [1, 2, 3]);
  192. $handle2.on('keydown', function() {
  193. expect($slider.find('.label-in-selection').length).toBe(4);
  194. checkTickLabels($tickLabels, [1, 2, 3, 4]);
  195. done();
  196. });
  197. // Move handle2 to the right with keyboard
  198. $handle2.focus();
  199. keyboardEvent.keyCode = keyboardEvent.which = 39;
  200. $handle2[0].dispatchEvent(keyboardEvent);
  201. });
  202. });
  203. });