index.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // 暂时未安装依赖,无法测试
  2. // @ts-ignore
  3. import { describe, expect, test } from 'vitest';
  4. import { deepMerge } from '@/utils';
  5. describe('deepMerge function', () => {
  6. test('should correctly merge basic data types', () => {
  7. const source = { a: 1, b: 2, c: null };
  8. const target = {
  9. a: 2,
  10. b: undefined,
  11. c: 3,
  12. };
  13. const expected = {
  14. a: 2,
  15. b: 2,
  16. c: 3,
  17. };
  18. expect(deepMerge(source, target)).toStrictEqual(expected);
  19. });
  20. test('should return the same date if only 1 is passed', () => {
  21. const foo = new Date();
  22. const merged = deepMerge(foo, null);
  23. const merged2 = deepMerge(undefined, foo);
  24. expect(merged).toStrictEqual(foo);
  25. expect(merged2).toStrictEqual(foo);
  26. expect(merged).toStrictEqual(merged2);
  27. });
  28. test('should merge two objects recursively', () => {
  29. const source = {
  30. a: { b: { c: 1 }, d: [1, 2] },
  31. e: [1, 2],
  32. foo: { bar: 3 },
  33. array: [
  34. {
  35. does: 'work',
  36. too: [1, 2, 3],
  37. },
  38. ],
  39. r: { a: 1 },
  40. };
  41. const target = {
  42. a: { b: { d: [3] } },
  43. e: [3],
  44. foo: { baz: 4 },
  45. qu: 5,
  46. array: [
  47. {
  48. does: 'work',
  49. too: [4, 5, 6],
  50. },
  51. {
  52. really: 'yes',
  53. },
  54. ],
  55. r: { a: 2 },
  56. };
  57. const expected = {
  58. a: { b: { c: 1, d: [3] }, d: [1, 2] },
  59. e: [3],
  60. foo: {
  61. bar: 3,
  62. baz: 4,
  63. },
  64. array: [
  65. {
  66. does: 'work',
  67. too: [4, 5, 6],
  68. },
  69. {
  70. really: 'yes',
  71. },
  72. ],
  73. qu: 5,
  74. r: { a: 2 },
  75. };
  76. expect(deepMerge(source, target)).toStrictEqual(expected);
  77. });
  78. test('should replace arrays by default', () => {
  79. const source = {
  80. a: { b: { d: [1, 2] } },
  81. e: [1, 2],
  82. };
  83. const target = {
  84. a: { b: { d: [3] } },
  85. e: [3],
  86. };
  87. const expected = {
  88. a: { b: { d: [3] } },
  89. e: [3],
  90. };
  91. expect(deepMerge(source, target)).toStrictEqual(expected);
  92. });
  93. test("should union arrays using mergeArrays = 'union'", () => {
  94. const source = {
  95. a: { b: { d: [1, 2] } },
  96. e: [1, 2],
  97. };
  98. const target = {
  99. a: { b: { d: [2, 3] } },
  100. e: [1, 3],
  101. };
  102. const expected = {
  103. a: { b: { d: [1, 2, 3] } },
  104. e: [1, 2, 3],
  105. };
  106. expect(deepMerge(source, target, 'union')).toStrictEqual(expected);
  107. });
  108. test("should intersect arrays using mergeArrays = 'intersection'", () => {
  109. const source = {
  110. a: { b: { d: [1, 2] } },
  111. e: [1, 2],
  112. };
  113. const target = {
  114. a: { b: { d: [2, 3] } },
  115. e: [3],
  116. };
  117. const expected = {
  118. a: { b: { d: [2] } },
  119. e: [],
  120. };
  121. expect(deepMerge(source, target, 'intersection')).toStrictEqual(expected);
  122. });
  123. test("should concatenate arrays using mergeArrays = 'concat'", () => {
  124. const source = {
  125. a: { b: { d: [1, 2] } },
  126. e: [1, 2],
  127. };
  128. const target = {
  129. a: { b: { d: [2, 3] } },
  130. e: [3],
  131. };
  132. const expected = {
  133. a: { b: { d: [1, 2, 2, 3] } },
  134. e: [1, 2, 3],
  135. };
  136. expect(deepMerge(source, target, 'concat')).toStrictEqual(expected);
  137. });
  138. });