nested-object.test.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { describe, expect, it } from 'vitest';
  2. import { nestedObject } from './nested-object';
  3. describe('nestedObject', () => {
  4. it('should convert flat object to nested object with level 1', () => {
  5. const flatObject = {
  6. anotherKeyExample: 2,
  7. commonAppName: 1,
  8. someOtherKey: 3,
  9. };
  10. const expectedNestedObject = {
  11. anotherKeyExample: 2,
  12. commonAppName: 1,
  13. someOtherKey: 3,
  14. };
  15. expect(nestedObject(flatObject, 1)).toEqual(expectedNestedObject);
  16. });
  17. it('should convert flat object to nested object with level 2', () => {
  18. const flatObject = {
  19. appAnotherKeyExample: 2,
  20. appCommonName: 1,
  21. appSomeOtherKey: 3,
  22. };
  23. const expectedNestedObject = {
  24. app: {
  25. anotherKeyExample: 2,
  26. commonName: 1,
  27. someOtherKey: 3,
  28. },
  29. };
  30. expect(nestedObject(flatObject, 2)).toEqual(expectedNestedObject);
  31. });
  32. it('should convert flat object to nested object with level 3', () => {
  33. const flatObject = {
  34. appAnotherKeyExampleValue: 2,
  35. appCommonNameKey: 1,
  36. appSomeOtherKeyItem: 3,
  37. };
  38. const expectedNestedObject = {
  39. app: {
  40. another: {
  41. keyExampleValue: 2,
  42. },
  43. common: {
  44. nameKey: 1,
  45. },
  46. some: {
  47. otherKeyItem: 3,
  48. },
  49. },
  50. };
  51. expect(nestedObject(flatObject, 3)).toEqual(expectedNestedObject);
  52. });
  53. it('should handle empty object', () => {
  54. const flatObject = {};
  55. const expectedNestedObject = {};
  56. expect(nestedObject(flatObject, 1)).toEqual(expectedNestedObject);
  57. });
  58. it('should handle single key object', () => {
  59. const flatObject = {
  60. singleKey: 1,
  61. };
  62. const expectedNestedObject = {
  63. singleKey: 1,
  64. };
  65. expect(nestedObject(flatObject, 1)).toEqual(expectedNestedObject);
  66. });
  67. it('should handle complex keys', () => {
  68. const flatObject = {
  69. anotherComplexKeyWithParts: 2,
  70. complexKeyWithMultipleParts: 1,
  71. };
  72. const expectedNestedObject = {
  73. anotherComplexKeyWithParts: 2,
  74. complexKeyWithMultipleParts: 1,
  75. };
  76. expect(nestedObject(flatObject, 1)).toEqual(expectedNestedObject);
  77. });
  78. it('should correctly nest an object based on the specified level', () => {
  79. const obj = {
  80. oneFiveSix: 'Value156',
  81. oneTwoFour: 'Value124',
  82. oneTwoThree: 'Value123',
  83. };
  84. const nested = nestedObject(obj, 2);
  85. expect(nested).toEqual({
  86. one: {
  87. fiveSix: 'Value156',
  88. twoFour: 'Value124',
  89. twoThree: 'Value123',
  90. },
  91. });
  92. });
  93. });