TickMarksSpec.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. *************************
  3. Tick Marks Tests
  4. *************************
  5. Verify that the number of tick marks matches what you set
  6. Verify the tick marks are at the correct intervals
  7. */
  8. describe("Slider with ticks tests", function() {
  9. var testSlider;
  10. it("Should have the number of tick marks you specify", function() {
  11. testSlider = $("#testSlider1").slider({
  12. ticks: [100, 200, 300, 400, 500]
  13. });
  14. var numTicks = $("#testSlider1").siblings('div.slider').find('.slider-tick').length;
  15. expect(numTicks).toBe(5);
  16. });
  17. it("Should be at the default positions", function() {
  18. testSlider = $("#testSlider1").slider({
  19. ticks: [100, 200, 300, 400, 500]
  20. });
  21. $("#testSlider1").siblings('div.slider').find('.slider-tick').each(function(i) {
  22. expect(this.style.left).toBe(100 * i / 4.0 + '%');
  23. });
  24. });
  25. it("Should be at the positions you specify", function() {
  26. var tickPositions = [0, 10, 20, 30, 100];
  27. testSlider = $("#testSlider1").slider({
  28. ticks: [100, 200, 300, 400, 500],
  29. ticks_positions: tickPositions
  30. });
  31. $("#testSlider1").siblings('div.slider').find('.slider-tick').each(function(i) {
  32. expect(this.style.left).toBe(tickPositions[i] + '%');
  33. });
  34. });
  35. it("Should have the tick labels you specify", function() {
  36. var tickLabels = ['$0', '$100', '$200', '$300', '$400'];
  37. testSlider = $("#testSlider1").slider({
  38. ticks: [100, 200, 300, 400, 500],
  39. ticks_labels: tickLabels
  40. });
  41. var tickLabelElements = $("#testSlider1").siblings('div.slider').find('.slider-tick-label');
  42. expect(tickLabelElements.length).toBe(tickLabels.length);
  43. tickLabelElements.each(function(i) {
  44. expect(this.innerHTML).toBe(tickLabels[i]);
  45. });
  46. });
  47. it("Should not snap to a tick within tick bounds when using the keyboard navigation", function() {
  48. testSlider = $("#testSlider1").slider({
  49. ticks: [100, 200, 300, 400, 500],
  50. ticks_snap_bounds: 30
  51. });
  52. // Focus on handle1
  53. var handle1 = $("#testSlider1").prev('div.slider').find('.slider-handle');
  54. handle1.focus();
  55. // Create keyboard event
  56. var keyboardEvent = document.createEvent("Events");
  57. keyboardEvent.initEvent("keydown", true, true);
  58. var keyPresses = 0;
  59. handle1.on("keydown", function() {
  60. keyPresses++;
  61. var value = $("#testSlider1").slider('getValue');
  62. expect(value).toBe(100 + keyPresses);
  63. });
  64. keyboardEvent.keyCode = keyboardEvent.which = 39; // RIGHT
  65. for (var i = 0; i < 5; i++) {
  66. handle1[0].dispatchEvent(keyboardEvent);
  67. }
  68. });
  69. it("Should show the correct tick marks as 'in-selection', according to the `selection` property", function() {
  70. var options = {
  71. ticks: [100, 200, 300, 400, 500],
  72. value: 250,
  73. selection: 'after'
  74. },
  75. $el = $("#testSlider1");
  76. testSlider = $el.slider(options);
  77. expect($el.siblings('div.slider').find('.in-selection').length).toBe(3);
  78. testSlider.slider('destroy');
  79. options.selection = 'before';
  80. testSlider = $el.slider(options);
  81. expect($el.siblings('div.slider').find('.in-selection').length).toBe(2);
  82. });
  83. it("Should reverse the tick labels if `reversed` option is set to true", function() {
  84. var ticks = [100, 200, 300, 400, 500];
  85. var ticksLabels = ["$100", "$200", "$300", "$400", "$500"];
  86. // Create reversed slider
  87. testSlider = $("#testSlider1").slider({
  88. id: "testSlider1Ref",
  89. ticks: ticks,
  90. ticks_labels: ticksLabels,
  91. ticks_snap_bounds: 30,
  92. reversed: true
  93. });
  94. // Assert that tick marks are reversed
  95. var tickLabelsFromDOM = $("#testSlider1Ref .slider-tick-label-container")
  96. .children(".slider-tick-label")
  97. .map(function() { return $(this).text(); })
  98. .toArray();
  99. var reversedTickLabels = ticksLabels.reverse();
  100. expect(tickLabelsFromDOM).toEqual(reversedTickLabels);
  101. });
  102. it("Should reverse the tick labels if `reversed` option is set to true and `ticks_positions` is specified", function() {
  103. var ticks = [0, 100, 200, 300, 400];
  104. var ticksLabels = ["$0", "$100", "$200", "$300", "$400"];
  105. // Create reversed slider
  106. testSlider = $("#testSlider1").slider({
  107. id: "testSlider1Ref",
  108. ticks: ticks,
  109. ticks_labels: ticksLabels,
  110. ticks_positions: [0, 30, 70, 90, 100],
  111. ticks_snap_bounds: 20,
  112. value: 200,
  113. reversed: true
  114. });
  115. // Assert that tick marks are reversed
  116. var tickLabelsFromDOM = $("#testSlider1Ref .slider-tick-label-container .slider-tick-label")
  117. .sort(function(tickLabelElemA, tickLabelElemB) {
  118. var leftOffsetA = $(tickLabelElemA).position().left;
  119. var leftOffsetB = $(tickLabelElemB).position().left;
  120. return leftOffsetA - leftOffsetB;
  121. })
  122. .map(function() { return $(this).text(); })
  123. .toArray();
  124. var reversedTickLabels = ticksLabels.reverse();
  125. expect(tickLabelsFromDOM).toEqual(reversedTickLabels);
  126. });
  127. it("should wrap all of the ticks within a div with classname '.slider-tick-container'", function() {
  128. // Create the slider with ticks
  129. var ticks = [0, 100, 200, 300, 400, 600];
  130. var $sliderDOMRef = $("#testSlider1");
  131. // Create reversed slider
  132. testSlider = $sliderDOMRef.slider({
  133. id: "testSlider1Ref",
  134. ticks: ticks,
  135. ticks_positions: [0, 30, 70, 90, 100, 130]
  136. });
  137. // Assert that the ticks are children of the container element
  138. var numTicks = $sliderDOMRef.siblings('div.slider').find('.slider-tick-container > .slider-tick').length;
  139. expect(numTicks).toBe(ticks.length);
  140. });
  141. afterEach(function() {
  142. if(testSlider) {
  143. testSlider.slider('destroy');
  144. testSlider = null;
  145. }
  146. });
  147. });
  148. describe("Slider min/max settings", function() {
  149. var $inputSlider;
  150. afterEach(function() {
  151. if ($inputSlider) {
  152. if ($inputSlider instanceof jQuery) {
  153. $inputSlider.slider('destroy');
  154. }
  155. $inputSlider = null;
  156. }
  157. });
  158. it("Should use min/max tick values for min/max settings", function() {
  159. $inputSlider = $('#testSlider1').slider({
  160. ticks: [1, 2, 3]
  161. });
  162. expect($inputSlider.slider('getAttribute', 'min')).toBe(1);
  163. expect($inputSlider.slider('getAttribute', 'max')).toBe(3);
  164. });
  165. it("Should not overwrite 'min' setting", function() {
  166. $inputSlider = $('#testSlider1').slider({
  167. min: 0,
  168. ticks: [1, 2, 3]
  169. });
  170. expect($inputSlider.slider('getAttribute', 'min')).toBe(0);
  171. expect($inputSlider.slider('getAttribute', 'max')).toBe(3);
  172. });
  173. it("Should not overwrite 'max' setting", function() {
  174. $inputSlider = $('#testSlider1').slider({
  175. max: 5,
  176. ticks: [1, 2, 3]
  177. });
  178. expect($inputSlider.slider('getAttribute', 'min')).toBe(1);
  179. expect($inputSlider.slider('getAttribute', 'max')).toBe(5);
  180. });
  181. it("Should not overwrite 'min' or max' settings", function() {
  182. $inputSlider = $('#testSlider1').slider({
  183. min: 0,
  184. max: 5,
  185. ticks: [1, 2, 3]
  186. });
  187. expect($inputSlider.slider('getAttribute', 'min')).toBe(0);
  188. expect($inputSlider.slider('getAttribute', 'max')).toBe(5);
  189. });
  190. it("Should ignore the ticks when outside of min/max range", function() {
  191. $inputSlider = $("#testSlider1").slider({
  192. ticks: [100, 200, 300, 400, 500],
  193. min: 15000,
  194. max: 25000
  195. });
  196. expect($inputSlider.slider('getAttribute', 'min')).toBe(15000);
  197. expect($inputSlider.slider('getAttribute', 'max')).toBe(25000);
  198. });
  199. });