Commit 527be3be authored by peterhegman's avatar peterhegman

Refactor `convertObjectProps*` tests to use `describe.each`

To improve readability, consistency, and make it easier to add tests
for `convertObjectProps*` functions in the future.
parent 31b07e0c
...@@ -539,78 +539,174 @@ describe('common_utils', () => { ...@@ -539,78 +539,174 @@ describe('common_utils', () => {
}); });
}); });
describe('convertObjectPropsToCamelCase', () => { describe('convertObjectProps*', () => {
it('returns new object with camelCase property names by converting object with snake_case names', () => { const mockConversionFunction = prop => `${prop}_converted`;
const snakeRegEx = /(_\w)/g; const isEmptyObject = obj =>
const mockObj = { typeof obj === 'object' && obj !== null && Object.keys(obj).length === 0;
const mockObjects = {
convertObjectProps: {
obj: {
id: 1, id: 1,
group_name: 'GitLab.org', group_name: 'GitLab.org',
absolute_web_url: 'https://gitlab.com/gitlab-org/', absolute_web_url: 'https://gitlab.com/gitlab-org/',
}; },
const mappings = { objNested: {
id: 'id', project_name: 'GitLab CE',
groupName: 'group_name', group_name: 'GitLab.org',
absoluteWebUrl: 'absolute_web_url', license_type: 'MIT',
tech_stack: {
backend: 'Ruby',
frontend_framework: 'Vue',
database: 'PostgreSQL',
},
},
},
convertObjectPropsToCamelCase: {
obj: {
id: 1,
group_name: 'GitLab.org',
absolute_web_url: 'https://gitlab.com/gitlab-org/',
},
objNested: {
project_name: 'GitLab CE',
group_name: 'GitLab.org',
license_type: 'MIT',
tech_stack: {
backend: 'Ruby',
frontend_framework: 'Vue',
database: 'PostgreSQL',
},
},
},
convertObjectPropsToSnakeCase: {
obj: {
id: 1,
groupName: 'GitLab.org',
absoluteWebUrl: 'https://gitlab.com/gitlab-org/',
},
objNested: {
projectName: 'GitLab CE',
groupName: 'GitLab.org',
licenseType: 'MIT',
techStack: {
backend: 'Ruby',
frontendFramework: 'Vue',
database: 'PostgreSQL',
},
},
},
}; };
const convertedObj = commonUtils.convertObjectPropsToCamelCase(mockObj); describe('convertObjectProps', () => {
it('returns an empty object if `conversionFunction` parameter is not a function', () => {
const result = commonUtils.convertObjectProps(null, mockObjects.convertObjectProps.obj);
Object.keys(convertedObj).forEach(prop => { expect(isEmptyObject(result)).toBeTruthy();
expect(snakeRegEx.test(prop)).toBeFalsy();
expect(convertedObj[prop]).toBe(mockObj[mappings[prop]]);
}); });
}); });
it('return empty object if method is called with null or undefined', () => { describe.each`
expect(Object.keys(commonUtils.convertObjectPropsToCamelCase(null)).length).toBe(0); functionName | mockObj | mockObjNested
expect(Object.keys(commonUtils.convertObjectPropsToCamelCase()).length).toBe(0); ${'convertObjectProps'} | ${mockObjects.convertObjectProps.obj} | ${mockObjects.convertObjectProps.objNested}
expect(Object.keys(commonUtils.convertObjectPropsToCamelCase({})).length).toBe(0); ${'convertObjectPropsToCamelCase'} | ${mockObjects.convertObjectPropsToCamelCase.obj} | ${mockObjects.convertObjectPropsToCamelCase.objNested}
${'convertObjectPropsToSnakeCase'} | ${mockObjects.convertObjectPropsToSnakeCase.obj} | ${mockObjects.convertObjectPropsToSnakeCase.objNested}
`('$functionName', ({ functionName, mockObj, mockObjNested }) => {
const testFunction =
functionName === 'convertObjectProps'
? (obj, options = {}) =>
commonUtils.convertObjectProps(mockConversionFunction, obj, options)
: commonUtils[functionName];
it('returns an empty object if `obj` parameter is null, undefined or an empty object', () => {
expect(isEmptyObject(testFunction(null))).toBeTruthy();
expect(isEmptyObject(testFunction())).toBeTruthy();
expect(isEmptyObject(testFunction({}))).toBeTruthy();
}); });
it('does not deep-convert by default', () => { it('converts object properties', () => {
const obj = { const expected = {
snake_key: { convertObjectProps: {
child_snake_key: 'value', id_converted: 1,
group_name_converted: 'GitLab.org',
absolute_web_url_converted: 'https://gitlab.com/gitlab-org/',
}, },
}; convertObjectPropsToCamelCase: {
id: 1,
expect(commonUtils.convertObjectPropsToCamelCase(obj)).toEqual({ groupName: 'GitLab.org',
snakeKey: { absoluteWebUrl: 'https://gitlab.com/gitlab-org/',
child_snake_key: 'value', },
convertObjectPropsToSnakeCase: {
id: 1,
group_name: 'GitLab.org',
absolute_web_url: 'https://gitlab.com/gitlab-org/',
}, },
});
});
describe('convertObjectPropsToSnakeCase', () => {
it('converts each object key to snake case', () => {
const obj = {
some: 'some',
'cool object': 'cool object',
likeThisLongOne: 'likeThisLongOne',
}; };
expect(commonUtils.convertObjectPropsToSnakeCase(obj)).toEqual({ expect(testFunction(mockObj)).toEqual(expected[functionName]);
some: 'some',
cool_object: 'cool object',
like_this_long_one: 'likeThisLongOne',
});
});
it('returns an empty object if there are no keys', () => {
['', {}, null].forEach(badObj => {
expect(commonUtils.convertObjectPropsToSnakeCase(badObj)).toEqual({});
});
});
}); });
describe('with options', () => { it('does not deep-convert by default', () => {
const objWithoutChildren = { const expected = {
convertObjectProps: {
project_name_converted: 'GitLab CE',
group_name_converted: 'GitLab.org',
license_type_converted: 'MIT',
tech_stack_converted: {
backend: 'Ruby',
frontend_framework: 'Vue',
database: 'PostgreSQL',
},
},
convertObjectPropsToCamelCase: {
projectName: 'GitLab CE',
groupName: 'GitLab.org',
licenseType: 'MIT',
techStack: {
backend: 'Ruby',
frontend_framework: 'Vue',
database: 'PostgreSQL',
},
},
convertObjectPropsToSnakeCase: {
project_name: 'GitLab CE', project_name: 'GitLab CE',
group_name: 'GitLab.org', group_name: 'GitLab.org',
license_type: 'MIT', license_type: 'MIT',
tech_stack: {
backend: 'Ruby',
frontendFramework: 'Vue',
database: 'PostgreSQL',
},
},
}; };
const objWithChildren = { expect(testFunction(mockObjNested)).toEqual(expected[functionName]);
});
describe('with options', () => {
describe('when options.deep is true', () => {
const expected = {
convertObjectProps: {
project_name_converted: 'GitLab CE',
group_name_converted: 'GitLab.org',
license_type_converted: 'MIT',
tech_stack_converted: {
backend_converted: 'Ruby',
frontend_framework_converted: 'Vue',
database_converted: 'PostgreSQL',
},
},
convertObjectPropsToCamelCase: {
projectName: 'GitLab CE',
groupName: 'GitLab.org',
licenseType: 'MIT',
techStack: {
backend: 'Ruby',
frontendFramework: 'Vue',
database: 'PostgreSQL',
},
},
convertObjectPropsToSnakeCase: {
project_name: 'GitLab CE', project_name: 'GitLab CE',
group_name: 'GitLab.org', group_name: 'GitLab.org',
license_type: 'MIT', license_type: 'MIT',
...@@ -619,105 +715,173 @@ describe('common_utils', () => { ...@@ -619,105 +715,173 @@ describe('common_utils', () => {
frontend_framework: 'Vue', frontend_framework: 'Vue',
database: 'PostgreSQL', database: 'PostgreSQL',
}, },
};
describe('when options.deep is true', () => {
it('converts object with child objects', () => {
const obj = {
snake_key: {
child_snake_key: 'value',
}, },
}; };
expect(commonUtils.convertObjectPropsToCamelCase(obj, { deep: true })).toEqual({ it('converts nested objects', () => {
snakeKey: { expect(testFunction(mockObjNested, { deep: true })).toEqual(expected[functionName]);
childSnakeKey: 'value',
},
}); });
});
it('converts array with child objects', () => {
const arr = [
{
child_snake_key: 'value',
},
];
expect(commonUtils.convertObjectPropsToCamelCase(arr, { deep: true })).toEqual([ it('converts array of nested objects', () => {
{ expect(testFunction([mockObjNested], { deep: true })).toEqual([expected[functionName]]);
childSnakeKey: 'value',
},
]);
}); });
it('converts array with child arrays', () => { it('converts array with child arrays', () => {
const arr = [ expect(testFunction([[mockObjNested]], { deep: true })).toEqual([
[ [expected[functionName]],
{
child_snake_key: 'value',
},
],
];
expect(commonUtils.convertObjectPropsToCamelCase(arr, { deep: true })).toEqual([
[
{
childSnakeKey: 'value',
},
],
]); ]);
}); });
}); });
describe('when options.dropKeys is provided', () => { describe('when options.dropKeys is provided', () => {
it('discards properties mentioned in `dropKeys` array', () => { it('discards properties mentioned in `dropKeys` array', () => {
expect( const expected = {
commonUtils.convertObjectPropsToCamelCase(objWithoutChildren, { convertObjectProps: {
dropKeys: ['group_name'], project_name_converted: 'GitLab CE',
}), license_type_converted: 'MIT',
).toEqual({ tech_stack_converted: {
backend: 'Ruby',
frontend_framework: 'Vue',
database: 'PostgreSQL',
},
},
convertObjectPropsToCamelCase: {
projectName: 'GitLab CE', projectName: 'GitLab CE',
licenseType: 'MIT', licenseType: 'MIT',
}); techStack: {
}); backend: 'Ruby',
frontend_framework: 'Vue',
database: 'PostgreSQL',
},
},
convertObjectPropsToSnakeCase: {
project_name: 'GitLab CE',
license_type: 'MIT',
tech_stack: {
backend: 'Ruby',
frontendFramework: 'Vue',
database: 'PostgreSQL',
},
},
};
const dropKeys = {
convertObjectProps: ['group_name'],
convertObjectPropsToCamelCase: ['group_name'],
convertObjectPropsToSnakeCase: ['groupName'],
};
it('discards properties mentioned in `dropKeys` array when `deep` is true', () => {
expect( expect(
commonUtils.convertObjectPropsToCamelCase(objWithChildren, { testFunction(mockObjNested, {
deep: true, dropKeys: dropKeys[functionName],
dropKeys: ['group_name', 'database'],
}), }),
).toEqual({ ).toEqual(expected[functionName]);
});
it('discards properties mentioned in `dropKeys` array when `deep` is true', () => {
const expected = {
convertObjectProps: {
project_name_converted: 'GitLab CE',
license_type_converted: 'MIT',
tech_stack_converted: {
backend_converted: 'Ruby',
frontend_framework_converted: 'Vue',
},
},
convertObjectPropsToCamelCase: {
projectName: 'GitLab CE', projectName: 'GitLab CE',
licenseType: 'MIT', licenseType: 'MIT',
techStack: { techStack: {
backend: 'Ruby', backend: 'Ruby',
frontendFramework: 'Vue', frontendFramework: 'Vue',
}, },
}); },
convertObjectPropsToSnakeCase: {
project_name: 'GitLab CE',
license_type: 'MIT',
tech_stack: {
backend: 'Ruby',
frontend_framework: 'Vue',
},
},
};
const dropKeys = {
convertObjectProps: ['group_name', 'database'],
convertObjectPropsToCamelCase: ['group_name', 'database'],
convertObjectPropsToSnakeCase: ['groupName', 'database'],
};
expect(
testFunction(mockObjNested, {
dropKeys: dropKeys[functionName],
deep: true,
}),
).toEqual(expected[functionName]);
}); });
}); });
describe('when options.ignoreKeyNames is provided', () => { describe('when options.ignoreKeyNames is provided', () => {
it('leaves properties mentioned in `ignoreKeyNames` array intact', () => { it('leaves properties mentioned in `ignoreKeyNames` array intact', () => {
expect( const expected = {
commonUtils.convertObjectPropsToCamelCase(objWithoutChildren, { convertObjectProps: {
ignoreKeyNames: ['group_name'], project_name_converted: 'GitLab CE',
}), group_name: 'GitLab.org',
).toEqual({ license_type_converted: 'MIT',
tech_stack_converted: {
backend: 'Ruby',
frontend_framework: 'Vue',
database: 'PostgreSQL',
},
},
convertObjectPropsToCamelCase: {
projectName: 'GitLab CE', projectName: 'GitLab CE',
licenseType: 'MIT',
group_name: 'GitLab.org', group_name: 'GitLab.org',
}); licenseType: 'MIT',
}); techStack: {
backend: 'Ruby',
frontend_framework: 'Vue',
database: 'PostgreSQL',
},
},
convertObjectPropsToSnakeCase: {
project_name: 'GitLab CE',
groupName: 'GitLab.org',
license_type: 'MIT',
tech_stack: {
backend: 'Ruby',
frontendFramework: 'Vue',
database: 'PostgreSQL',
},
},
};
const ignoreKeyNames = {
convertObjectProps: ['group_name'],
convertObjectPropsToCamelCase: ['group_name'],
convertObjectPropsToSnakeCase: ['groupName'],
};
it('leaves properties mentioned in `ignoreKeyNames` array intact when `deep` is true', () => {
expect( expect(
commonUtils.convertObjectPropsToCamelCase(objWithChildren, { testFunction(mockObjNested, {
deep: true, ignoreKeyNames: ignoreKeyNames[functionName],
ignoreKeyNames: ['group_name', 'frontend_framework'],
}), }),
).toEqual({ ).toEqual(expected[functionName]);
});
it('leaves properties mentioned in `ignoreKeyNames` array intact when `deep` is true', () => {
const expected = {
convertObjectProps: {
project_name_converted: 'GitLab CE',
group_name: 'GitLab.org',
license_type_converted: 'MIT',
tech_stack_converted: {
backend_converted: 'Ruby',
frontend_framework: 'Vue',
database_converted: 'PostgreSQL',
},
},
convertObjectPropsToCamelCase: {
projectName: 'GitLab CE', projectName: 'GitLab CE',
group_name: 'GitLab.org', group_name: 'GitLab.org',
licenseType: 'MIT', licenseType: 'MIT',
...@@ -726,6 +890,31 @@ describe('common_utils', () => { ...@@ -726,6 +890,31 @@ describe('common_utils', () => {
frontend_framework: 'Vue', frontend_framework: 'Vue',
database: 'PostgreSQL', database: 'PostgreSQL',
}, },
},
convertObjectPropsToSnakeCase: {
project_name: 'GitLab CE',
groupName: 'GitLab.org',
license_type: 'MIT',
tech_stack: {
backend: 'Ruby',
frontendFramework: 'Vue',
database: 'PostgreSQL',
},
},
};
const ignoreKeyNames = {
convertObjectProps: ['group_name', 'frontend_framework'],
convertObjectPropsToCamelCase: ['group_name', 'frontend_framework'],
convertObjectPropsToSnakeCase: ['groupName', 'frontendFramework'],
};
expect(
testFunction(mockObjNested, {
deep: true,
ignoreKeyNames: ignoreKeyNames[functionName],
}),
).toEqual(expected[functionName]);
}); });
}); });
}); });
......
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