update constructor, add option validation tests

This commit is contained in:
2016-04-14 15:03:29 -07:00
parent 83823ebdf0
commit 5619612943
3 changed files with 60 additions and 13 deletions

View File

@@ -21,5 +21,8 @@
"expect.js": "~0.3.1", "expect.js": "~0.3.1",
"mocha": "~2.4.5", "mocha": "~2.4.5",
"sinon": "~1.17.3" "sinon": "~1.17.3"
},
"dependencies": {
"joi": "~8.0.5"
} }
} }

View File

@@ -1,12 +1,38 @@
class User { import events from 'events';
constructor(firstName, lastName) { import Joi from 'joi';
this.firstName = firstName;
this.lastName = lastName;
}
getName() { export default class Elastique extends events.EventEmitter {
return `${this.firstName} ${this.lastName}` constructor(options = {}) {
super();
var schema = Joi.object({
url: Joi.string().uri({ scheme: ['http', 'https'] }).default('http://localhost:9200'),
index: Joi.string().required(),
interval: Joi.string().default('1w'),
timeout: Joi.number().min(10000).default(10000),
}).default();
const validateSchema = () => new Promise(function (resolve, reject) {
schema.validate(options, (err, settings) => {
if (err) return reject(err);
resolve(settings);
})
})
this.ready = true;
Promise.all([
validateSchema()
])
.then(([ settings ]) => {
console.log('settings', settings);
this.settings = settings;
})
.catch((err) => {
this.ready = false;
this.emit('error', err);
throw err;
});
} }
} }
export default User;

View File

@@ -1,10 +1,28 @@
import User from '../lib/index' import Elastique from '../lib/index'
import expect from 'expect.js'; import expect from 'expect.js';
describe('User class', function () { describe('Elastique class', function () {
it('should return the name', function () { describe('Option validation', function () {
var user = new User('test', 'user'); it('should emit error without an index', function (done) {
const queue = new Elastique();
expect(user.getName()).to.equal('test user'); queue.on('error', (err) => {
expect(err).to.be.an(Error);
expect(err.message).to.match(/index/);
done();
});
});
it('should emit error with an invalid URL', function (done) {
const queue = new Elastique({
index: 'elastique',
url: 'nope://nope'
});
queue.on('error', (err) => {
expect(err).to.be.an(Error);
expect(err.message).to.match(/url/);
done();
});
});
}); });
}); });