From bec8cf4e8e61b2cda45892a73c745a4631200845 Mon Sep 17 00:00:00 2001 From: Oleg Koval Date: Wed, 17 Jan 2018 13:44:05 +0100 Subject: [PATCH] fix: add options handling, test --- .gitignore | 1 + index.js | 10 +++++----- test/index.js | 8 ++++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 98ff05e..546c498 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /node_modules/ +/coverage/ /.envrc .DS_Store diff --git a/index.js b/index.js index ffadb38..d4c3b51 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ const Promise = require('bluebird'); const request = Promise.promisify(require('request')); -const { UnexpectedStatusCodeError } = require('./error'); +const TrembitaError = require('./error'); const Trembita = class Trembita { constructor(options) { @@ -10,7 +10,7 @@ const Trembita = class Trembita { return this.client(options) .then(res => { if (!expectedCodes.includes(res.statusCode)) { - const error = new UnexpectedStatusCodeError({ + const error = new TrembitaError.UnexpectedStatusCodeError({ options, httpStatusCode: res.statusCode, httpBody: res.body @@ -27,9 +27,9 @@ const Trembita = class Trembita { this.log.trace({ options }, 'request') return this.raw(options) }; - if (!options.endpoint) { - throw new Error('no endpoint'); - } + + if (!options) { throw new TrembitaError('missing options'); } + if (!options.endpoint) { throw new TrembitaError('missing endpoint'); } this.endpoint = options.endpoint; this.log = options.log || console; diff --git a/test/index.js b/test/index.js index 606d51e..37dd2f0 100644 --- a/test/index.js +++ b/test/index.js @@ -28,6 +28,14 @@ describe('Trembita:', () => { expect(tbita).to.have.property('client'); }); + it('should fail if options are not provided', () => { + expect(() => new Trembita()).to.throw('missing options') + }) + + it('should fail if endpoint is not provided', () => { + expect(() => new Trembita({})).to.throw('missing endpoint') + }) + it('should return status code 200 and resourse', () => { scope .get('/users?page=2')