cancel.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. var test = require('tape');
  3. var dragula = require('..');
  4. test('cancel 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.cancel();
  9. }, 'dragula ignores a single call to drake.cancel');
  10. st.end();
  11. });
  12. t.test('multiple times', function once (st) {
  13. var drake = dragula();
  14. st.doesNotThrow(function () {
  15. drake.cancel();
  16. drake.cancel();
  17. drake.cancel();
  18. drake.cancel();
  19. }, 'dragula ignores multiple calls to drake.cancel');
  20. st.end();
  21. });
  22. t.end();
  23. });
  24. test('when dragging and cancel gets called, nothing happens', function (t) {
  25. var div = document.createElement('div');
  26. var item = document.createElement('div');
  27. var drake = dragula([div]);
  28. div.appendChild(item);
  29. document.body.appendChild(div);
  30. drake.start(item);
  31. drake.cancel();
  32. t.equal(div.children.length, 1, 'nothing happens');
  33. t.equal(drake.dragging, false, 'drake has stopped dragging');
  34. t.end();
  35. });
  36. test('when dragging and cancel gets called, cancel event is emitted', function (t) {
  37. var div = document.createElement('div');
  38. var item = document.createElement('div');
  39. var drake = dragula([div]);
  40. div.appendChild(item);
  41. document.body.appendChild(div);
  42. drake.start(item);
  43. drake.on('cancel', cancel);
  44. drake.on('dragend', dragend);
  45. drake.cancel();
  46. t.plan(3);
  47. t.end();
  48. function dragend () {
  49. t.pass('dragend got called');
  50. }
  51. function cancel (target, container) {
  52. t.equal(target, item, 'cancel was invoked with item');
  53. t.equal(container, div, 'cancel was invoked with container');
  54. }
  55. });
  56. test('when dragging a copy and cancel gets called, default does not revert', function (t) {
  57. var div = document.createElement('div');
  58. var div2 = document.createElement('div');
  59. var item = document.createElement('div');
  60. var drake = dragula([div, div2]);
  61. div.appendChild(item);
  62. document.body.appendChild(div);
  63. document.body.appendChild(div2);
  64. drake.start(item);
  65. div2.appendChild(item);
  66. drake.on('drop', drop);
  67. drake.on('dragend', dragend);
  68. drake.cancel();
  69. t.plan(4);
  70. t.end();
  71. function dragend () {
  72. t.pass('dragend got called');
  73. }
  74. function drop (target, parent, source) {
  75. t.equal(target, item, 'drop was invoked with item');
  76. t.equal(parent, div2, 'drop was invoked with final container');
  77. t.equal(source, div, 'drop was invoked with source container');
  78. }
  79. });
  80. test('when dragging a copy and cancel gets called, revert is executed', function (t) {
  81. var div = document.createElement('div');
  82. var div2 = document.createElement('div');
  83. var item = document.createElement('div');
  84. var drake = dragula([div, div2]);
  85. div.appendChild(item);
  86. document.body.appendChild(div);
  87. document.body.appendChild(div2);
  88. drake.start(item);
  89. div2.appendChild(item);
  90. drake.on('cancel', cancel);
  91. drake.on('dragend', dragend);
  92. drake.cancel(true);
  93. t.plan(3);
  94. t.end();
  95. function dragend () {
  96. t.pass('dragend got called');
  97. }
  98. function cancel (target, container) {
  99. t.equal(target, item, 'cancel was invoked with item');
  100. t.equal(container, div, 'cancel was invoked with container');
  101. }
  102. });