Commit 6634f9e7 by Alex MacCaw

fix tests (and a few bugs)

parent 0716f356
......@@ -51,8 +51,7 @@ ClearbitClient.prototype.url = function (options) {
ClearbitClient.prototype.request = function (options) {
options = _.defaults(options, {
method: 'get',
query: {}
method: 'get'
});
return needle.requestAsync(
......
......@@ -17,15 +17,20 @@ function ClearbitResource (data) {
_.extend(this, data);
}
ClearbitResource.extractParams = function(params) {
return _.omit(params || {}, 'path', 'method', 'params', 'client', 'api', 'stream');
ClearbitResource.extractParams = function(options) {
var params = _.omit(options || {},
'path', 'method', 'params',
'client', 'api', 'stream'
);
return _.isEmpty(params) ? null : params;
};
ClearbitResource.get = Promise.method(function (path, options) {
options = _.extend({
path: path,
method: 'get',
params: this.extractParams(options)
query: this.extractParams(options)
}, this.options, options || {});
return this.client.request(options)
......@@ -50,6 +55,9 @@ ClearbitResource.post = Promise.method(function (path, options) {
return this.client.request(options)
.bind(this)
.then(function (data) {
return new this(data);
})
.catch(isUnknownRecord, function () {
throw new this.NotFoundError(this.name + ' not found');
});
......@@ -63,7 +71,13 @@ function createErrors (name) {
}
exports.create = function (name, options) {
var Resource = function () {
var Resource = function (data) {
if (_.isArray(data)) {
return data.map(function(item){
return new this.constructor(item);
}.bind(this));
}
ClearbitResource.apply(this, arguments);
};
......
'use strict';
var assert = require('assert');
var resource = require('./resource');
var _ = require('lodash');
......
......@@ -34,22 +34,22 @@ describe('Client', function () {
});
describe('#base', function () {
describe('#endpoint', function () {
it('requires an API', function () {
expect(client.base.bind(client, {}))
expect(client.endpoint.bind(client, {}))
.to.throw(/API must be specified/);
});
it('can generate the default base', function () {
expect(client.base({
it('can generate the default endpoint', function () {
expect(client.endpoint({
api: 'person'
}))
.to.equal('https://person.clearbit.com/v1');
});
it('can generate a streaming base', function () {
expect(client.base({
it('can generate a streaming endpoint', function () {
expect(client.endpoint({
api: 'person',
stream: true
}))
......@@ -57,7 +57,7 @@ describe('Client', function () {
});
it('can set a custom version', function () {
expect(client.base({
expect(client.endpoint({
api: 'person',
version: '2'
}))
......
[
{
"id": "6ffa17f3-2653-485f-9129-73bdaa88087a",
"name": {
"fullName": "Joe Schmo"
},
"addresses": [],
"list": "ofac_sdn",
"remarks": null,
"type": "entity"
}
]
'use strict';
var expect = require('chai').expect;
var nock = require('nock');
var Watchlist = require('../')('k').Watchlist;
describe('Watchlist', function () {
var mock;
before(function () {
mock = nock('https://watchlist.clearbit.com');
});
after(nock.cleanAll);
afterEach(function () {
mock.done();
});
describe('Watchlist#search', function () {
var watchlist = require('./fixtures/watchlist');
it('can search a watchlist by name', function () {
mock
.post('/v1/search/all')
.reply(200, watchlist);
return Watchlist.search({name: 'Joe'})
.then(function (watchlist) {
// console.log(watchlist.constructor)
expect(watchlist[0])
.to.be.an.instanceOf(Watchlist)
.and.have.property('id', watchlist.id);
});
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment