remove.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. var test = require('tape');
  3. var events = require('./lib/events');
  4. var dragula = require('..');
  5. test('remove does not throw when not dragging', function (t) {
  6. t.test('a single time', function once (st) {
  7. var drake = dragula();
  8. st.doesNotThrow(function () {
  9. drake.remove();
  10. }, 'dragula ignores a single call to drake.remove');
  11. st.end();
  12. });
  13. t.test('multiple times', function once (st) {
  14. var drake = dragula();
  15. st.doesNotThrow(function () {
  16. drake.remove();
  17. drake.remove();
  18. drake.remove();
  19. drake.remove();
  20. }, 'dragula ignores multiple calls to drake.remove');
  21. st.end();
  22. });
  23. t.end();
  24. });
  25. test('when dragging and remove gets called, element is removed', function (t) {
  26. var div = document.createElement('div');
  27. var item = document.createElement('div');
  28. var drake = dragula([div]);
  29. div.appendChild(item);
  30. document.body.appendChild(div);
  31. drake.start(item);
  32. drake.remove();
  33. t.equal(div.children.length, 0, 'item got removed from container');
  34. t.equal(drake.dragging, false, 'drake has stopped dragging');
  35. t.end();
  36. });
  37. test('when dragging and remove gets called, remove event is emitted', function (t) {
  38. var div = document.createElement('div');
  39. var item = document.createElement('div');
  40. var drake = dragula([div]);
  41. div.appendChild(item);
  42. document.body.appendChild(div);
  43. drake.start(item);
  44. drake.on('remove', remove);
  45. drake.on('dragend', dragend);
  46. drake.remove();
  47. t.plan(3);
  48. t.end();
  49. function dragend () {
  50. t.pass('dragend got called');
  51. }
  52. function remove (target, container) {
  53. t.equal(target, item, 'remove was invoked with item');
  54. t.equal(container, div, 'remove was invoked with container');
  55. }
  56. });
  57. test('when dragging a copy and remove gets called, cancel event is emitted', function (t) {
  58. var div = document.createElement('div');
  59. var item = document.createElement('div');
  60. var drake = dragula([div], { copy: true });
  61. div.appendChild(item);
  62. document.body.appendChild(div);
  63. events.raise(item, 'mousedown', { which: 1 });
  64. events.raise(item, 'mousemove', { which: 1 });
  65. drake.on('cancel', cancel);
  66. drake.on('dragend', dragend);
  67. drake.remove();
  68. t.plan(4);
  69. t.end();
  70. function dragend () {
  71. t.pass('dragend got called');
  72. }
  73. function cancel (target, container) {
  74. t.equal(target.className, 'gu-transit', 'cancel was invoked with item');
  75. t.notEqual(target, item, 'item is a copy and not the original');
  76. t.equal(container, null, 'cancel was invoked with container');
  77. }
  78. });