destroy.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. var test = require('tape');
  3. var dragula = require('..');
  4. test('destroy does not throw when not dragging, destroyed, or whatever', function (t) {
  5. t.test('a single time', function once (st) {
  6. var drake = dragula();
  7. st.doesNotThrow(function () {
  8. drake.destroy();
  9. }, 'dragula bites into a single call to drake.destroy');
  10. st.end();
  11. });
  12. t.test('multiple times', function once (st) {
  13. var drake = dragula();
  14. st.doesNotThrow(function () {
  15. drake.destroy();
  16. drake.destroy();
  17. drake.destroy();
  18. drake.destroy();
  19. }, 'dragula bites into multiple calls to drake.destroy');
  20. st.end();
  21. });
  22. t.end();
  23. });
  24. test('when dragging and destroy 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.destroy();
  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 destroy gets called, dragend event is emitted gracefully', 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('dragend', dragend);
  44. drake.destroy();
  45. t.plan(1);
  46. t.end();
  47. function dragend () {
  48. t.pass('dragend got called');
  49. }
  50. });
  51. test('when dragging a copy and destroy gets called, default does not revert', function (t) {
  52. var div = document.createElement('div');
  53. var div2 = document.createElement('div');
  54. var item = document.createElement('div');
  55. var drake = dragula([div, div2]);
  56. div.appendChild(item);
  57. document.body.appendChild(div);
  58. document.body.appendChild(div2);
  59. drake.start(item);
  60. div2.appendChild(item);
  61. drake.on('drop', drop);
  62. drake.on('dragend', dragend);
  63. drake.destroy();
  64. t.plan(4);
  65. t.end();
  66. function dragend () {
  67. t.pass('dragend got called');
  68. }
  69. function drop (target, parent, source) {
  70. t.equal(target, item, 'drop was invoked with item');
  71. t.equal(parent, div2, 'drop was invoked with final container');
  72. t.equal(source, div, 'drop was invoked with source container');
  73. }
  74. });
  75. test('when dragging a copy and destroy gets called, revert is executed', function (t) {
  76. var div = document.createElement('div');
  77. var div2 = document.createElement('div');
  78. var item = document.createElement('div');
  79. var drake = dragula([div, div2], { revertOnSpill: true });
  80. div.appendChild(item);
  81. document.body.appendChild(div);
  82. document.body.appendChild(div2);
  83. drake.start(item);
  84. div2.appendChild(item);
  85. drake.on('cancel', cancel);
  86. drake.on('dragend', dragend);
  87. drake.destroy();
  88. t.plan(3);
  89. t.end();
  90. function dragend () {
  91. t.pass('dragend got called');
  92. }
  93. function cancel (target, container) {
  94. t.equal(target, item, 'cancel was invoked with item');
  95. t.equal(container, div, 'cancel was invoked with container');
  96. }
  97. });