objectProperties.spec.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { $objectProperties } from '~/services/utils/objectProperties'
  2. import { AnyJson } from '~/types/interfaces'
  3. describe('cloneAndFlatten()', () => {
  4. it('should throw an error if args is not an object', () =>
  5. expect(() => $objectProperties.cloneAndFlatten(String as AnyJson)).toThrowError('Expecting an object parameter')
  6. )
  7. it('should return same values for flat objects', () =>
  8. expect($objectProperties.cloneAndFlatten({ foo: 1, bar: 'baz' })).toStrictEqual({ foo: 1, bar: 'baz' })
  9. )
  10. it('should copy null values', () =>
  11. expect($objectProperties.cloneAndFlatten({ foo: null })).toStrictEqual({ foo: null })
  12. )
  13. it('should clone the input', () => {
  14. const object = { foo: 1, bar: 'baz' }
  15. const flatObject = $objectProperties.cloneAndFlatten(object)
  16. expect(flatObject).not.toBe(object)
  17. flatObject.foo = 2
  18. expect(object.foo).toEqual(1)
  19. })
  20. it('should flatten nested objects', () =>
  21. expect($objectProperties.cloneAndFlatten({ a: 1, b: { c: 2 }, d: { e: 3, f: { g: 4, h: 5 } } })).toStrictEqual({ a: 1, 'b.c': 2, 'd.e': 3, 'd.f.g': 4, 'd.f.h': 5 })
  22. )
  23. it('should not flatten arrays', () =>
  24. expect($objectProperties.cloneAndFlatten({ a: [1, 2, 3] })).toStrictEqual({ a: [1, 2, 3] })
  25. )
  26. it('should not flatten strings', () =>
  27. expect($objectProperties.cloneAndFlatten({ a: 'hello, world' })).toStrictEqual({ a: 'hello, world' })
  28. )
  29. it('should not flatten dates', () => {
  30. const d = new Date()
  31. expect($objectProperties.cloneAndFlatten({ a: d })).toStrictEqual({ a: d })
  32. })
  33. it('should not flatten excluded properties', () =>
  34. expect($objectProperties.cloneAndFlatten({ a: 1, b: { c: 2 }, d: { e: 3, f: { g: 4, h: 5 } } }, ['d'])).toStrictEqual({ a: 1, 'b.c': 2, d: { e: 3, f: { g: 4, h: 5 } } })
  35. )
  36. })
  37. describe('cloneAndNest()', () => {
  38. it('should throw an error if args is not an object', () =>
  39. expect(() => $objectProperties.cloneAndNest(String as AnyJson)).toThrowError('Expecting an object parameter')
  40. )
  41. it('should return same values for flat objects', () =>
  42. expect($objectProperties.cloneAndNest({ foo: 1, bar: 'baz' })).toStrictEqual({ foo: 1, bar: 'baz' })
  43. )
  44. it('should copy null values', () =>
  45. expect($objectProperties.cloneAndNest({ foo: null })).toStrictEqual({ foo: null })
  46. )
  47. it('should clone the input', () => {
  48. const object = { foo: 1, bar: 'baz' }
  49. const nestedObject = $objectProperties.cloneAndNest(object)
  50. expect(nestedObject).not.toBe(object)
  51. nestedObject.foo = 2
  52. expect(object.foo).toEqual(1)
  53. })
  54. it('should nest flattened objects', () =>
  55. expect($objectProperties.cloneAndNest({ a: 1, 'b.c': 2, 'd.e': 3, 'd.f.g': 4, 'd.f.h': 5 })).toStrictEqual({ a: 1, b: { c: 2 }, d: { e: 3, f: { g: 4, h: 5 } } })
  56. )
  57. it('should not error on null nested objects', () =>
  58. expect($objectProperties.cloneAndNest({ a: null, 'a.b': null })).toStrictEqual({ a: null })
  59. )
  60. })
  61. describe('sortObjectByKey()', () => {
  62. it('should throw an error if args is not an array', () =>
  63. expect(() => $objectProperties.sortObjectByKey('foo')).toThrowError('Expecting an object parameter')
  64. )
  65. it('should sort an array by his keys', () =>
  66. expect($objectProperties.sortObjectByKey({ b: 1, d: 2, c: 3, a: 4 })).toStrictEqual({ a: 4, b: 1, c: 3, d: 2 })
  67. )
  68. })