KeyboardSupportSpec.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. describe("Keyboard Support Tests", function() {
  2. var testSlider,
  3. handle1,
  4. handle2,
  5. keyboardEvent,
  6. initialMinVal = 0,
  7. initialMaxVal = 10,
  8. initialStepVal = 1,
  9. initialSliderVal = 5;
  10. /*
  11. Before/After setup
  12. */
  13. beforeEach(function() {
  14. // Create keyboard event
  15. keyboardEvent = document.createEvent("Events");
  16. keyboardEvent.initEvent("keydown", true, true);
  17. });
  18. afterEach(function() {
  19. if(testSlider) { testSlider.slider('destroy'); }
  20. keyboardEvent = null;
  21. keyboardEvent = null;
  22. });
  23. /*
  24. Begin Tests
  25. */
  26. describe("Clicking on slider handle automatically gives it focus", function() {
  27. beforeEach(function() {
  28. testSlider = $("#testSlider1").slider({
  29. id: 'testSlider'
  30. });
  31. handle1 = $("#testSlider").find(".slider-handle:first");
  32. });
  33. it("clicking on handle1 gives focus to handle1", function(done) {
  34. handle1.focus(function() {
  35. expect(true).toBeTruthy();
  36. done();
  37. });
  38. handle1.focus();
  39. });
  40. });
  41. describe("When slider handle has TAB focus", function() {
  42. it("should display it's tooltip if 'tooltip' option is set to 'show'", function() {
  43. testSlider = $("#testSlider1").slider({
  44. id: 'testSlider',
  45. tooltip: 'show'
  46. });
  47. handle1 = $("#testSlider").find(".slider-handle:first");
  48. // Check for no tooltip before focus
  49. var tooltipIsShown = $("#testSlider").find("div.tooltip").hasClass("in");
  50. expect(tooltipIsShown).toBeFalsy();
  51. handle1.focus();
  52. // Tooltip should be present after focus
  53. tooltipIsShown = $("#testSlider").find("div.tooltip").hasClass("in");
  54. expect(tooltipIsShown).toBeTruthy();
  55. });
  56. it("should not display it's tooltip if 'tooltip' option is set to 'hide'", function() {
  57. testSlider = $("#testSlider1").slider({
  58. id: 'testSlider',
  59. tooltip: 'hide'
  60. });
  61. handle1 = $("#testSlider").find(".slider-handle:first");
  62. // Check for hidden tooltip before focus
  63. var tooltipIsHidden = $("#testSlider").children("div.tooltip").hasClass("hide");
  64. expect(tooltipIsHidden).toBeTruthy();
  65. handle1.focus();
  66. // Tooltip should remain hidden after focus
  67. tooltipIsHidden = $("#testSlider").children("div.tooltip").hasClass("hide");
  68. expect(tooltipIsHidden).toBeTruthy();
  69. });
  70. it("should not affect the tooltip display if 'tooltip' option is set to 'always'", function() {
  71. testSlider = $("#testSlider1").slider({
  72. id: 'testSlider',
  73. tooltip: 'always'
  74. });
  75. handle1 = $("#testSlider").find(".slider-handle:first");
  76. var $tooltip = $("#testSlider").children("div.tooltip");
  77. // Check for shown tooltip before focus
  78. var tooltipIsShown = $tooltip.hasClass("in");
  79. expect(tooltipIsShown).toBeTruthy();
  80. handle1.focus();
  81. // Tooltip should remain present after focus
  82. tooltipIsShown = $tooltip.hasClass("in");
  83. expect(tooltipIsShown).toBeTruthy();
  84. });
  85. });
  86. describe("For horizontal sliders where its handle has focus", function() {
  87. beforeEach(function() {
  88. // Initialize the slider
  89. testSlider = $("#testSlider1").slider({
  90. id: 'testSlider',
  91. orientation: 'horizontal',
  92. min: initialMinVal,
  93. max: initialMaxVal,
  94. step: initialStepVal,
  95. value: initialSliderVal
  96. });
  97. // Focus on handle1
  98. handle1 = $("#testSlider .min-slider-handle");
  99. handle1.focus();
  100. });
  101. it("moves to the left by the 'step' value when the LEFT arrow key is pressed", function(done) {
  102. handle1.on("keydown", function() {
  103. var sliderValue = $("#testSlider1").slider('getValue');
  104. var expectedSliderValue = initialSliderVal - initialStepVal;
  105. expect(sliderValue).toBe(expectedSliderValue);
  106. done();
  107. });
  108. keyboardEvent.keyCode = keyboardEvent.which = 37;
  109. handle1[0].dispatchEvent(keyboardEvent);
  110. });
  111. it("moves to the right by the 'step' value when the RIGHT arrow key is pressed", function(done) {
  112. handle1.on("keydown", function() {
  113. var sliderValue = $("#testSlider1").slider('getValue');
  114. var expectedSliderValue = initialSliderVal + initialStepVal;
  115. expect(sliderValue).toBe(expectedSliderValue);
  116. done();
  117. });
  118. keyboardEvent.keyCode = keyboardEvent.which = 39;
  119. handle1[0].dispatchEvent(keyboardEvent);
  120. });
  121. it("moves to the left by the 'step' value when the DOWN arrow key is pressed", function(done) {
  122. handle1.on("keydown", function() {
  123. var sliderValue = testSlider.slider('getValue');
  124. var expectedSliderValue = initialSliderVal - initialStepVal;
  125. expect(sliderValue).toBe(expectedSliderValue);
  126. done();
  127. });
  128. keyboardEvent.keyCode = keyboardEvent.which = 40;
  129. handle1[0].dispatchEvent(keyboardEvent);
  130. });
  131. it("moves to the right by the 'step' value when the UP arrow key is pressed", function(done) {
  132. handle1.on("keydown", function() {
  133. var sliderValue = testSlider.slider('getValue');
  134. var expectedSliderValue = initialSliderVal + initialStepVal;
  135. expect(sliderValue).toBe(expectedSliderValue);
  136. done();
  137. });
  138. keyboardEvent.keyCode = keyboardEvent.which = 38;
  139. handle1[0].dispatchEvent(keyboardEvent);
  140. });
  141. });
  142. describe("For vertical sliders where its handle has focus", function() {
  143. beforeEach(function() {
  144. // Initialize the slider
  145. testSlider = $("#testSlider1").slider({
  146. id: 'testSlider',
  147. orientation: 'vertical',
  148. min: initialMinVal,
  149. max: initialMaxVal,
  150. step: initialStepVal,
  151. value: initialSliderVal
  152. });
  153. // Focus on handle1
  154. handle1 = $("#testSlider").find(".slider-handle:first");
  155. handle1.focus();
  156. });
  157. it("moves down by the 'step' value when the LEFT arrow key is pressed", function(done) {
  158. handle1.on("keydown", function() {
  159. var sliderValue = testSlider.slider('getValue');
  160. var expectedSliderValue = initialSliderVal - initialStepVal;
  161. expect(sliderValue).toBe(expectedSliderValue);
  162. done();
  163. });
  164. keyboardEvent.keyCode = keyboardEvent.which = 37;
  165. handle1[0].dispatchEvent(keyboardEvent);
  166. });
  167. it("moves up by the 'step' value when the RIGHT arrow key is pressed", function(done) {
  168. handle1.on("keydown", function() {
  169. var sliderValue = testSlider.slider('getValue');
  170. var expectedSliderValue = initialSliderVal + initialStepVal;
  171. expect(sliderValue).toBe(expectedSliderValue);
  172. done();
  173. });
  174. keyboardEvent.keyCode = keyboardEvent.which = 39;
  175. handle1[0].dispatchEvent(keyboardEvent);
  176. });
  177. it("moves down by the 'step' value when the DOWN arrow key is pressed", function(done) {
  178. handle1.on("keydown", function() {
  179. var sliderValue = testSlider.slider('getValue');
  180. var expectedSliderValue = initialSliderVal - initialStepVal;
  181. expect(sliderValue).toBe(expectedSliderValue);
  182. done();
  183. });
  184. keyboardEvent.keyCode = keyboardEvent.which = 40;
  185. handle1[0].dispatchEvent(keyboardEvent);
  186. });
  187. it("moves up by the 'step' value when the UP arrow key is pressed", function(done) {
  188. handle1.on("keydown", function() {
  189. var sliderValue = testSlider.slider('getValue');
  190. var expectedSliderValue = initialSliderVal + initialStepVal;
  191. expect(sliderValue).toBe(expectedSliderValue);
  192. done();
  193. });
  194. keyboardEvent.keyCode = keyboardEvent.which = 38;
  195. handle1[0].dispatchEvent(keyboardEvent);
  196. });
  197. });
  198. describe("For a reversed slider (regardless of 'orientation')", function() {
  199. beforeEach(function() {
  200. // Initialize the slider
  201. testSlider = $("#testSlider1").slider({
  202. id: 'testSlider',
  203. reversed: true,
  204. min: initialMinVal,
  205. max: initialMaxVal,
  206. step: initialStepVal,
  207. value: initialSliderVal
  208. });
  209. // Focus on handle1
  210. handle1 = $("#testSlider").find(".slider-handle:first");
  211. handle1.focus();
  212. });
  213. it("moves to the left by the 'step' value when the LEFT arrow key is pressed", function(done) {
  214. handle1.on("keydown", function() {
  215. var sliderValue = testSlider.slider('getValue');
  216. var expectedSliderValue = initialSliderVal - initialStepVal;
  217. expect(sliderValue).toBe(expectedSliderValue);
  218. done();
  219. });
  220. keyboardEvent.keyCode = keyboardEvent.which = 37;
  221. handle1[0].dispatchEvent(keyboardEvent);
  222. });
  223. it("moves to the right by the 'step' value when the RIGHT arrow key is pressed", function(done) {
  224. handle1.on("keydown", function() {
  225. var sliderValue = testSlider.slider('getValue');
  226. var expectedSliderValue = initialSliderVal + initialStepVal;
  227. expect(sliderValue).toBe(expectedSliderValue);
  228. done();
  229. });
  230. keyboardEvent.keyCode = keyboardEvent.which = 39;
  231. handle1[0].dispatchEvent(keyboardEvent);
  232. });
  233. it("moves to the left by the 'step' value when the DOWN arrow key is pressed", function(done) {
  234. handle1.on("keydown", function() {
  235. var sliderValue = testSlider.slider('getValue');
  236. var expectedSliderValue = initialSliderVal - initialStepVal;
  237. expect(sliderValue).toBe(expectedSliderValue);
  238. done();
  239. });
  240. keyboardEvent.keyCode = keyboardEvent.which = 40;
  241. handle1[0].dispatchEvent(keyboardEvent);
  242. });
  243. it("moves to the right by the 'step' value when the UP arrow key is pressed", function(done) {
  244. handle1.on("keydown", function() {
  245. var sliderValue = testSlider.slider('getValue');
  246. var expectedSliderValue = initialSliderVal + initialStepVal;
  247. expect(sliderValue).toBe(expectedSliderValue);
  248. done();
  249. });
  250. keyboardEvent.keyCode = keyboardEvent.which = 38;
  251. handle1[0].dispatchEvent(keyboardEvent);
  252. });
  253. });
  254. describe("For a range slider (regardless of 'orientation')", function() {
  255. beforeEach(function() {
  256. // Initialize the slider
  257. testSlider = $("#testSlider1").slider({
  258. id: 'testSlider',
  259. min: initialMinVal,
  260. max: initialMaxVal,
  261. step: initialStepVal,
  262. value: [initialSliderVal, initialSliderVal]
  263. });
  264. });
  265. describe("when handle1 tries to overtake handle2 from the left", function() {
  266. beforeEach(function() {
  267. handle1 = $("#testSlider").find(".slider-handle:first");
  268. handle2 = $("#testSlider").find(".slider-handle:last");
  269. handle1.focus();
  270. });
  271. it("handle2 moves to the right by the step value", function(done) {
  272. handle1.on("keydown", function() {
  273. var sliderValue = testSlider.slider('getValue');
  274. var expectedSliderValue = initialSliderVal + initialStepVal;
  275. expect(sliderValue[1]).toBe(expectedSliderValue);
  276. done();
  277. });
  278. keyboardEvent.keyCode = keyboardEvent.which = 39;
  279. handle1[0].dispatchEvent(keyboardEvent);
  280. });
  281. it("handle1's value remains unchanged", function(done) {
  282. var sliderValue = testSlider.slider('getValue');
  283. handle1.on("keydown", function() {
  284. expect(sliderValue[0]).toBe(initialSliderVal);
  285. done();
  286. });
  287. keyboardEvent.keyCode = keyboardEvent.which = 39;
  288. handle1[0].dispatchEvent(keyboardEvent);
  289. });
  290. it("Should give focus to handle2 after overtaking from the left", function(done) {
  291. handle2.on("focus", function() {
  292. expect(true).toBe(true);
  293. done();
  294. });
  295. keyboardEvent.keyCode = keyboardEvent.which = 39;
  296. handle1[0].dispatchEvent(keyboardEvent);
  297. });
  298. });
  299. describe("when handle2 tries to overtake handle1 from the right", function() {
  300. beforeEach(function() {
  301. handle1 = $("#testSlider").find(".slider-handle:first");
  302. handle2 = $("#testSlider").find(".slider-handle:last");
  303. handle2.focus();
  304. });
  305. it("handle1 moves to the left by the step value", function(done) {
  306. handle2.on("keydown", function() {
  307. var sliderValue = testSlider.slider('getValue');
  308. var expectedSliderValue = initialSliderVal - initialStepVal;
  309. expect(sliderValue[0]).toBe(expectedSliderValue);
  310. done();
  311. });
  312. keyboardEvent.keyCode = keyboardEvent.which = 37;
  313. handle2[0].dispatchEvent(keyboardEvent);
  314. });
  315. it("handle2's value remains unchanged", function(done) {
  316. var sliderValue = testSlider.slider('getValue');
  317. handle2.on("keydown", function() {
  318. expect(sliderValue[1]).toBe(initialSliderVal);
  319. done();
  320. });
  321. keyboardEvent.keyCode = keyboardEvent.which = 37;
  322. handle2[0].dispatchEvent(keyboardEvent);
  323. });
  324. it("Should give focus to handle1 after overtaking from the right", function(done) {
  325. handle1.on("focus", function() {
  326. expect(true).toBe(true);
  327. done();
  328. });
  329. keyboardEvent.keyCode = keyboardEvent.which = 37;
  330. handle2[0].dispatchEvent(keyboardEvent);
  331. });
  332. });
  333. });
  334. describe("For the natural arrow keys", function() {
  335. var testCases = [{
  336. reversed: false,
  337. keyEvent: 37,
  338. expectedSliderValue: initialSliderVal - initialStepVal,
  339. orientation: 'horizontal',
  340. key: 'left'
  341. }, {
  342. reversed: true,
  343. keyEvent: 37,
  344. expectedSliderValue: initialSliderVal + initialStepVal,
  345. orientation: 'horizontal',
  346. key: 'left'
  347. }, {
  348. reversed: false,
  349. keyEvent: 39,
  350. expectedSliderValue: initialSliderVal + initialStepVal,
  351. orientation: 'horizontal',
  352. key: 'right'
  353. }, {
  354. reversed: true,
  355. keyEvent: 39,
  356. expectedSliderValue: initialSliderVal - initialStepVal,
  357. orientation: 'horizontal',
  358. key: 'right'
  359. }, {
  360. reversed: false,
  361. keyEvent: 38,
  362. expectedSliderValue: initialSliderVal - initialStepVal,
  363. orientation: 'vertical',
  364. key: 'up'
  365. }, {
  366. reversed: true,
  367. keyEvent: 38,
  368. expectedSliderValue: initialSliderVal + initialStepVal,
  369. orientation: 'vertical',
  370. key: 'up'
  371. }, {
  372. reversed: false,
  373. keyEvent: 40,
  374. expectedSliderValue: initialSliderVal + initialStepVal,
  375. orientation: 'vertical',
  376. key: 'down'
  377. }, {
  378. reversed: true,
  379. keyEvent: 40,
  380. expectedSliderValue: initialSliderVal - initialStepVal,
  381. orientation: 'vertical',
  382. key: 'down'
  383. }];
  384. testCases.forEach(function(testCase) {
  385. describe("A"+((testCase.reversed)? " reversed" : "")+testCase.orientation+" slider is used for the arrow keys", function() {
  386. beforeEach(function() {
  387. // Initialize the slider
  388. testSlider = $("#testSlider1").slider({
  389. id: 'testSlider',
  390. min: initialMinVal,
  391. max: initialMaxVal,
  392. step: initialStepVal,
  393. value: initialSliderVal,
  394. natural_arrow_keys: true,
  395. reversed: testCase.reversed,
  396. orientation: testCase.orientation
  397. });
  398. handle1 = $("#testSlider").find(".slider-handle:first");
  399. handle1.focus();
  400. });
  401. it("moves to the "+testCase.key+" by the 'step' value when the "+testCase.key+" arrow key is pressed", function(done) {
  402. handle1.on("keydown", function() {
  403. var sliderValue = testSlider.slider('getValue');
  404. var expectedSliderValue = testCase.expectedSliderValue;
  405. expect(sliderValue).toBe(expectedSliderValue);
  406. done();
  407. });
  408. keyboardEvent.keyCode = keyboardEvent.which = testCase.keyEvent;
  409. handle1[0].dispatchEvent(keyboardEvent);
  410. });
  411. });
  412. });
  413. });
  414. });
  415. describe("Navigating slider with the keyboard", function() {
  416. var mySlider;
  417. var keyboardEvent;
  418. var options;
  419. var $handle1;
  420. var $handle2;
  421. describe("Does not trigger 'change' event when values do not change", function() {
  422. var initialValues = [0, 1];
  423. beforeEach(function() {
  424. options = {
  425. id: 'mySlider',
  426. min: -100,
  427. max: 100,
  428. step: 1,
  429. value: initialValues,
  430. range: true
  431. };
  432. // Create keyboard event
  433. keyboardEvent = document.createEvent('Event');
  434. keyboardEvent.initEvent('keydown', true, true);
  435. });
  436. afterEach(function() {
  437. if (mySlider) {
  438. if (mySlider instanceof Slider) { mySlider.destroy(); }
  439. mySlider = null;
  440. }
  441. });
  442. it("Should not trigger 'change' event", function(done) {
  443. var hasSlideStarted = false;
  444. var hasChanged = false;
  445. options.value = [-100, 0];
  446. mySlider = new Slider($('#testSlider1')[0], options);
  447. $handle1 = $('#mySlider').find('.slider-handle:first');
  448. mySlider.on('slideStart', function() {
  449. hasSlideStarted = true;
  450. });
  451. mySlider.on('change', function() {
  452. hasChanged = true;
  453. });
  454. mySlider.on('slideStop', function() {
  455. var value = mySlider.getValue();
  456. expect(hasSlideStarted).toBe(true);
  457. expect(hasChanged).toBe(false);
  458. expect(value).toEqual([-100, 0]);
  459. done();
  460. });
  461. // Move the handle to the left
  462. keyboardEvent.keyCode = keyboardEvent.which = 37;
  463. $handle1[0].dispatchEvent(keyboardEvent);
  464. });
  465. it("Should not trigger 'change' event via `setValue()`", function(done) {
  466. var isSliding = false;
  467. var hasChanged = false;
  468. mySlider = new Slider($('#testSlider1')[0], options);
  469. $handle1 = $('#mySlider').find('.slider-handle:first');
  470. mySlider.on('slide', function() {
  471. isSliding = true;
  472. });
  473. mySlider.on('change', function() {
  474. hasChanged = true;
  475. });
  476. // Change both values of the range slider
  477. mySlider.setValue(initialValues, true, true);
  478. window.setTimeout(function() {
  479. var value = mySlider.getValue();
  480. expect(isSliding).toBe(true);
  481. expect(hasChanged).toBe(false);
  482. expect(value).toEqual(initialValues);
  483. done();
  484. });
  485. });
  486. });
  487. describe("Does trigger 'change' event when either value changes for range sliders", function() {
  488. beforeEach(function() {
  489. options = {
  490. id: 'mySlider',
  491. min: -100,
  492. max: 100,
  493. step: 1,
  494. value: [-10, 10],
  495. range: true
  496. };
  497. // Create keyboard event
  498. keyboardEvent = document.createEvent('Event');
  499. keyboardEvent.initEvent('keydown', true, true);
  500. });
  501. afterEach(function() {
  502. if (mySlider) {
  503. if (mySlider instanceof Slider) { mySlider.destroy(); }
  504. mySlider = null;
  505. }
  506. });
  507. it("Should trigger 'change' event when the moving the lower handle", function(done) {
  508. var hasSlideStarted = false;
  509. var hasChanged = false;
  510. mySlider = new Slider($('#testSlider1')[0], options);
  511. $handle1 = $('#mySlider').find('.slider-handle:first');
  512. mySlider.on('slideStart', function() {
  513. hasSlideStarted = true;
  514. });
  515. mySlider.on('change', function() {
  516. hasChanged = true;
  517. });
  518. mySlider.on('slideStop', function() {
  519. var value = mySlider.getValue();
  520. expect(hasSlideStarted).toBe(true);
  521. expect(hasChanged).toBe(true);
  522. expect(value).toEqual([-11, 10]);
  523. done();
  524. });
  525. // Move the handle to the left
  526. keyboardEvent.keyCode = keyboardEvent.which = 37;
  527. $handle1[0].dispatchEvent(keyboardEvent);
  528. });
  529. it("Should trigger 'change' event when moving the upper handle", function(done) {
  530. var hasSlideStarted = false;
  531. var hasChanged = false;
  532. mySlider = new Slider($('#testSlider1')[0], options);
  533. $handle2 = $('#mySlider').find('.slider-handle:last');
  534. mySlider.on('slideStart', function() {
  535. hasSlideStarted = true;
  536. });
  537. mySlider.on('change', function() {
  538. hasChanged = true;
  539. });
  540. mySlider.on('slideStop', function() {
  541. var value = mySlider.getValue();
  542. expect(hasSlideStarted).toBe(true);
  543. expect(hasChanged).toBe(true);
  544. expect(value).toEqual([-10, 11]);
  545. done();
  546. });
  547. // Move the handle to the right
  548. keyboardEvent.keyCode = keyboardEvent.which = 39;
  549. $handle2[0].dispatchEvent(keyboardEvent);
  550. });
  551. it("Should trigger 'change' event when both values change via `setValue()`", function(done) {
  552. var isSliding = false;
  553. mySlider = new Slider($('#testSlider1')[0], options);
  554. $handle2 = $('#mySlider').find('.slider-handle:last');
  555. mySlider.on('slide', function() {
  556. isSliding = true;
  557. });
  558. mySlider.on('change', function() {
  559. var value = mySlider.getValue();
  560. expect(isSliding).toBe(true);
  561. expect(value).toEqual([-50, 50]);
  562. done();
  563. });
  564. // Change both values of the range slider
  565. mySlider.setValue([-50, 50], true, true);
  566. });
  567. });
  568. });
  569. describe("Navigating range sliders with the keyboard", function() {
  570. var mySlider;
  571. var keyboardEvent;
  572. var options;
  573. var $handle1;
  574. var $handle2;
  575. describe("Range slider values", function() {
  576. beforeEach(function() {
  577. options = {
  578. id: 'mySlider',
  579. min: -100,
  580. max: 100,
  581. step: 1,
  582. value: [0, 1],
  583. range: true
  584. };
  585. // Create keyboard event
  586. keyboardEvent = document.createEvent('Event');
  587. keyboardEvent.initEvent('keydown', true, true);
  588. });
  589. afterEach(function() {
  590. if (mySlider) {
  591. if (mySlider instanceof Slider) { mySlider.destroy(); }
  592. mySlider = null;
  593. }
  594. });
  595. it("Should not go below minimum at 'slideStart'", function(done) {
  596. options.value = [-100, 0];
  597. mySlider = new Slider($('#testSlider1')[0], options);
  598. $handle1 = $('#mySlider').find('.slider-handle:first');
  599. mySlider.on('slideStart', function(newVal) {
  600. expect(newVal).toEqual([-100, 0]);
  601. done();
  602. });
  603. // Move the handle to the left
  604. keyboardEvent.keyCode = keyboardEvent.which = 37;
  605. $handle1[0].dispatchEvent(keyboardEvent);
  606. });
  607. it("Should not go above maximum at 'slideStart'", function(done) {
  608. options.value = [0, 100];
  609. mySlider = new Slider($('#testSlider1')[0], options);
  610. $handle2 = $('#mySlider').find('.slider-handle:last');
  611. mySlider.on('slideStart', function(newVal) {
  612. expect(newVal).toEqual([0, 100]);
  613. done();
  614. });
  615. // Move the handle to the right
  616. keyboardEvent.keyCode = keyboardEvent.which = 39;
  617. $handle2[0].dispatchEvent(keyboardEvent);
  618. });
  619. });
  620. });