Skip to content

Commit

Permalink
Added tests for Api constructor, moved non-exported methods into plug…
Browse files Browse the repository at this point in the history
…ins/ folder
  • Loading branch information
Hovhannes Babayan committed Dec 26, 2014
1 parent f51765a commit 91b404b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 17 deletions.
42 changes: 32 additions & 10 deletions src/Api.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
/**
* Creates new Api instance. Accepts configuration object with the following keys:
* hostname {String} - Required. A name of host to connect to
* port {String} - Optional. A port on host to connect to. Defaults to 80.
* version {String} - Optional. Which version of api to use.
* At the moment of writing can be 1.0, 2.0, 3.0, 4.0, 5.0.
* Pass 'internal' to use AtTask internal API (this is the latest version, maybe unstable)
* @param {Object} config
* @constructor
*/
function Api(config) {
this.httpOptions = {
hostname: config.hostname,
port: config.port || 80,
path: '/attask/api',
method: 'GET',
withCredentials: false
};

// Append version to hostname if provided
if (config.version) {
this.httpOptions.path = this.httpOptions.path + '/v' + config.version;
// Append version to path if provided
var path;
if (config.version === 'internal') {
path = '/attask/api-internal'
}
else {
path = '/attask/api';
if (config.version) {
path = path + '/v' + config.version;
}
}
this.httpOptions.path = path;
}

require('./request')(Api);
require('./login')(Api);
require('./logout')(Api);
require('./search')(Api);
require('./get')(Api);
require('./post')(Api);
var plugins = [
'request',
'login',
'logout',
'search',
'get',
'post'
];
for(var i=0; i<plugins.length; ++i) {
require('./plugins/' + plugins[i])(Api);
}

module.exports = Api;
2 changes: 1 addition & 1 deletion src/get.js → src/plugins/get.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Api = require('./Api');
var Api = require('./../Api');

module.exports = function(Api) {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/login.js → src/plugins/login.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Api = require('./Api');
var Api = require('./../Api');

module.exports = function(Api) {
Api.prototype.login = function (username, password) {
Expand Down
2 changes: 1 addition & 1 deletion src/logout.js → src/plugins/logout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Api = require('./Api');
var Api = require('./../Api');

module.exports = function(Api) {
Api.prototype.logout = function () {
Expand Down
2 changes: 1 addition & 1 deletion src/post.js → src/plugins/post.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Api = require('./Api');
var Api = require('./../Api');

module.exports = function(Api) {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/request.js → src/plugins/request.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Api = require('./Api'),
var Api = require('./../Api'),
queryString = require('querystring'),
http = require('http'),
util = require('util');
Expand Down
2 changes: 1 addition & 1 deletion src/search.js → src/plugins/search.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Api = require('./Api');
var Api = require('./../Api');

module.exports = function(Api) {
Api.prototype.search = function (objCode, query, fields) {
Expand Down
17 changes: 16 additions & 1 deletion test/ApiSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var Api = require('./../src/Api');


describe('Create new instance for API', function() {
it('should have methods', function(){
it('should have methods', function() {
var api = new Api({hostname: 'localhost'});
expect(api).to.respondTo('get');
expect(api).to.respondTo('login');
Expand All @@ -21,4 +21,19 @@ describe('Create new instance for API', function() {
expect(api).to.respondTo('namedQuery');
expect(api).to.respondTo('metadata');
});

it('should set correct API path based on passed configuration (version is passed)', function() {
var api = new Api({version: '2.0'});
expect(api.httpOptions.path).to.equal('/attask/api/v2.0');
});

it('should set correct API path based on passed configuration (version is not passed)', function() {
var api = new Api({});
expect(api.httpOptions.path).to.equal('/attask/api');
});

it('should set correct API path based on passed configuration (version="internal")', function() {
var api = new Api({version: 'internal'});
expect(api.httpOptions.path).to.equal('/attask/api-internal');
});
});

0 comments on commit 91b404b

Please sign in to comment.