From 561961294350b714d08dc6f4dcb503dac71506b1 Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Thu, 14 Apr 2016 15:03:29 -0700 Subject: [PATCH] update constructor, add option validation tests --- package.json | 3 +++ src/index.js | 42 ++++++++++++++++++++++++++++++++++-------- test/index.js | 28 +++++++++++++++++++++++----- 3 files changed, 60 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 8c9be2b..523fa45 100644 --- a/package.json +++ b/package.json @@ -21,5 +21,8 @@ "expect.js": "~0.3.1", "mocha": "~2.4.5", "sinon": "~1.17.3" + }, + "dependencies": { + "joi": "~8.0.5" } } diff --git a/src/index.js b/src/index.js index ac498ea..d58bd6b 100644 --- a/src/index.js +++ b/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; \ No newline at end of file diff --git a/test/index.js b/test/index.js index 8dcd68b..d8068f7 100644 --- a/test/index.js +++ b/test/index.js @@ -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(); + }); + }); }); }); \ No newline at end of file