Adding basic tests to shared utilities. (#35)

This commit is contained in:
Sergio Díaz 2018-06-06 16:56:12 -06:00 committed by GitHub
parent cc7245b501
commit ef34ea04aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 85 additions and 11 deletions

View File

@ -69,7 +69,7 @@ describe('Request Cache', () => {
}); });
it('should return an object when given an undefined value', () => { it('should return an object when given an undefined value', () => {
expect(Object.keys(cache.parse(undefined)).length).toBe(0); expect(cache.parse(undefined)).toEqual({});
}); });
it('should parse any value other than undefined', () => { it('should parse any value other than undefined', () => {

View File

@ -0,0 +1,75 @@
const {
requestRegex,
replacementRegex,
dynamicValueRegex,
UpperCaseKeys,
removeOptionalKeys,
toKebabCase,
replaceInObject
} = require('../shared');
describe('Shared Utilities', () => {
describe('requestRegex', () => {
test.each([
['GET /hello', true],
['HEAD /hello', true],
['POST /hello', true],
['PUT /hello', true],
['DELETE /hello', true],
['CONNECT /hello', true],
['OPTIONS /hello', true],
['TRACE /hello', true],
['PATCH /hello', true]
])('should match: %s', (example, expected) => {
expect(requestRegex.test(example)).toBe(expected);
});
});
describe('replacementRegex', () => {
test.each([
['$a.b', ['$a.b']],
['GET /hello/$a.name', ['$a.name']],
['PUT /hi/$a.a/$a.b', ['$a.a', '$a.b']],
[`\\$value`, ['\\$value']]
])('should match: %s', (example, expected) => {
expect(example.match(replacementRegex)).toEqual(expected);
});
});
describe('dynamicValueRegex', () => {
test.each([
['$[test()]', ['$[test()]']],
['$[test(1, 2, 3)]', ['$[test(1, 2, 3)]']],
[`$[test({ \n id: 1 \n })]`, ['$[test({ \n id: 1 \n })]']]
])('should match: %s', (example, expected) => {
expect(example.match(dynamicValueRegex)).toEqual(expected);
});
});
describe('UpperCaseKeys', () => {
it('should uppercase all first-level keys in an object', () => {
let a = { test: 1, Test2: 2 };
expect(UpperCaseKeys(a)).toEqual({ TEST: 1, TEST2: 2 });
});
});
describe('removeOptionalKeys', () => {
it('should remove empty objects from an object', () => {
let a = { b: {}, c: 2, d: {} };
expect(removeOptionalKeys(a, ['b', 'd'])).toEqual({ c: 2 });
});
});
describe('toKebabCase', () => {
it('should convert camel case to kebab case', () => {
expect(toKebabCase('helloWorld')).toBe('hello-world');
});
});
describe('replaceInObject', () => {
it('should replace every value in an object with the output of a function', () => {
let a = { b: 'b', c: 'c' };
expect(replaceInObject(a, obj => 'a')).toEqual({ b: 'a', c: 'a' });
});
});
});

View File

@ -31,15 +31,15 @@ class RequestCache {
return null; return null;
} }
return replaceInObject(item, item => { return replaceInObject(item, item =>
return item.replace(replacementRegex, (match, key) => { item.replace(replacementRegex, (match, key) => {
if (match.startsWith('\\')) { if (match.startsWith('\\')) {
return match.replace('\\$', '$'); return match.replace('\\$', '$');
} }
return this.get(key); return this.get(key);
}); })
}); );
} }
} }

View File

@ -20,15 +20,14 @@ const UpperCaseKeys = function(obj) {
return result; return result;
}; };
const isEmptyObject = obj =>
Object.keys(obj).length === 0 && obj.constructor === Object;
const removeOptionalKeys = function(obj, optionalValues) { const removeOptionalKeys = function(obj, optionalValues) {
let result = {}; let result = {};
Object.keys(obj).forEach(key => { Object.keys(obj).forEach(key => {
if ( if (optionalValues.includes(key) && isEmptyObject(obj[key])) {
optionalValues.includes(key) &&
(Object.keys(obj[key]).length === 0 &&
obj[key].constructor === Object)
) {
return; return;
} }
@ -42,7 +41,6 @@ const toKebabCase = function(str) {
return str return str
.trim() .trim()
.replace(/([a-z])([A-Z])/g, '$1-$2') .replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/\s+/g, '-')
.toLowerCase(); .toLowerCase();
}; };
@ -62,6 +60,7 @@ const replaceInObject = function(obj, fn) {
} }
if (type === 'object') { if (type === 'object') {
obj = Object.assign({}, obj);
Object.keys(obj).forEach(k => (obj[k] = replaceInObject(obj[k], fn))); Object.keys(obj).forEach(k => (obj[k] = replaceInObject(obj[k], fn)));
} }