objectUtils.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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({'a': 1, 'b.c': 3})
  9. })
  10. test('With an empty object, it should return an empty object', () => {
  11. expect(ObjectUtils.cloneAndFlatten({})).toEqual({})
  12. })
  13. // test('With an non-object, it should throw an error', () => {
  14. // // TODO: comment ça pourrait ne pas être un objet?
  15. // expect(ObjectUtils.cloneAndFlatten([])).toThrowError('Expecting an object parameter')
  16. // })
  17. })
  18. describe('cloneAndNest', () => {
  19. test('If the object is already nested, it should return an identical object', () => {
  20. expect(ObjectUtils.cloneAndNest({'a': 1, 'b': { 'c': 3 }})).toEqual({'a': 1, 'b': { 'c': 3 }})
  21. })
  22. test('With a flatten object, it should return an nested object', () => {
  23. expect(ObjectUtils.cloneAndNest({'a': 1, 'b.c': 3})).toEqual({'a': 1, 'b': { 'c': 3 }})
  24. })
  25. test('With an empty object, it should return an empty object', () => {
  26. expect(ObjectUtils.cloneAndNest({})).toEqual({})
  27. })
  28. })
  29. describe('isObject', () => {
  30. test('with object', () => {
  31. expect(ObjectUtils.isObject({ 'a': 1 })).toBeTruthy()
  32. })
  33. test('with array', () => {
  34. expect(ObjectUtils.isObject([ 1, 2, 3 ])).toBeFalsy()
  35. })
  36. test('with null', () => {
  37. expect(ObjectUtils.isObject(null)).toBeFalsy()
  38. })
  39. test('with date', () => {
  40. expect(ObjectUtils.isObject(new Date())).toBeFalsy()
  41. })
  42. })
  43. describe('clone', () => {
  44. test('with simple object', () => {
  45. const initial = { 'a': 1 }
  46. let result = ObjectUtils.clone(initial)
  47. result['b'] = 2
  48. expect(initial).toEqual({ 'a': 1 })
  49. })
  50. test('with empty object', () => {
  51. expect(ObjectUtils.clone({})).toEqual({})
  52. })
  53. test('with nested object', () => {
  54. expect(ObjectUtils.clone({'a': 1, 'b': { 'c': 3 }})).toEqual({'a': 1, 'b': { 'c': 3 }})
  55. })
  56. })
  57. describe('sortObjectsByKey', () => {
  58. test('with simple object', () => {
  59. expect(ObjectUtils.sortObjectsByKey({b:1, d:2, c:3, a:4})).toEqual({a:4, b:1, c:3, d:2})
  60. })
  61. test('with empty object', () => {
  62. expect(ObjectUtils.sortObjectsByKey({})).toEqual({})
  63. })
  64. // test('with non-object', () => {
  65. // // TODO: comment ça pourrait ne pas être un objet?
  66. // expect(ObjectUtils.sortObjectsByKey({})).toThrowError('Expecting an object parameter')
  67. // })
  68. })