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,193 +539,382 @@ describe('common_utils', () => { ...@@ -539,193 +539,382 @@ 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;
id: 1,
group_name: 'GitLab.org', const mockObjects = {
absolute_web_url: 'https://gitlab.com/gitlab-org/', convertObjectProps: {
}; obj: {
const mappings = { id: 1,
id: 'id', group_name: 'GitLab.org',
groupName: 'group_name', absolute_web_url: 'https://gitlab.com/gitlab-org/',
absoluteWebUrl: 'absolute_web_url', },
}; objNested: {
project_name: 'GitLab CE',
group_name: 'GitLab.org',
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 }) => {
it('does not deep-convert by default', () => { const testFunction =
const obj = { functionName === 'convertObjectProps'
snake_key: { ? (obj, options = {}) =>
child_snake_key: 'value', commonUtils.convertObjectProps(mockConversionFunction, obj, options)
}, : commonUtils[functionName];
};
it('returns an empty object if `obj` parameter is null, undefined or an empty object', () => {
expect(commonUtils.convertObjectPropsToCamelCase(obj)).toEqual({ expect(isEmptyObject(testFunction(null))).toBeTruthy();
snakeKey: { expect(isEmptyObject(testFunction())).toBeTruthy();
child_snake_key: 'value', expect(isEmptyObject(testFunction({}))).toBeTruthy();
},
}); });
});
describe('convertObjectPropsToSnakeCase', () => { it('converts object properties', () => {
it('converts each object key to snake case', () => { const expected = {
const obj = { convertObjectProps: {
some: 'some', id_converted: 1,
'cool object': 'cool object', group_name_converted: 'GitLab.org',
likeThisLongOne: 'likeThisLongOne', absolute_web_url_converted: 'https://gitlab.com/gitlab-org/',
},
convertObjectPropsToCamelCase: {
id: 1,
groupName: 'GitLab.org',
absoluteWebUrl: 'https://gitlab.com/gitlab-org/',
},
convertObjectPropsToSnakeCase: {
id: 1,
group_name: 'GitLab.org',
absolute_web_url: 'https://gitlab.com/gitlab-org/',
},
}; };
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', () => { it('does not deep-convert by default', () => {
['', {}, null].forEach(badObj => { const expected = {
expect(commonUtils.convertObjectPropsToSnakeCase(badObj)).toEqual({}); convertObjectProps: {
}); project_name_converted: 'GitLab CE',
}); group_name_converted: 'GitLab.org',
}); license_type_converted: 'MIT',
tech_stack_converted: {
describe('with options', () => { backend: 'Ruby',
const objWithoutChildren = { frontend_framework: 'Vue',
project_name: 'GitLab CE', database: 'PostgreSQL',
group_name: 'GitLab.org', },
license_type: 'MIT', },
}; convertObjectPropsToCamelCase: {
projectName: 'GitLab CE',
groupName: 'GitLab.org',
licenseType: 'MIT',
techStack: {
backend: 'Ruby',
frontend_framework: 'Vue',
database: 'PostgreSQL',
},
},
convertObjectPropsToSnakeCase: {
project_name: 'GitLab CE',
group_name: 'GitLab.org',
license_type: 'MIT',
tech_stack: {
backend: 'Ruby',
frontendFramework: 'Vue',
database: 'PostgreSQL',
},
},
};
const objWithChildren = { expect(testFunction(mockObjNested)).toEqual(expected[functionName]);
project_name: 'GitLab CE', });
group_name: 'GitLab.org',
license_type: 'MIT',
tech_stack: {
backend: 'Ruby',
frontend_framework: 'Vue',
database: 'PostgreSQL',
},
};
describe('when options.deep is true', () => { describe('with options', () => {
it('converts object with child objects', () => { describe('when options.deep is true', () => {
const obj = { const expected = {
snake_key: { convertObjectProps: {
child_snake_key: 'value', 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',
group_name: 'GitLab.org',
license_type: 'MIT',
tech_stack: {
backend: 'Ruby',
frontend_framework: 'Vue',
database: 'PostgreSQL',
},
}, },
}; };
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', () => { it('converts array of nested objects', () => {
const arr = [ expect(testFunction([mockObjNested], { deep: true })).toEqual([expected[functionName]]);
{ });
child_snake_key: 'value',
},
];
expect(commonUtils.convertObjectPropsToCamelCase(arr, { deep: true })).toEqual([ it('converts array with child arrays', () => {
{ expect(testFunction([[mockObjNested]], { deep: true })).toEqual([
childSnakeKey: 'value', [expected[functionName]],
}, ]);
]); });
}); });
it('converts array with child arrays', () => { describe('when options.dropKeys is provided', () => {
const arr = [ it('discards properties mentioned in `dropKeys` array', () => {
[ const expected = {
{ convertObjectProps: {
child_snake_key: 'value', project_name_converted: 'GitLab CE',
license_type_converted: 'MIT',
tech_stack_converted: {
backend: 'Ruby',
frontend_framework: 'Vue',
database: 'PostgreSQL',
},
}, },
], convertObjectPropsToCamelCase: {
]; projectName: 'GitLab CE',
licenseType: 'MIT',
expect(commonUtils.convertObjectPropsToCamelCase(arr, { deep: true })).toEqual([ techStack: {
[ backend: 'Ruby',
{ frontend_framework: 'Vue',
childSnakeKey: 'value', database: 'PostgreSQL',
},
}, },
], convertObjectPropsToSnakeCase: {
]); project_name: 'GitLab CE',
}); license_type: 'MIT',
}); tech_stack: {
backend: 'Ruby',
describe('when options.dropKeys is provided', () => { frontendFramework: 'Vue',
it('discards properties mentioned in `dropKeys` array', () => { database: 'PostgreSQL',
expect( },
commonUtils.convertObjectPropsToCamelCase(objWithoutChildren, { },
dropKeys: ['group_name'], };
}),
).toEqual({ const dropKeys = {
projectName: 'GitLab CE', convertObjectProps: ['group_name'],
licenseType: 'MIT', convertObjectPropsToCamelCase: ['group_name'],
convertObjectPropsToSnakeCase: ['groupName'],
};
expect(
testFunction(mockObjNested, {
dropKeys: dropKeys[functionName],
}),
).toEqual(expected[functionName]);
}); });
});
it('discards properties mentioned in `dropKeys` array when `deep` is true', () => { it('discards properties mentioned in `dropKeys` array when `deep` is true', () => {
expect( const expected = {
commonUtils.convertObjectPropsToCamelCase(objWithChildren, { convertObjectProps: {
deep: true, project_name_converted: 'GitLab CE',
dropKeys: ['group_name', 'database'], license_type_converted: 'MIT',
}), tech_stack_converted: {
).toEqual({ backend_converted: 'Ruby',
projectName: 'GitLab CE', frontend_framework_converted: 'Vue',
licenseType: 'MIT', },
techStack: { },
backend: 'Ruby', convertObjectPropsToCamelCase: {
frontendFramework: 'Vue', projectName: 'GitLab CE',
}, licenseType: 'MIT',
techStack: {
backend: 'Ruby',
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',
projectName: 'GitLab CE', tech_stack_converted: {
licenseType: 'MIT', backend: 'Ruby',
group_name: 'GitLab.org', frontend_framework: 'Vue',
database: 'PostgreSQL',
},
},
convertObjectPropsToCamelCase: {
projectName: 'GitLab CE',
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'],
};
expect(
testFunction(mockObjNested, {
ignoreKeyNames: ignoreKeyNames[functionName],
}),
).toEqual(expected[functionName]);
}); });
});
it('leaves properties mentioned in `ignoreKeyNames` array intact when `deep` is true', () => { it('leaves properties mentioned in `ignoreKeyNames` array intact when `deep` is true', () => {
expect( const expected = {
commonUtils.convertObjectPropsToCamelCase(objWithChildren, { convertObjectProps: {
deep: true, project_name_converted: 'GitLab CE',
ignoreKeyNames: ['group_name', 'frontend_framework'], group_name: 'GitLab.org',
}), license_type_converted: 'MIT',
).toEqual({ tech_stack_converted: {
projectName: 'GitLab CE', backend_converted: 'Ruby',
group_name: 'GitLab.org', frontend_framework: 'Vue',
licenseType: 'MIT', database_converted: 'PostgreSQL',
techStack: { },
backend: 'Ruby', },
frontend_framework: 'Vue', convertObjectPropsToCamelCase: {
database: 'PostgreSQL', projectName: 'GitLab CE',
}, 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', '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