var test = require('tape');
var listen = require('../listen');
var EventEmitter = require('events').EventEmitter;
var emitter;
var el;

test('listen returns an eventemitter with a stop function', function(t) {
  t.plan(2);

  emitter = listen({});
  t.ok(emitter instanceof EventEmitter, 'valid EventEmitter');
  t.ok(typeof emitter.stop == 'function', 'has a stop function');
});

test('captures dom events', function(t) {
  if (typeof document == 'undefined') {
    return t.end('not in a browser environment');
  }

  t.plan(1);

  // create the test element
  el = createSampleElement();

  // listen to the emitter
  emitter = listen(el, ['click']).once('click', function(evt) {
    t.pass('received click event');
  });

  // generate the a click event
  generateClick(el);
});

test('can stop event capture', function(t) {
  if (typeof document == 'undefined') {
    return t.end();
  }

  t.plan(1);
  emitter.stop();
  emitter.once('click', function() {
    t.fail('captured event and should not have');
  });

  el.addEventListener('click', function() {
    t.pass('normal event capture worked as expected');
  });

  generateClick(el);
});


/* internal helpers */

function createSampleElement() {
  var div = document.createElement('div');
  document.body.appendChild(div);

  return div;
}

function generateClick(el) {
  var evt = document.createEvent('MouseEvents');
  var bounds = el.getBoundingClientRect();

  evt.initMouseEvent(
    'click', // type
    true, // can bubble
    true, // cancelable
    window, // window context
    0, // detail arg
    (bounds.top + 10) | 0, // screenX
    (bounds.left + 10) | 0, // screenY
    10, // clientX
    10, // clientY
    false, // ctrl?
    false, // alt?
    false, // shift?
    false, // meta?
    0, // button index, 0 = left
    null // related target
  );

  return el.dispatchEvent(evt);
}

@@ -379,52 +379,8 @@
 );%0A%0A
-test('captures dom events', function(t) %7B%0A  
 if (
@@ -387,33 +387,33 @@
 typeof document 
-=
+!
 = 'undefined') %7B
@@ -419,61 +419,52 @@
 %7B%0A  
-  return t.end('not in a browser environment');%0A  %7D%0A%0A
+test('captures dom events', function(t) %7B%0A  
   t.
@@ -469,24 +469,26 @@
 t.plan(1);%0A%0A
+  
   // create 
@@ -504,16 +504,18 @@
 element%0A
+  
   el = c
@@ -533,24 +533,26 @@
 Element();%0A%0A
+  
   // listen 
@@ -568,16 +568,18 @@
 itter%0A  
+ 
+ 
 emitter 
@@ -628,32 +628,34 @@
 tion(evt) %7B%0A    
+  
 t.pass('received
@@ -666,24 +666,26 @@
 ck event');%0A
+  
   %7D);%0A%0A  // 
@@ -681,16 +681,18 @@
  %7D);%0A%0A  
+ 
+ 
 // gener
@@ -713,16 +713,18 @@
 k event%0A
+  
   genera
@@ -736,21 +736,25 @@
 ck(el);%0A
+  
 %7D);%0A%0A
+  
 test('ca
@@ -796,71 +796,8 @@
 %7B%0A  
-if (typeof document == 'undefined') %7B%0A    return t.end();%0A  %7D%0A%0A
   t.
@@ -803,24 +803,26 @@
 .plan(1);%0A  
+ 
+ 
 emitter.stop
@@ -825,16 +825,18 @@
 stop();%0A
+  
   emitte
@@ -868,16 +868,18 @@
 ) %7B%0A    
+  
 t.fail('
@@ -918,21 +918,25 @@
 ve');%0A  
+ 
+ 
 %7D);%0A%0A
+  
   el.add
@@ -967,24 +967,26 @@
 unction() %7B%0A
+  
     t.pass('
@@ -1030,21 +1030,25 @@
 ed');%0A  
+ 
+ 
 %7D);%0A%0A
+  
   genera
@@ -1060,20 +1060,23 @@
 ck(el);%0A
+  
 %7D);%0A
+%7D
 %0A%0A/* int

Include the DOM specific tests more intelligently