objectUtils.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { describe, test, it, expect } from 'vitest'
  2. import ObjectUtils from '~/services/utils/objectUtils'
  3. import StringUtils from '~/services/utils/stringUtils'
  4. describe('cloneAndFlatten', () => {
  5. test('If the object is already flat, it should return an identical object', () => {
  6. expect(ObjectUtils.cloneAndFlatten({ a: 1, b: 2 })).toEqual({ a: 1, b: 2 })
  7. })
  8. test('With a nested object, it should return an flat object', () => {
  9. expect(ObjectUtils.cloneAndFlatten({ a: 1, b: { c: 3 } })).toEqual({
  10. a: 1,
  11. 'b.c': 3,
  12. })
  13. })
  14. test('With an empty object, it should return an empty object', () => {
  15. expect(ObjectUtils.cloneAndFlatten({})).toEqual({})
  16. })
  17. // test('With an non-object, it should throw an error', () => {
  18. // // TODO: comment ça pourrait ne pas être un objet?
  19. // expect(ObjectUtils.cloneAndFlatten([])).toThrowError('Expecting an object parameter')
  20. // })
  21. })
  22. describe('cloneAndNest', () => {
  23. test('If the object is already nested, it should return an identical object', () => {
  24. expect(ObjectUtils.cloneAndNest({ a: 1, b: { c: 3 } })).toEqual({
  25. a: 1,
  26. b: { c: 3 },
  27. })
  28. })
  29. test('With a flatten object, it should return an nested object', () => {
  30. expect(ObjectUtils.cloneAndNest({ a: 1, 'b.c': 3 })).toEqual({
  31. a: 1,
  32. b: { c: 3 },
  33. })
  34. })
  35. test('With an empty object, it should return an empty object', () => {
  36. expect(ObjectUtils.cloneAndNest({})).toEqual({})
  37. })
  38. })
  39. describe('isObject', () => {
  40. test('with object', () => {
  41. expect(ObjectUtils.isObject({ a: 1 })).toBeTruthy()
  42. })
  43. test('with array', () => {
  44. expect(ObjectUtils.isObject([1, 2, 3])).toBeFalsy()
  45. })
  46. test('with null', () => {
  47. expect(ObjectUtils.isObject(null)).toBeFalsy()
  48. })
  49. test('with date', () => {
  50. expect(ObjectUtils.isObject(new Date())).toBeFalsy()
  51. })
  52. })
  53. describe('clone', () => {
  54. test('with simple object', () => {
  55. const initial = { a: 1 }
  56. const result = ObjectUtils.clone(initial)
  57. result.b = 2
  58. expect(initial).toEqual({ a: 1 })
  59. })
  60. test('with empty object', () => {
  61. expect(ObjectUtils.clone({})).toEqual({})
  62. })
  63. test('with nested object', () => {
  64. expect(ObjectUtils.clone({ a: 1, b: { c: 3 } })).toEqual({
  65. a: 1,
  66. b: { c: 3 },
  67. })
  68. })
  69. })
  70. describe('sortObjectsByKey', () => {
  71. test('with simple object', () => {
  72. expect(ObjectUtils.sortObjectsByKey({ b: 1, d: 2, c: 3, a: 4 })).toEqual({
  73. a: 4,
  74. b: 1,
  75. c: 3,
  76. d: 2,
  77. })
  78. })
  79. test('with empty object', () => {
  80. expect(ObjectUtils.sortObjectsByKey({})).toEqual({})
  81. })
  82. // test('with non-object', () => {
  83. // // TODO: comment ça pourrait ne pas être un objet?
  84. // expect(ObjectUtils.sortObjectsByKey({})).toThrowError('Expecting an object parameter')
  85. // })
  86. })
  87. describe('hash', () => {
  88. test('empty object', async () => {
  89. expect(await ObjectUtils.hash({})).toBe(
  90. 'bf21a9e8fbc5a3846fb05b4fa0859e0917b2202f',
  91. )
  92. })
  93. test('simple object', async () => {
  94. expect(await ObjectUtils.hash({ a: 1, b: 2, c: 3 })).toBe(
  95. 'e7ec4a8f2309bdd4c4c57cb2adfb79c91a293597',
  96. )
  97. })
  98. test('simple unsorted object', async () => {
  99. expect(await ObjectUtils.hash({ b: 2, a: 1, c: 3 })).toBe(
  100. 'e7ec4a8f2309bdd4c4c57cb2adfb79c91a293597',
  101. )
  102. })
  103. })