Commit de141c81 authored by Aaron Tidwell's avatar Aaron Tidwell

add tests for client, error-handler, matches, participants, tournaments. ...

add tests for client, error-handler, matches, participants, tournaments.  update coverage npm script to get 100% coverage
parent 3da12d43
......@@ -76,8 +76,7 @@ function convertProperties(obj, converionFunction) {
//objects recurse
if (typeof obj[prop] === 'object' && obj[prop] !== null) {
obj[converionFunction(prop)] = convertProperties(obj[prop], converionFunction);
}
else if (prop.search(checkRegex) > -1) {
} else if (prop.search(checkRegex) > -1) {
obj[converionFunction(prop)] = obj[prop];
//remove it
delete obj[prop];
......@@ -93,7 +92,7 @@ Client.prototype.setSubdomain = function(subdomain) {
//generate the subdomain URL string if there is one
if (!subdomain) {
this.options.subdomain = '';
} else if (subdomain[subdomain.length-1] !== '-') {
} else if (subdomain[subdomain.length - 1] !== '-') {
this.options.subdomain = subdomain + '-';
} else {
this.options.subdomain = subdomain;
......@@ -114,7 +113,7 @@ Client.prototype.makeRequest = function(obj) {
// Add on the api key
obj.api_key = this.options.get('apiKey'); //convert for url
obj.cache_bust = Math.random()
//serialize the properties
//serialize the properties
var serialized = serializeProperties(obj);
var compiledParams = serialized.serialized;
//merge the stuff to remove
......@@ -158,7 +157,7 @@ Client.prototype.makeRequest = function(obj) {
if (self.options.get('format') == 'json') {
resData = JSON.parse(resData);
if (self.options.get('massageProperties')) {
resData = convertProperties(resData,UnderscoreToCamel);
resData = convertProperties(resData, UnderscoreToCamel);
}
}
callback(null, resData); //no error, so no err object
......
......@@ -134,6 +134,27 @@ describe('Client Class', () => {
expect(opts['some_property[another_property]']).toBe('anotherthing');
});
it('should recurse down properties to camelCase but not parent prototype props', () => {
const parent = Object.create({});
parent.something = 'a thing';
const child = Object.create(parent);
child.path = '/some/path';
child.method = 'GET';
child.someProperty = {
anotherProperty: 'anotherthing'
};
expect(child.something).toBe('a thing');
client.makeRequest(child);
const opts = parseOpts();
expect(opts['some_property[another_property]']).toBe('anotherthing');
expect(opts['something']).not.toBeDefined();
});
it('should add the api key to request', () => {
client.options.apiKey = 'mykey';
client.makeRequest({});
......
......@@ -16,7 +16,7 @@ exports.handle = function(res, resData, callback, format) {
statusCode: res.statusCode,
text: 'Invalid API key'
};
callback(err,res);
callback(err, res);
return;
}
......@@ -28,13 +28,15 @@ exports.handle = function(res, resData, callback, format) {
statusCode: res.statusCode,
text: 'Object not found within your account scope'
};
callback(err,res);
callback(err, res);
return;
}
// 422 - Validation error(s) for create or update method
if (res.statusCode === 422) {
if (format == 'json') { resData = JSON.parse(resData); }
if (format == 'json') {
resData = JSON.parse(resData);
}
err = {
error: true,
errors: resData.errors,
......@@ -49,11 +51,11 @@ exports.handle = function(res, resData, callback, format) {
err = {
error: true,
errors: [],
statusCode: res.statusCode,
statusCode: res.statusCode,
text: resData
};
// ship the response object back as the data
callback(err, res);
return;
}
\ No newline at end of file
}
const HandleFn = require('./error-handler').handle;
//exports.handle = function(res, resData, callback, format)
describe('error handler', () => {
it('should throw an error if the statusCode is 401 402 or 404, or an unknown', () => {
const errStatus = [401, 402, 404, 999];
function cb(err, res) {
expect(err).toBeDefined();
}
errStatus.forEach((status) => {
HandleFn({
statusCode: status
}, {}, cb, 'json');
});
});
it('should have error, errors, statusCode, and text props', () => {
const errStatus = [401, 402, 404];
function cb(err, res) {
expect(err.error).toBeDefined(true);
expect(typeof err.errors).toBeDefined([]);
expect(errStatus.indexOf(err.statusCode) > -1).toBe(true);
expect(err.text).toBeDefined();
}
errStatus.forEach((status) => {
HandleFn({
statusCode: status
}, {}, cb, 'json');
});
});
it('should parse resData as json if json is passed as a 422', () => {
function cb(err, res) {
expect(err.text).toEqual({
some: 'prop'
});
}
HandleFn({
statusCode: 422
}, '{"some":"prop"}', cb, 'json');
});
it('should not parse resData if not json type and passed as a 422', () => {
function cb(err, res) {
expect(err.text).toEqual('<prop>');
}
HandleFn({
statusCode: 422
}, '<prop>', cb, 'xml');
});
});
......@@ -33,4 +33,4 @@ Matches.prototype.update = function(obj) {
delete obj.matchId;
obj.method = 'PUT';
this.makeRequest(obj);
};
\ No newline at end of file
};
const Matches = require('./matches').Matches;
const Client = require('./client').Client;
let matchesInstance;
describe('matches endpoints', () => {
beforeEach(() => {
matchesInstance = new Matches();
});
describe('matches constructor', () => {
it('should inherit from the client', () => {
expect(Object.getPrototypeOf(Matches.prototype)).toBe(Client.prototype);
});
});
// index GET tournaments/:tournament/matches
describe('index', () => {
it('should create an appropriate url without a subdomain', () => {
matchesInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/25/matches',
method: 'GET'
});
};
matchesInstance.index({
id: 25
});
});
it('should create an appropriate url with a subdomain', () => {
matchesInstance.options.subdomain = 'somedomain-';
matchesInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/somedomain-25/matches',
method: 'GET'
})
};
matchesInstance.index({
id: 25
});
});
});
// show GET tournaments/:tournament/matches/:match_id
describe('show', () => {
it('should create an appropriate url without a subdomain', () => {
matchesInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/25/matches/123',
method: 'GET'
});
};
matchesInstance.show({
id: 25,
matchId: 123
});
});
it('should create an appropriate url with a subdomain', () => {
matchesInstance.options.subdomain = 'somedomain-';
matchesInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/somedomain-25/matches/123',
method: 'GET'
});
};
matchesInstance.show({
id: 25,
matchId: 123
});
});
});
// update PUT tournaments/:tournament/matches/:match_id
describe('update', () => {
it('should create an appropriate url without a subdomain', () => {
matchesInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/25/matches/123',
method: 'PUT'
});
};
matchesInstance.update({
id: 25,
matchId: 123
});
});
it('should create an appropriate url with a subdomain', () => {
matchesInstance.options.subdomain = 'somedomain-';
matchesInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/somedomain-25/matches/123',
method: 'PUT'
});
};
matchesInstance.update({
id: 25,
matchId: 123
});
});
});
});
......@@ -8,11 +8,6 @@
var util = require('util');
var Client = require('./client').Client;
//used for the randomize workaround
var sys = require('sys')
var exec = require('child_process').exec;
var Participants = exports.Participants = function(options) {
Client.call(this, options); // call parent constructor
};
......@@ -63,4 +58,4 @@ Participants.prototype.randomize = function(obj) {
delete obj.id;
obj.method = 'POST';
this.makeRequest(obj);
}
\ No newline at end of file
}
const Participants = require('./participants').Participants;
const Client = require('./client').Client;
let participantsInstance;
describe('participants endpoints', () => {
beforeEach(() => {
participantsInstance = new Participants();
});
describe('participants constructor', () => {
it('should inherit from the client', () => {
expect(Object.getPrototypeOf(Participants.prototype)).toBe(Client.prototype);
});
});
// index GET tournaments/:tournament/participants
describe('index', () => {
it('should create an appropriate url without a subdomain', () => {
participantsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/25/participants',
method: 'GET'
})
};
participantsInstance.index({
id: 25
});
});
it('should create an appropriate url with a subdomain', () => {
participantsInstance.options.subdomain = 'somedomain-';
participantsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/somedomain-25/participants',
method: 'GET'
})
};
participantsInstance.index({
id: 25
});
});
});
// create POST tournaments/:tournament/participants
describe('create', () => {
it('should create an appropriate url without a subdomain', () => {
participantsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/25/participants',
method: 'POST'
})
};
participantsInstance.create({
id: 25
});
});
it('should create an appropriate url with a subdomain', () => {
participantsInstance.options.subdomain = 'somedomain-';
participantsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/somedomain-25/participants',
method: 'POST'
})
};
participantsInstance.create({
id: 25
});
});
});
// show GET tournaments/:tournament/participants/:participant_id
describe('show', () => {
it('should create an appropriate url without a subdomain', () => {
participantsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/25/participants/456',
method: 'GET'
})
};
participantsInstance.show({
id: 25,
participantId: 456
});
});
it('should create an appropriate url with a subdomain', () => {
participantsInstance.options.subdomain = 'somedomain-';
participantsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/somedomain-25/participants/456',
method: 'GET'
})
};
participantsInstance.show({
id: 25,
participantId: 456
});
});
});
// update PUT tournaments/:tournament/participants/:participant_id
describe('update', () => {
it('should create an appropriate url without a subdomain', () => {
participantsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/25/participants/456',
method: 'PUT'
})
};
participantsInstance.update({
id: 25,
participantId: 456
});
});
it('should create an appropriate url with a subdomain', () => {
participantsInstance.options.subdomain = 'somedomain-';
participantsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/somedomain-25/participants/456',
method: 'PUT'
})
};
participantsInstance.update({
id: 25,
participantId: 456
});
});
});
// destroy DELETE tournaments/:tournament/participants/:participant_id
describe('destroy', () => {
it('should create an appropriate url without a subdomain', () => {
participantsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/25/participants/456',
method: 'DELETE'
})
};
participantsInstance.destroy({
id: 25,
participantId: 456
});
});
it('should create an appropriate url with a subdomain', () => {
participantsInstance.options.subdomain = 'somedomain-';
participantsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/somedomain-25/participants/456',
method: 'DELETE'
})
};
participantsInstance.destroy({
id: 25,
participantId: 456
});
});
});
// randomize GET tournaments/:tournament/participants/randomize
describe('randomize', () => {
it('should create an appropriate url without a subdomain', () => {
participantsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/25/participants/randomize',
method: 'POST'
})
};
participantsInstance.randomize({
id: 25
});
});
it('should create an appropriate url with a subdomain', () => {
participantsInstance.options.subdomain = 'somedomain-';
participantsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/somedomain-25/participants/randomize',
method: 'POST'
})
};
participantsInstance.randomize({
id: 25
});
});
});
});
......@@ -14,7 +14,7 @@ var Tournaments = exports.Tournaments = function(options) {
Client.call(this, options); // call parent constructor
this.getRawSubdomain = function() {
return this.options.get('subdomain').replace('-','');
return this.options.get('subdomain').replace('-', '');
};
};
......@@ -81,4 +81,4 @@ Tournaments.prototype.reset = function(obj) {
delete obj.id;
obj.method = 'POST';
this.makeRequest(obj);
};
\ No newline at end of file
};
const Tournaments = require('./tournaments').Tournaments;
const Client = require('./client').Client;
let tournamentsInstance;
describe('tournaments endpoints', () => {
beforeEach(() => {
tournamentsInstance = new Tournaments();
});
describe('tournaments constructor', () => {
it('should inherit from the client', () => {
expect(Object.getPrototypeOf(Tournaments.prototype)).toBe(Client.prototype);
});
});
// index GET tournaments
describe('index', () => {
it('should create an appropriate url without a subdomain', () => {
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
id: 25,
method: 'GET'
})
};
tournamentsInstance.index({
id: 25
});
});
it('should create an appropriate url with a subdomain', () => {
tournamentsInstance.options.subdomain = 'somedomain-';
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
id: 25,
method: 'GET',
subdomain: 'somedomain'
})
};
tournamentsInstance.index({
id: 25
});
});
});
// create POST tournaments
describe('create', () => {
it('should create an appropriate url without a subdomain', () => {
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
id: 25,
method: 'POST'
})
};
tournamentsInstance.create({
id: 25
});
});
it('should create an appropriate url with a subdomain', () => {
tournamentsInstance.options.subdomain = 'somedomain-';
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
id: 25,
method: 'POST',
tournament: {
subdomain: 'somedomain'
}
})
};
tournamentsInstance.create({
id: 25,
tournament: {}
});
});
});
// show GET tournaments/:tournament
describe('show', () => {
it('should create an appropriate url without a subdomain', () => {
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/25',
method: 'GET'
})
};
tournamentsInstance.show({
id: 25
});
});
it('should create an appropriate url with a subdomain', () => {
tournamentsInstance.options.subdomain = 'somedomain-';
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/somedomain-25',
method: 'GET'
})
};
tournamentsInstance.show({
id: 25
});
});
});
// update PUT tournaments/:tournament
describe('update', () => {
it('should create an appropriate url without a subdomain', () => {
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/25',
method: 'PUT'
})
};
tournamentsInstance.update({
id: 25
});
});
it('should create an appropriate url with a subdomain', () => {
tournamentsInstance.options.subdomain = 'somedomain-';
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/somedomain-25',
method: 'PUT'
})
};
tournamentsInstance.update({
id: 25
});
});
});
// destroy DELETE tournaments/:tournament
describe('destroy', () => {
it('should create an appropriate url without a subdomain', () => {
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/25',
method: 'DELETE'
})
};
tournamentsInstance.destroy({
id: 25
});
});
it('should create an appropriate url with a subdomain', () => {
tournamentsInstance.options.subdomain = 'somedomain-';
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/somedomain-25',
method: 'DELETE'
})
};
tournamentsInstance.destroy({
id: 25
});
});
});
// start POST tournaments/:tournament/start
describe('start', () => {
it('should create an appropriate url without a subdomain', () => {
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/25/start',
method: 'POST'
})
};
tournamentsInstance.start({
id: 25
});
});
it('should create an appropriate url with a subdomain', () => {
tournamentsInstance.options.subdomain = 'somedomain-';
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/somedomain-25/start',
method: 'POST'
})
};
tournamentsInstance.start({
id: 25
});
});
});
// finalize POST tournaments/:tournament/finalize
describe('finalize', () => {
it('should create an appropriate url without a subdomain', () => {
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/25/finalize',
method: 'POST'
})
};
tournamentsInstance.finalize({
id: 25
});
});
it('should create an appropriate url with a subdomain', () => {
tournamentsInstance.options.subdomain = 'somedomain-';
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/somedomain-25/finalize',
method: 'POST'
})
};
tournamentsInstance.finalize({
id: 25
});
});
});
// reset POST tournaments/:tournament/reset
describe('reset', () => {
it('should create an appropriate url without a subdomain', () => {
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/25/reset',
method: 'POST'
})
};
tournamentsInstance.reset({
id: 25
});
});
it('should create an appropriate url with a subdomain', () => {
tournamentsInstance.options.subdomain = 'somedomain-';
tournamentsInstance.makeRequest = (obj) => {
expect(obj).toEqual({
path: '/somedomain-25/reset',
method: 'POST'
})
};
tournamentsInstance.reset({
id: 25
});
});
});
});
......@@ -28,4 +28,4 @@ exports.createClient = function createClient(options) {
};
return client;
};
\ No newline at end of file
};
......@@ -4,16 +4,13 @@
"author": "Aaron Tiwell <aaron.tidwell@gmail.com>",
"main": "./lib/challonge.js",
"version": "1.2.0",
"contributors": [
{
"name": "Ricardo Reis",
"email": "ricardojoaoreis@gmail.com"
},
{
"name": "Jonas Vanen",
"email": "jonas.vanen@gmail.com"
}
],
"contributors": [{
"name": "Ricardo Reis",
"email": "ricardojoaoreis@gmail.com"
}, {
"name": "Jonas Vanen",
"email": "jonas.vanen@gmail.com"
}],
"repository": {
"type": "git",
"url": "https://github.com/Tidwell/node-challonge"
......@@ -35,7 +32,7 @@
},
"scripts": {
"test": "./node_modules/jasmine/bin/jasmine.js",
"coverage": "node ./node_modules/istanbul/lib/cli.js cover --include-all-sources ./node_modules/jasmine/bin/jasmine.js"
"coverage": "node ./node_modules/istanbul/lib/cli.js cover --include-all-sources ./node_modules/jasmine/bin/jasmine.js -x '**/spec/**' -x '**/key.js'"
},
"engine": "node >= 0.10.x"
}
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