Skip to content

Commit c192067

Browse files
committed
Merge pull request #88 from aydrian/ISSUE-79
Modified toApiFormat spec test file to fit standard naming convention. Added tests for code coverage.
2 parents 24b3cc4 + 3fa3a23 commit c192067

File tree

3 files changed

+155
-82
lines changed

3 files changed

+155
-82
lines changed

lib/toApiFormat.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,11 @@ module.exports = function toApiFormat(source) {
88
var excludeList = ['substitution_data', 'tags', 'metadata', 'attributes', 'headers'];
99

1010
try{
11-
// Handle arrays
12-
if(Array.isArray(source) && _.isObject(source[0])) {
11+
// Handle arrays (Only need to handle arrays of objects for our use case.)
12+
if(Array.isArray(source) && _.isPlainObject(source[0])) {
1313
dest = [];
1414
for(var i = 0; i < source.length; i++) {
15-
// Exclude appropriately
16-
if( -1 === excludeList.indexOf(source[i])) {
17-
dest.push(toApiFormat(source[i]));
18-
} else {
19-
dest.push(source[i]);
20-
}
15+
dest.push(toApiFormat(source[i]));
2116
}
2217
return dest;
2318
}

test/spec/toApiFormat.js

Lines changed: 0 additions & 74 deletions
This file was deleted.

test/spec/toApiFormat.spec.js

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
'use strict';
2+
3+
var chai = require('chai')
4+
, sinon = require('sinon')
5+
, sinonChai = require('sinon-chai')
6+
, toApiFormat = require('../../lib/toApiFormat')
7+
, expect = chai.expect
8+
, _ = require('lodash')
9+
;
10+
11+
chai.use(sinonChai);
12+
13+
describe('toApiFormat', function() {
14+
it('should recursively convert complex object keys to snake_case', function(done) {
15+
var testObj = {
16+
simpleString: "foo"
17+
, boolVal: true
18+
, numBers: 201
19+
, bangObj: {
20+
strTwo: "bar"
21+
, string_three: "skip"
22+
}
23+
, recipients: [
24+
{name: 'John Doe', address: '[email protected]'}
25+
, {name: 'Sam Jones', address: '[email protected]'}
26+
]
27+
, fizzArr: [
28+
{
29+
buzzInga: "buzz"
30+
, bang: {
31+
bellHop: "bell"
32+
}
33+
}
34+
, {
35+
bilBo: "baggins"
36+
, alias: {
37+
misTer: "underhill"
38+
}
39+
}
40+
]
41+
, good_name: null
42+
};
43+
44+
var validationObj = {
45+
simple_string: "foo"
46+
, bool_val: true
47+
, num_bers: 201
48+
, bang_obj: {
49+
str_two: "bar"
50+
, string_three: "skip"
51+
}
52+
, recipients: [
53+
{name: 'John Doe', address: '[email protected]'}
54+
, {name: 'Sam Jones', address: '[email protected]'}
55+
]
56+
, fizz_arr: [
57+
{
58+
buzz_inga: "buzz"
59+
, bang: {
60+
bell_hop: "bell"
61+
}
62+
}
63+
, {
64+
bil_bo: "baggins"
65+
, alias: {
66+
mis_ter: "underhill"
67+
}
68+
}
69+
]
70+
, good_name: null
71+
};
72+
73+
var out = toApiFormat(testObj);
74+
75+
expect(out).to.deep.equal(validationObj);
76+
77+
done();
78+
});
79+
80+
it('should throw an error if it encounters one', function(done) {
81+
var stub = sinon.stub(_, 'snakeCase').throws();
82+
83+
var testObj = {
84+
simpleString: "foo"
85+
, substitution_data: {
86+
key1: 'value1',
87+
key_2: 'value_2'
88+
}
89+
, sampleArray: [
90+
'oneTag',
91+
'two_tag',
92+
'substitution_data',
93+
'tags'
94+
]
95+
};
96+
97+
try {
98+
var out = toApiFormat(testObj);
99+
} catch(e) {
100+
expect(e).to.not.be.null;
101+
}
102+
103+
stub.restore();
104+
done();
105+
});
106+
107+
it('should ignore sub-properties of properties on exclude list', function(done) {
108+
var testObj = {
109+
simpleString: "foo"
110+
, substitution_data: {
111+
key1: 'value1',
112+
key_2: 'value_2'
113+
}
114+
, sampleArray: [
115+
{
116+
goodValue: 'hello',
117+
tags: [
118+
'oneTag',
119+
'two_tag',
120+
'substitution_data',
121+
'tags'
122+
]
123+
}
124+
]
125+
};
126+
127+
var validationObj = {
128+
simple_string: "foo"
129+
, substitution_data: {
130+
key1: 'value1',
131+
key_2: 'value_2'
132+
}
133+
, sample_array: [
134+
{
135+
good_value: 'hello',
136+
tags: [
137+
'oneTag',
138+
'two_tag',
139+
'substitution_data',
140+
'tags'
141+
]
142+
}
143+
]
144+
};
145+
146+
var out = toApiFormat(testObj);
147+
148+
expect(out).to.deep.equal(validationObj);
149+
150+
done();
151+
});
152+
});

0 commit comments

Comments
 (0)