var _ = require('underscore');
var chai = require('chai');
var Goodtables = require('./index');
var request = require('superagent');
var should = require('chai').should();
var spies = require('chai-spies');
var queryString = require('query-string');


chai.use(spies);

describe('Goodtables API wrapper', function() {
  it('throw error if data file is not passed in params', function(done, err) {
    if(err) done(err);

    try {
      (new Goodtables()).run();
    } catch(exception) {
      exception.message.should.be.not.empty;
      exception.message.should.be.a('string');
      done();
      return;
    }

    done('Exception not thrown');
  });

  it('respect passed param for request method', function(done, err) {
    var goodtables;
    var spyGet;
    var spyPost;


    if(err) done(err);

    spyGet = chai.spy.on(request, 'get');
    spyPost = chai.spy.on(request, 'post');
    goodtables = new Goodtables({method: 'get'});

    goodtables.run('data').then(function() {
      goodtables = new Goodtables({method: 'post'});

      goodtables.run('data').then(function() {
        spyGet.should.have.been.called();
        spyPost.should.have.been.called();
        done();
      });
    });
  });

  it('provide default values for all params', function(done, err) {
    var goodtables;
    var spyQuery;


    if(err) done(err);

    goodtables = new Goodtables();

    require('superagent-mock')(request, [{
      callback: function (match, data) {
        _.isEqual(queryString.parse(match[0].split('?')[1]), {
          fail_fast        : 'true',
          format           : 'csv',
          ignore_empty_rows: 'false',
          report_limit     : '1000',
          row_limit        : '20000'
        }).should.be.true;

        done();

        return {text: ''};
      },

      fixtures: function (match, params) { return ''; },
      pattern: '.*'
    }]);

    goodtables.run('data');
  });

  it('return Promise object', function(done, err) {
    if(err) done(err);
    chai.assert(false);
  });

  it('reject with a message when connection failed', function(done, err){
    if(err) done(err);
    chai.assert(false);
  });
});

@@ -85,24 +85,67 @@
 './index');%0A
+var Promise = require('promise-polyfill');%0A
 var request 
@@ -1998,32 +1998,54 @@
 on(done, err) %7B%0A
+    var goodtables;%0A%0A%0A
     if(err) done
@@ -2051,37 +2051,316 @@
 e(err);%0A
+%0A
     
-chai.assert(false
+goodtables = new Goodtables();%0A%0A    require('superagent-mock')(request, %5B%7B%0A      callback: function (match, data) %7B return %7Btext: ''%7D; %7D,%0A      fixtures: function (match, params) %7B return ''; %7D,%0A      pattern: '.*'%0A    %7D%5D);%0A%0A    goodtables.run('data').should.be.an.instanceOf(Promise);%0A    done(
 );%0A  %7D);

Test if Promise object returned passed (#32)