Commit 6634f9e7 by Alex MacCaw

fix tests (and a few bugs)

parent 0716f356
...@@ -51,8 +51,7 @@ ClearbitClient.prototype.url = function (options) { ...@@ -51,8 +51,7 @@ ClearbitClient.prototype.url = function (options) {
ClearbitClient.prototype.request = function (options) { ClearbitClient.prototype.request = function (options) {
options = _.defaults(options, { options = _.defaults(options, {
method: 'get', method: 'get'
query: {}
}); });
return needle.requestAsync( return needle.requestAsync(
......
...@@ -17,15 +17,20 @@ function ClearbitResource (data) { ...@@ -17,15 +17,20 @@ function ClearbitResource (data) {
_.extend(this, data); _.extend(this, data);
} }
ClearbitResource.extractParams = function(params) { ClearbitResource.extractParams = function(options) {
return _.omit(params || {}, 'path', 'method', 'params', 'client', 'api', 'stream'); var params = _.omit(options || {},
'path', 'method', 'params',
'client', 'api', 'stream'
);
return _.isEmpty(params) ? null : params;
}; };
ClearbitResource.get = Promise.method(function (path, options) { ClearbitResource.get = Promise.method(function (path, options) {
options = _.extend({ options = _.extend({
path: path, path: path,
method: 'get', method: 'get',
params: this.extractParams(options) query: this.extractParams(options)
}, this.options, options || {}); }, this.options, options || {});
return this.client.request(options) return this.client.request(options)
...@@ -50,6 +55,9 @@ ClearbitResource.post = Promise.method(function (path, options) { ...@@ -50,6 +55,9 @@ ClearbitResource.post = Promise.method(function (path, options) {
return this.client.request(options) return this.client.request(options)
.bind(this) .bind(this)
.then(function (data) {
return new this(data);
})
.catch(isUnknownRecord, function () { .catch(isUnknownRecord, function () {
throw new this.NotFoundError(this.name + ' not found'); throw new this.NotFoundError(this.name + ' not found');
}); });
...@@ -63,7 +71,13 @@ function createErrors (name) { ...@@ -63,7 +71,13 @@ function createErrors (name) {
} }
exports.create = function (name, options) { 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); ClearbitResource.apply(this, arguments);
}; };
......
'use strict'; 'use strict';
var assert = require('assert');
var resource = require('./resource'); var resource = require('./resource');
var _ = require('lodash'); var _ = require('lodash');
......
...@@ -34,22 +34,22 @@ describe('Client', function () { ...@@ -34,22 +34,22 @@ describe('Client', function () {
}); });
describe('#base', function () { describe('#endpoint', function () {
it('requires an API', function () { it('requires an API', function () {
expect(client.base.bind(client, {})) expect(client.endpoint.bind(client, {}))
.to.throw(/API must be specified/); .to.throw(/API must be specified/);
}); });
it('can generate the default base', function () { it('can generate the default endpoint', function () {
expect(client.base({ expect(client.endpoint({
api: 'person' api: 'person'
})) }))
.to.equal('https://person.clearbit.com/v1'); .to.equal('https://person.clearbit.com/v1');
}); });
it('can generate a streaming base', function () { it('can generate a streaming endpoint', function () {
expect(client.base({ expect(client.endpoint({
api: 'person', api: 'person',
stream: true stream: true
})) }))
...@@ -57,7 +57,7 @@ describe('Client', function () { ...@@ -57,7 +57,7 @@ describe('Client', function () {
}); });
it('can set a custom version', function () { it('can set a custom version', function () {
expect(client.base({ expect(client.endpoint({
api: 'person', api: 'person',
version: '2' 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