flatten-object.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { describe, expect, it } from 'vitest';
  2. import { flattenObject } from './flatten-object';
  3. describe('flattenObject', () => {
  4. it('should flatten a nested object correctly', () => {
  5. const nestedObject = {
  6. language: 'en',
  7. notifications: {
  8. email: true,
  9. push: {
  10. sound: true,
  11. vibration: false,
  12. },
  13. },
  14. theme: 'light',
  15. };
  16. const expected = {
  17. language: 'en',
  18. notificationsEmail: true,
  19. notificationsPushSound: true,
  20. notificationsPushVibration: false,
  21. theme: 'light',
  22. };
  23. const result = flattenObject(nestedObject);
  24. expect(result).toEqual(expected);
  25. });
  26. it('should handle empty objects', () => {
  27. const nestedObject = {};
  28. const expected = {};
  29. const result = flattenObject(nestedObject);
  30. expect(result).toEqual(expected);
  31. });
  32. it('should handle objects with primitive values', () => {
  33. const nestedObject = {
  34. active: true,
  35. age: 30,
  36. name: 'Alice',
  37. };
  38. const expected = {
  39. active: true,
  40. age: 30,
  41. name: 'Alice',
  42. };
  43. const result = flattenObject(nestedObject);
  44. expect(result).toEqual(expected);
  45. });
  46. it('should handle objects with null values', () => {
  47. const nestedObject = {
  48. user: {
  49. age: null,
  50. name: null,
  51. },
  52. };
  53. const expected = {
  54. userAge: null,
  55. userName: null,
  56. };
  57. const result = flattenObject(nestedObject);
  58. expect(result).toEqual(expected);
  59. });
  60. it('should handle nested empty objects', () => {
  61. const nestedObject = {
  62. a: {},
  63. b: { c: {} },
  64. };
  65. const expected = {};
  66. const result = flattenObject(nestedObject);
  67. expect(result).toEqual(expected);
  68. });
  69. it('should handle arrays within objects', () => {
  70. const nestedObject = {
  71. hobbies: ['reading', 'gaming'],
  72. name: 'Alice',
  73. };
  74. const expected = {
  75. hobbies: ['reading', 'gaming'],
  76. name: 'Alice',
  77. };
  78. const result = flattenObject(nestedObject);
  79. expect(result).toEqual(expected);
  80. });
  81. it('should flatten objects with nested arrays correctly', () => {
  82. const nestedObject = {
  83. person: {
  84. hobbies: ['reading', 'gaming'],
  85. name: 'Alice',
  86. },
  87. };
  88. const expected = {
  89. personHobbies: ['reading', 'gaming'],
  90. personName: 'Alice',
  91. };
  92. const result = flattenObject(nestedObject);
  93. expect(result).toEqual(expected);
  94. });
  95. it('should handle objects with undefined values', () => {
  96. const nestedObject = {
  97. user: {
  98. age: undefined,
  99. name: 'Alice',
  100. },
  101. };
  102. const expected = {
  103. userAge: undefined,
  104. userName: 'Alice',
  105. };
  106. const result = flattenObject(nestedObject);
  107. expect(result).toEqual(expected);
  108. });
  109. });