'use strict';

var test = require('tape');
var events = require('./lib/events');
var dragula = require('..');

test('remove does not throw when not dragging', function (t) {
  t.test('a single time', function once (st) {
    var drake = dragula();
    st.doesNotThrow(function () {
      drake.remove();
    }, 'dragula ignores a single call to drake.remove');
    st.end();
  });
  t.test('multiple times', function once (st) {
    var drake = dragula();
    st.doesNotThrow(function () {
      drake.remove();
      drake.remove();
      drake.remove();
      drake.remove();
    }, 'dragula ignores multiple calls to drake.remove');
    st.end();
  });
  t.end();
});

test('when dragging and remove gets called, element is removed', function (t) {
  var div = document.createElement('div');
  var item = document.createElement('div');
  var drake = dragula([div]);
  div.appendChild(item);
  document.body.appendChild(div);
  drake.start(item);
  drake.remove();
  t.equal(div.children.length, 0, 'item got removed from container');
  t.equal(drake.dragging, false, 'drake has stopped dragging');
  t.end();
});

test('when dragging and remove gets called, remove event is emitted', function (t) {
  var div = document.createElement('div');
  var item = document.createElement('div');
  var drake = dragula([div]);
  div.appendChild(item);
  document.body.appendChild(div);
  drake.start(item);
  drake.on('remove', remove);
  drake.on('dragend', dragend);
  drake.remove();
  t.plan(3);
  t.end();
  function dragend () {
    t.pass('dragend got called');
  }
  function remove (target, container) {
    t.equal(target, item, 'remove was invoked with item');
    t.equal(container, div, 'remove was invoked with container');
  }
});

test('when dragging a copy and remove gets called, cancel event is emitted', function (t) {
  var div = document.createElement('div');
  var item = document.createElement('div');
  var drake = dragula([div], { copy: true });
  div.appendChild(item);
  document.body.appendChild(div);
  events.raise(item, 'mousedown', { which: 0 });
  events.raise(item, 'mousemove', { which: 0 });
  drake.on('cancel', cancel);
  drake.on('dragend', dragend);
  drake.remove();
  t.plan(4);
  t.end();
  function dragend () {
    t.pass('dragend got called');
  }
  function cancel (target, container) {
    t.equal(target.className, 'gu-transit', 'cancel was invoked with item');
    t.notEqual(target, item, 'item is a copy and not the original');
    t.equal(container, null, 'cancel was invoked with container');
  }
});

test('when dragging a copy and remove gets called, cancel event is emitted', function (t) {
  var div = document.createElement('div');
  var item = document.createElement('div');
  var drake = dragula([div], { copy: true });
  div.appendChild(item);
  document.body.appendChild(div);
  events.raise(item, 'mousedown', { which: 0 });
  events.raise(item, 'mousemove', { which: 0 });
  drake.on('cancel', cancel);
  drake.on('dragend', dragend);
  drake.remove();
  t.plan(4);
  t.end();
  function dragend () {
    t.pass('dragend got called');
  }
  function cancel (target, container) {
    t.equal(target.className, 'gu-transit', 'cancel was invoked with item');
    t.notEqual(target, item, 'item is a copy and not the original');
    t.equal(container, null, 'cancel was invoked with container');
  }
});

@@ -2545,814 +2545,4 @@
 %7D);%0A
-%0Atest('when dragging a copy and remove gets called, cancel event is emitted', function (t) %7B%0A  var div = document.createElement('div');%0A  var item = document.createElement('div');%0A  var drake = dragula(%5Bdiv%5D, %7B copy: true %7D);%0A  div.appendChild(item);%0A  document.body.appendChild(div);%0A  events.raise(item, 'mousedown', %7B which: 0 %7D);%0A  events.raise(item, 'mousemove', %7B which: 0 %7D);%0A  drake.on('cancel', cancel);%0A  drake.on('dragend', dragend);%0A  drake.remove();%0A  t.plan(4);%0A  t.end();%0A  function dragend () %7B%0A    t.pass('dragend got called');%0A  %7D%0A  function cancel (target, container) %7B%0A    t.equal(target.className, 'gu-transit', 'cancel was invoked with item');%0A    t.notEqual(target, item, 'item is a copy and not the original');%0A    t.equal(container, null, 'cancel was invoked with container');%0A  %7D%0A%7D);%0A

Remove duplicate test