Skip to content

Commit

Permalink
Merge pull request #88 from aydrian/ISSUE-79
Browse files Browse the repository at this point in the history
Modified toApiFormat spec test file to fit standard naming convention. Added tests for code coverage.
  • Loading branch information
aydrian committed Aug 6, 2015
2 parents 24b3cc4 + 3fa3a23 commit c192067
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 82 deletions.
11 changes: 3 additions & 8 deletions lib/toApiFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@ module.exports = function toApiFormat(source) {
var excludeList = ['substitution_data', 'tags', 'metadata', 'attributes', 'headers'];

try{
// Handle arrays
if(Array.isArray(source) && _.isObject(source[0])) {
// Handle arrays (Only need to handle arrays of objects for our use case.)
if(Array.isArray(source) && _.isPlainObject(source[0])) {
dest = [];
for(var i = 0; i < source.length; i++) {
// Exclude appropriately
if( -1 === excludeList.indexOf(source[i])) {
dest.push(toApiFormat(source[i]));
} else {
dest.push(source[i]);
}
dest.push(toApiFormat(source[i]));
}
return dest;
}
Expand Down
74 changes: 0 additions & 74 deletions test/spec/toApiFormat.js

This file was deleted.

152 changes: 152 additions & 0 deletions test/spec/toApiFormat.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
'use strict';

var chai = require('chai')
, sinon = require('sinon')
, sinonChai = require('sinon-chai')
, toApiFormat = require('../../lib/toApiFormat')
, expect = chai.expect
, _ = require('lodash')
;

chai.use(sinonChai);

describe('toApiFormat', function() {
it('should recursively convert complex object keys to snake_case', function(done) {
var testObj = {
simpleString: "foo"
, boolVal: true
, numBers: 201
, bangObj: {
strTwo: "bar"
, string_three: "skip"
}
, recipients: [
{name: 'John Doe', address: '[email protected]'}
, {name: 'Sam Jones', address: '[email protected]'}
]
, fizzArr: [
{
buzzInga: "buzz"
, bang: {
bellHop: "bell"
}
}
, {
bilBo: "baggins"
, alias: {
misTer: "underhill"
}
}
]
, good_name: null
};

var validationObj = {
simple_string: "foo"
, bool_val: true
, num_bers: 201
, bang_obj: {
str_two: "bar"
, string_three: "skip"
}
, recipients: [
{name: 'John Doe', address: '[email protected]'}
, {name: 'Sam Jones', address: '[email protected]'}
]
, fizz_arr: [
{
buzz_inga: "buzz"
, bang: {
bell_hop: "bell"
}
}
, {
bil_bo: "baggins"
, alias: {
mis_ter: "underhill"
}
}
]
, good_name: null
};

var out = toApiFormat(testObj);

expect(out).to.deep.equal(validationObj);

done();
});

it('should throw an error if it encounters one', function(done) {
var stub = sinon.stub(_, 'snakeCase').throws();

var testObj = {
simpleString: "foo"
, substitution_data: {
key1: 'value1',
key_2: 'value_2'
}
, sampleArray: [
'oneTag',
'two_tag',
'substitution_data',
'tags'
]
};

try {
var out = toApiFormat(testObj);
} catch(e) {
expect(e).to.not.be.null;
}

stub.restore();
done();
});

it('should ignore sub-properties of properties on exclude list', function(done) {
var testObj = {
simpleString: "foo"
, substitution_data: {
key1: 'value1',
key_2: 'value_2'
}
, sampleArray: [
{
goodValue: 'hello',
tags: [
'oneTag',
'two_tag',
'substitution_data',
'tags'
]
}
]
};

var validationObj = {
simple_string: "foo"
, substitution_data: {
key1: 'value1',
key_2: 'value_2'
}
, sample_array: [
{
good_value: 'hello',
tags: [
'oneTag',
'two_tag',
'substitution_data',
'tags'
]
}
]
};

var out = toApiFormat(testObj);

expect(out).to.deep.equal(validationObj);

done();
});
});

0 comments on commit c192067

Please sign in to comment.