import { DataUtils } from '..' const $data = new DataUtils() describe('Testing Data Utils', () => { describe('compareObject', () => { test('compareObjects should return false', () => { const result = $data.compareObjects( { name: 'ACME Inc', id: '000000000001' }, { name: 'ACME Inc', id: '000000000001' } ) expect(result).toBeFalsy() }) test('compareObjects should return diff for "id"', () => { const result = $data.compareObjects( { name: 'ACME Inc', id: '000000000001' }, { name: 'ACME Inc', id: '000000000002' } ) const assert = { id: { oldValue: '000000000001', newValue: '000000000002', }, } expect(result).toBeTruthy() expect(result).toHaveProperty('id') expect(result).toEqual(assert) }) test('compareObjects should return diff for "height"', () => { const result = $data.compareObjects( { id: '000000000001', meta: { age: 10, }, }, { id: '000000000001', meta: { age: 10, height: 180, }, } ) const assert = { meta: { height: { oldValue: undefined, newValue: 180, }, }, } expect(result).toBeTruthy() expect(result).toEqual(assert) }) test('compareObjects should exclude "createdAt" diff', () => { const result = $data.compareObjects( { id: '000000000001', createdAt: 99999999, }, { id: '000000000001', createdAt: 88888888, }, ['createdAt'] ) expect(result).toBeFalsy() }) }) describe('validateJson', () => { it('should return valid when passing objects', () => { const json = { name: 'ACME Inc' } const schema = { type: 'object', properties: { name: { type: 'string', }, }, required: ['name'], additionalProperties: false, } const result = $data.validateJson(json, schema) expect(result).toEqual(true) }) it('should return valid when passing JSON', () => { const json = JSON.stringify({ name: 'ACME Inc' }) const schema = JSON.stringify({ type: 'object', properties: { name: { type: 'string', }, }, required: ['name'], additionalProperties: false, }) const result = $data.validateJson(json, schema) expect(result).toEqual(true) }) it('should fail additional properties', () => { const json = { name: 'ACME Inc', age: 10 } const schema = { type: 'object', properties: { name: { type: 'string', }, }, required: ['name'], additionalProperties: false, } const result = $data.validateJson(json, schema) expect(result).toBeTruthy() expect(result).toHaveLength(1) expect(result[0]).toEqual( 'instance is not allowed to have the additional property "age"' ) }) }) describe('String manipulation cases', () => { it('should return string in camel case', () => { const str = 'This is a WEIRD String' const result = $data.toCamelCase(str) expect(result).toBe('thisIsAWeirdString') }) it('should return snake case from camel case', () => { const str = 'camelCaseTest' const result = $data.camelToSnake(str) expect(result).toBe('camel-case-test') }) it('should return string without hyphens', () => { const str = 'camel-Case-Test' const result = $data.hyphenToNull(str) expect(result).toBe('camelCaseTest') }) it('should return string with spaces in place of hyphens', () => { const str = 'camel-Case-Test' const result = $data.hyphenToSpace(str) expect(result).toBe('camel Case Test') }) it('should return string with spaces in place of underscores', () => { const str = 'camel_Case_Test' const result = $data.underscoreToSpace(str) expect(result).toBe('camel Case Test') }) it('should return string with hyphens in place of spaces', () => { const str = 'camel Case Test' const result = $data.spaceToHyphen(str) expect(result).toBe('camel-Case-Test') }) it('should return string converting camel/pascal case to words with upper case', () => { const str = 'camelCaseTest' const result = $data.toRegularForm(str) expect(result).toBe('Camel Case Test') }) it('should return string converted to snake case with custom prefix', () => { const prefix = 'foo' const str = 'camelCaseTest' const result = $data.prefixSnakeCase(prefix, str) expect(result).toBe('foo-camel-case-test') }) it('should return initials for first and last name', () => { const str = 'john hancock' const result = $data.parseInitials(str) expect(result).toBe('JH') }) it('should return initials for first only', () => { const str = 'john' const result = $data.parseInitials(str) expect(result).toBe('JO') }) it('should return initials for first and last only', () => { const str = 'john hancock smith-barney' const result = $data.parseInitials(str) expect(result).toBe('JS') }) it('should return number ordinal in lowercase', () => { const num = 5 const result = $data.numToOrdinal(num, false) expect(result).toBe('5th') }) it('should return number ordinal in uppercase', () => { const num = 5 const result = $data.numToOrdinal(num, true) expect(result).toBe('5TH') }) }) })