update constructor, add option validation tests
This commit is contained in:
@@ -21,5 +21,8 @@
|
||||
"expect.js": "~0.3.1",
|
||||
"mocha": "~2.4.5",
|
||||
"sinon": "~1.17.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"joi": "~8.0.5"
|
||||
}
|
||||
}
|
||||
|
||||
42
src/index.js
42
src/index.js
@@ -1,12 +1,38 @@
|
||||
class User {
|
||||
constructor(firstName, lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
import events from 'events';
|
||||
import Joi from 'joi';
|
||||
|
||||
getName() {
|
||||
return `${this.firstName} ${this.lastName}`
|
||||
export default class Elastique extends events.EventEmitter {
|
||||
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;
|
||||
@@ -1,10 +1,28 @@
|
||||
import User from '../lib/index'
|
||||
import Elastique from '../lib/index'
|
||||
import expect from 'expect.js';
|
||||
|
||||
describe('User class', function () {
|
||||
it('should return the name', function () {
|
||||
var user = new User('test', 'user');
|
||||
describe('Elastique class', function () {
|
||||
describe('Option validation', function () {
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user