UNPKG

box-view-api

Version:

Box View API, node.js client library

124 lines (110 loc) 4.98 kB
// Tests for the generic Box View API class var API = require('index').BoxViewAPI; function commonInstanceMethodTests(getInstance, method) { var dummyCb = function () { }; var commonGoodParams = [ {name: 'a non-blank string resource, and a function callback', args: ['resource', dummyCb]}, {name: 'a non-blank string resource, an options hash, and a function callback', args: ['resource', {}, dummyCb]} ]; var commonBadParams = [ {name: 'no parameters', args: []}, {name: 'a callback only', args: [dummyCb]}, {name: 'a non-blank string resource, no options, no callback', args: ['resource']}, {name: 'a non-blank string resource, an options hash, no callback', args: ['resource', {}]}, {name: 'a non-blank string resource, an options hash, a callback, and a extra parameter', args: ['resource', {}, dummyCb, 'random']}, {name: 'a blank string resource, an options hash, and a function callback', args: ['', {}, dummyCb]}, {name: 'a non-string resource, an options hash, and a function callback', args: [{}, {}, dummyCb]}, {name: 'a non-blank string resource, non-object options, and a function callback', args: ['resource', 'random', dummyCb]}, {name: 'a non-blank string resource, an options hash, and a non function callback', args: ['resource', {}, 'random']} ]; framework.instanceMethodTests(getInstance, method, commonGoodParams, commonBadParams); } describe('Box View API: NodeJS API: base', function() { describe('constructor', function(){ it('should throw an error with no API Key', function() { framework.throwsError(function(){new API();}); }); it('should throw an error if too many args are passed', function() { framework.throwsError(function(){new API(framework.boxView.dummyApiKey, 1);}); }); it('should throw errors with invalid API Token parameters', function() { framework.values.invalidParamList.forEach(function(e) { framework.throwsError(function() { new API(e.value); }, 'Expected an error with an API Token of type '+e.name); }); }); it('should NOT throw an error with a non-empty API Key string', function(){ new API(framework.boxView.dummyApiKey); }); }); describe('instance', function() { var instance; function getInstance() { return instance; } before(function(){ instance = new API(framework.boxView.dummyApiKey); }); it('should have a uriString function', function() { framework.assert(typeof instance.uriString === 'function'); }); it('should have a post function', function() { framework.assert(typeof instance.post === 'function'); }); it('should have a put function', function () { framework.assert(typeof instance.put === 'function'); }); it('should have a get function', function () { framework.assert(typeof instance.get === 'function'); }); it('should have a del function', function () { framework.assert(typeof instance.del === 'function'); }); describe('.uriString()', function() { var resource = 'some/resource'; var uriBase = framework.url.format({ protocol: API.base.protocol, host: API.base.host, pathname: framework.path.join(API.base.pathname, resource) }); var uriResource; var uriFromQuery; var uriFromQS; before(function() { uriResource = instance.uriString(resource); uriFromQuery = instance.uriString(resource, {}); uriFromQS = instance.uriString(resource, {}); }); it('should throw errors with invalid resource parameters', function() { framework.values.invalidParamList.forEach(function(e) { framework.throwsError(function() { instance.uriString(e.value); }, 'Expected an error with a resource param of type '+e.name); }); }); it('should create the correct uri for a resource ('+uriBase+')', function() { framework.assert(uriResource === uriBase, 'URI is not correct URI base: '+uriResource); }); it('should create a uri from a querystring which starts with the resource URI ('+uriBase+')', function() { framework.assert(uriFromQS.indexOf(uriBase) === 0, 'URI does not start with resource URI: '+uriFromQS); }); it('should create a uri from a query hash which starts with the resource URI ('+uriBase+')', function() { framework.assert(uriFromQuery.indexOf(uriBase) === 0, 'URI does not start with resource URI: '+uriFromQuery); }); }); describe('.post()', function () { commonInstanceMethodTests(getInstance, 'post'); }); describe('.put()', function () { commonInstanceMethodTests(getInstance, 'put'); }); describe('.get()', function() { commonInstanceMethodTests(getInstance, 'get'); }); describe('.del()', function () { commonInstanceMethodTests(getInstance, 'del'); }); }); });