RangeHighlightsSpec.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. RangeHighlights Render Tests
  3. */
  4. describe("RangeHighlights Render Tests", function() {
  5. var testSlider1;
  6. var testSlider2;
  7. var testSlider3;
  8. var testSlider4;
  9. //setup
  10. beforeEach(function() {
  11. var rangeHighlightsOpts1 = [
  12. { "start": 2, "end": 5, "class": "category1" }, // left: 10%; width: 15%
  13. { "start": 7, "end": 8, "class": "category2" }, // left: 35%; width: 5%
  14. { "start": 17, "end": 19 }, // left: 85%; width: 10%
  15. { "start": 17, "end": 24 }, //out of range - not visible
  16. { "start": -3, "end": 19 } //out of range - not visible
  17. ];
  18. var rangeHighlightsOpts2 = [
  19. { "start": 2, "end": 5, "class": "category1" }, // top: 10%; height: 15%
  20. { "start": 7, "end": 8, "class": "category2" }, // top: 35%; height: 5%
  21. { "start": 17, "end": 19 }, // top: 85%; height: 10%
  22. { "start": 7, "end": -4 }, //out of range - not visible
  23. { "start": 23, "end": 15 } //out of range - not visible
  24. ];
  25. testSlider1 = $('#testSlider1').slider({
  26. id: 'slider1',
  27. min: 0,
  28. max: 20,
  29. step: 1,
  30. value: 14,
  31. rangeHighlights: rangeHighlightsOpts1
  32. });
  33. testSlider2 = $('#testSlider2').slider({
  34. id: 'slider2',
  35. min: 0,
  36. max: 20,
  37. step: 1,
  38. value: 14,
  39. orientation: 'vertical',
  40. rangeHighlights: rangeHighlightsOpts2
  41. });
  42. testSlider3 = $('#testSlider3').slider({
  43. id: 'slider3',
  44. min: 0,
  45. max: 20,
  46. step: 1,
  47. value: 14,
  48. reversed: true,
  49. rangeHighlights: rangeHighlightsOpts1
  50. });
  51. testSlider4 = $('#testSlider4').slider({
  52. id: 'slider4',
  53. min: 0,
  54. max: 20,
  55. step: 1,
  56. value: 14,
  57. reversed: true,
  58. orientation: 'vertical',
  59. rangeHighlights: rangeHighlightsOpts2
  60. });
  61. });
  62. //cleanup
  63. afterEach(function() {
  64. testSlider1.slider('destroy');
  65. testSlider1 = null;
  66. testSlider2.slider('destroy');
  67. testSlider2 = null;
  68. testSlider3.slider('destroy');
  69. testSlider3 = null;
  70. testSlider4.slider('destroy');
  71. testSlider4 = null;
  72. });
  73. //test the visibility of ranges e.g. : { "start": 23, "end": 15 } - out of range - not visible
  74. function testHighlightedElements(sliderId, isHorizontal, expections) {
  75. //check elements exist
  76. it("Highlighted ranges are rendered - " + sliderId, function() {
  77. expect($(sliderId).length).toBe(1);
  78. expect($(sliderId + ' .slider-rangeHighlight').length).toBe(5);
  79. expect($(sliderId + ' .slider-rangeHighlight.category1').length).toBe(1);
  80. expect($(sliderId + ' .slider-rangeHighlight.category2').length).toBe(1);
  81. });
  82. //check elements exist within proper display value
  83. it("Highlighted ranges render inside the slider's bounds " + sliderId, function() {
  84. expect($(sliderId).length).toBe(1);
  85. var ranges = $(sliderId + ' .slider-rangeHighlight');
  86. expect(ranges.length).toBe(5);
  87. for (var i = 0; i < ranges.length; i++) {
  88. expect($(ranges[i]).is(":visible")).toBe(expections[i].isVisible);
  89. if (expections[i].isVisible) {
  90. if(isHorizontal) {
  91. expect(_getLeftPercent($(ranges[i]))).toBe(expections[i].start);
  92. expect(_getWidthPercent($(ranges[i]))).toBe(expections[i].size);
  93. } else {
  94. expect(_getTopPercent($(ranges[i]))).toBe(expections[i].start);
  95. expect(_getHeightPercent($(ranges[i]))).toBe(expections[i].size);
  96. }
  97. }
  98. }
  99. });
  100. }
  101. function _getLeftPercent(element) {
  102. return Math.round(100 * element.position().left / element.parent().width()) + '%';
  103. }
  104. function _getWidthPercent(element) {
  105. var width = element.width();
  106. var parentWidth = element.offsetParent().width();
  107. return Math.round(100 * width / parentWidth) + '%';
  108. }
  109. function _getTopPercent(element) {
  110. return Math.round(100 * element.position().top / element.parent().height()) + '%';
  111. }
  112. function _getHeightPercent(element) {
  113. var height = element.height();
  114. var parentHeight = element.offsetParent().height();
  115. return Math.round(100 * height / parentHeight) + '%';
  116. }
  117. //test both testSlider
  118. testHighlightedElements('#slider1', true, [{
  119. isVisible: true,
  120. start: '10%',
  121. size: '15%'
  122. }, {
  123. isVisible: true,
  124. start: '35%',
  125. size: '5%'
  126. }, {
  127. isVisible: true,
  128. start: '85%',
  129. size: '10%'
  130. }, {
  131. isVisible: false,
  132. start: '85%',
  133. size: '10%'
  134. }, {
  135. isVisible: false,
  136. start: '85%',
  137. size: '10%'
  138. }]);
  139. testHighlightedElements('#slider2', false, [{
  140. isVisible: true,
  141. start: '10%',
  142. size: '15%'
  143. }, {
  144. isVisible: true,
  145. start: '35%',
  146. size: '5%'
  147. }, {
  148. isVisible: true,
  149. start: '85%',
  150. size: '10%'
  151. }, {
  152. isVisible: false,
  153. start: '85%',
  154. size: '10%'
  155. }, {
  156. isVisible: false,
  157. start: '85%',
  158. size: '10%'
  159. }]);
  160. testHighlightedElements('#slider3', true, [{
  161. isVisible: true,
  162. start: '75%',
  163. size: '15%'
  164. }, {
  165. isVisible: true,
  166. start: '60%',
  167. size: '5%'
  168. }, {
  169. isVisible: true,
  170. start: '5%',
  171. size: '10%'
  172. }, {
  173. isVisible: false,
  174. start: '5%',
  175. size: '10%'
  176. }, {
  177. isVisible: false,
  178. start: '5%',
  179. size: '10%'
  180. }]);
  181. testHighlightedElements('#slider4', false, [{
  182. isVisible: true,
  183. start: '75%',
  184. size: '15%'
  185. }, {
  186. isVisible: true,
  187. start: '60%',
  188. size: '5%'
  189. }, {
  190. isVisible: true,
  191. start: '5%',
  192. size: '10%'
  193. }, {
  194. isVisible: false,
  195. start: '5%',
  196. size: '10%'
  197. }, {
  198. isVisible: false,
  199. start: '5%',
  200. size: '10%'
  201. }]);
  202. });