objectUtils.test.ts 2.7 KB

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