end.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use strict';
  2. var test = require('tape');
  3. var dragula = require('..');
  4. test('end does not throw when not dragging', function (t) {
  5. t.test('a single time', function once (st) {
  6. var drake = dragula();
  7. st.doesNotThrow(function () {
  8. drake.end();
  9. }, 'dragula ignores a single call to drake.end');
  10. st.end();
  11. });
  12. t.test('multiple times', function once (st) {
  13. var drake = dragula();
  14. st.doesNotThrow(function () {
  15. drake.end();
  16. drake.end();
  17. drake.end();
  18. drake.end();
  19. }, 'dragula ignores multiple calls to drake.end');
  20. st.end();
  21. });
  22. t.end();
  23. });
  24. test('when already dragging, .end() ends (cancels) previous drag', function (t) {
  25. var div = document.createElement('div');
  26. var item1 = document.createElement('div');
  27. var item2 = document.createElement('div');
  28. var drake = dragula([div]);
  29. div.appendChild(item1);
  30. div.appendChild(item2);
  31. document.body.appendChild(div);
  32. drake.start(item1);
  33. drake.on('dragend', end);
  34. drake.on('cancel', cancel);
  35. drake.end();
  36. t.plan(4);
  37. t.equal(drake.dragging, false, 'final state is: drake is not dragging');
  38. t.end();
  39. function end (item) {
  40. t.equal(item, item1, 'dragend invoked with correct item');
  41. }
  42. function cancel (item, source) {
  43. t.equal(item, item1, 'cancel invoked with correct item');
  44. t.equal(source, div, 'cancel invoked with correct source');
  45. }
  46. });
  47. test('when already dragged, ends (drops) previous drag', function (t) {
  48. var div = document.createElement('div');
  49. var div2 = document.createElement('div');
  50. var item1 = document.createElement('div');
  51. var item2 = document.createElement('div');
  52. var drake = dragula([div, div2]);
  53. div.appendChild(item1);
  54. div.appendChild(item2);
  55. document.body.appendChild(div);
  56. document.body.appendChild(div2);
  57. drake.start(item1);
  58. div2.appendChild(item1);
  59. drake.on('dragend', end);
  60. drake.on('drop', drop);
  61. drake.end();
  62. t.plan(5);
  63. t.equal(drake.dragging, false, 'final state is: drake is not dragging');
  64. t.end();
  65. function end (item) {
  66. t.equal(item, item1, 'dragend invoked with correct item');
  67. }
  68. function drop (item, target, source) {
  69. t.equal(item, item1, 'drop invoked with correct item');
  70. t.equal(source, div, 'drop invoked with correct source');
  71. t.equal(target, div2, 'drop invoked with correct target');
  72. }
  73. });