added simple helper for properly handling promises in my tests

This commit is contained in:
Spencer Alger
2014-02-25 09:50:35 -07:00
parent ca01f7a6e3
commit f05bde519d

View File

@ -9,6 +9,21 @@ describe('Angular esFactory', function () {
});
var uuid = (function () { var i = 0; return function () { return ++i; }; }());
/**
* Perform promise based async code in a way that mocha will understand
* @param {Function} cb - node style callback
* @param {Function} body - function that executes async and returns a promise/value
*/
var prom = function (cb, body) {
expect(cb).to.be.a('function');
expect(body).to.be.a('function');
var promise = body();
expect(promise.then).to.be.a('function');
promise.then(function () { cb(); }, cb);
};
function directive(makeDirective) {
var root = document.createElement('div');
root.setAttribute('ng-controller', 'empty-controller');
@ -37,6 +52,7 @@ describe('Angular esFactory', function () {
};
});
});
it('has Transport and ConnectionPool properties', function (done) {
directive(function (esFactory) {
return function () {
@ -46,6 +62,7 @@ describe('Angular esFactory', function () {
};
});
});
it('returns a new client when it is called', function (done) {
directive(function (esFactory) {
return function () {
@ -61,16 +78,35 @@ describe('Angular esFactory', function () {
};
});
});
it('returns an error created by calling a method incorrectly', function (done) {
directive(function (esFactory) {
return function () {
var client = esFactory({ hosts: null });
prom(done, function () {
var client = esFactory({ hosts: null });
return client.get().then(function () {
expect.fail('promise should have been rejected');
}, function (err) {
expect(err.message).to.match(/unable/i);
});
});
};
});
});
client.get().then(function () {
expect.fail('promise should have been rejected');
}, function (err) {
expect(err.message).to.match(/unable/i);
done();
it('ping\'s properly', function (done) {
directive(function (esFactory) {
return function () {
prom(done, function () {
var client = esFactory({
hosts: 'not-a-valid-es-host.es'
});
return client.ping().then(function () {
expect.fail('promise should have been rejected');
}, function (err) {
expect(err).to.be.a(esFactory.errors.NoConnections);
});
});
};
});