var assert = require('proclaim');
var config = require('../helpers/client-config');
var Keen = require('../../../lib/browser');

describe('Auto Tracking', function() {

  beforeEach(function() {
    this.timeout(5000);
    this.client = new Keen({
      projectId: config.projectId,
      writeKey: config.writeKey,
      requestType: 'xhr',
      host: config.host,
      protocol: config.protocol
    });
  });

  it('should capture "pageviews," "clicks," and "form submits"', function(done){
    var aNode = document.createElement('A');
    var bNode = document.createElement('BUTTON');
    var fNode = document.createElement('FORM');
    var iNode = document.createElement('INPUT');
    var pNode = document.createElement('INPUT');
    var inc = 0;

    this.client.on('recordEvent', function(stream, payload){
      if (stream === 'pageviews') {
        assert.equal(stream, 'pageviews');
        inc++;
      }
      else if (stream === 'clicks') {
        assert.equal(stream, 'clicks');
        assert.equal(payload.element.id, 'test-auto-tracker-clicks');
        assert.equal(payload.element.node_name, 'A');
        aNode.outerHTML = '';
        inc++;
      }
      else if (stream === 'form_submissions') {
        assert.equal(stream, 'form_submissions');
        assert.equal(payload.element.id, 'test-auto-tracker-submits');
        assert.equal(payload.element.node_name, 'FORM');
        assert.equal(payload.form.fields.email, 'team@keen.io');
        assert.notOk(payload.form.fields.password);
        fNode.outerHTML = '';
        inc++;
      }
      if (inc === 3) {
        done();
      }
    });
    this.client.initAutoTracking();

    /*
      Anchor Tag Listener
    */
    aNode.id = 'test-auto-tracker-clicks';
    aNode.href = 'javascript:void(0);';
    aNode.onclick = function(e){
      e.preventDefault();
      return false;
    };
    document.body.appendChild(aNode);

    /*
      Form Listener
    */
    fNode.id = 'test-auto-tracker-submits';
    fNode.action = 'javascript:void(0);';
    fNode.onsubmit = function(e) {
      e.preventDefault();
      return false;
    };
    // fNode.style.display = 'none';

    iNode.type = 'text';
    iNode.name = 'email';
    iNode.value = 'team@keen.io';

    pNode.type = 'password';
    pNode.name = 'password';
    pNode.value = '**********';

    bNode.type = 'submit';
    fNode.appendChild(iNode);
    fNode.appendChild(pNode);
    fNode.appendChild(bNode);
    document.body.appendChild(fNode);

    /*
      Init Behavior
    */
    if (aNode.click) {
      aNode.click();
      bNode.click();
    }
    else if(document.createEvent) {
      ev = document.createEvent('MouseEvent');
      ev.initMouseEvent('click',
          true /* bubble */, true /* cancelable */,
          window, null,
          0, 0, 0, 0,
          false, false, false, false,
          0, null
      );
      aNode.dispatchEvent(ev);
      bNode.dispatchEvent(ev);
    }
  });

});

@@ -192,32 +192,8 @@
 ) %7B%0A
-    this.timeout(5000);%0A
     
@@ -461,14 +461,34 @@
 ion(
-done)%7B
+)%7B%0A    this.timeout(5000);
 %0A   
@@ -1558,24 +1558,27 @@
      %7D%0A     
+ //
  if (inc ===
@@ -1589,18 +1589,21 @@
 %7B%0A      
+//
   
+ 
 done();%0A
@@ -1603,24 +1603,27 @@
 one();%0A     
+ //
  %7D%0A    %7D);%0A 
@@ -2515,24 +2515,49 @@
 vior%0A    */%0A
+    // Init anchor click%0A
     if (aNod
@@ -2584,24 +2584,417 @@
 de.click();%0A
+    %7D%0A    else if (document.createEvent) %7B%0A      var ev1 = document.createEvent('MouseEvent');%0A      ev1.initMouseEvent('click',%0A          true /* bubble */, true /* cancelable */,%0A          window, null,%0A          0, 0, 0, 0,%0A          false, false, false, false,%0A          0, null%0A      );%0A      aNode.dispatchEvent(ev1);%0A    %7D%0A%0A    // Init form button click (submit)%0A    if (bNode.click) %7B%0A
       bNode.
@@ -3019,16 +3019,17 @@
  else if
+ 
 (documen
@@ -3051,18 +3051,23 @@
 %7B%0A      
+var 
 ev
+2
  = docum
@@ -3105,16 +3105,17 @@
       ev
+2
 .initMou
@@ -3298,39 +3298,8 @@
  );%0A
-      aNode.dispatchEvent(ev);%0A
     
@@ -3322,16 +3322,17 @@
 Event(ev
+2
 );%0A    %7D

Break out manual event handling