objectProperties.spec.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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)).toThrow()
  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. let object = { foo: 1, bar: 'baz'};
  15. let 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. let 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)).toThrow()
  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. let object = { foo: 1, bar: 'baz'};
  49. let 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. });