utils.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { describe, expect, it } from 'vitest';
  6. import { Errors } from '@/types';
  7. import { FieldEventUtils, mergeFeedbacks } from '@/core/utils';
  8. describe('core/utils', () => {
  9. describe('mergeFeedbacks', () => {
  10. it('should merge when some key in source is empty array', () => {
  11. const origin = {
  12. a: ['error'],
  13. b: ['error'],
  14. } as unknown as Errors;
  15. const source = {
  16. a: [],
  17. } as unknown as Errors;
  18. const result = mergeFeedbacks(origin, source);
  19. expect(result).toEqual({
  20. a: [],
  21. b: ['error'],
  22. });
  23. });
  24. it('should merge when some key in source is undefined', () => {
  25. const origin = {
  26. a: ['error'],
  27. b: ['error'],
  28. } as unknown as Errors;
  29. const source = {
  30. a: undefined,
  31. } as unknown as Errors;
  32. const result = mergeFeedbacks(origin, source);
  33. expect(result).toEqual({
  34. a: undefined,
  35. b: ['error'],
  36. });
  37. });
  38. });
  39. describe('FieldEventUtils.shouldTriggerFieldChangeEvent', () => {
  40. it('array append: should not trigger for all array child or grand child', () => {
  41. expect(
  42. FieldEventUtils.shouldTriggerFieldChangeEvent(
  43. {
  44. values: {},
  45. prevValues: {},
  46. name: 'arr',
  47. options: {
  48. action: 'array-append',
  49. indexes: [0],
  50. },
  51. },
  52. 'arr.0',
  53. ),
  54. ).toBe(false);
  55. expect(
  56. FieldEventUtils.shouldTriggerFieldChangeEvent(
  57. {
  58. values: {},
  59. prevValues: {},
  60. name: 'arr',
  61. options: {
  62. action: 'array-append',
  63. indexes: [0],
  64. },
  65. },
  66. 'arr.0.x',
  67. ),
  68. ).toBe(false);
  69. expect(
  70. FieldEventUtils.shouldTriggerFieldChangeEvent(
  71. {
  72. values: {},
  73. prevValues: {},
  74. name: 'arr',
  75. options: {
  76. action: 'array-append',
  77. indexes: [0],
  78. },
  79. },
  80. 'arr',
  81. ),
  82. ).toBe(true);
  83. expect(
  84. FieldEventUtils.shouldTriggerFieldChangeEvent(
  85. {
  86. values: {},
  87. prevValues: {},
  88. name: 'p.arr',
  89. options: {
  90. action: 'array-append',
  91. indexes: [0],
  92. },
  93. },
  94. 'p',
  95. ),
  96. ).toBe(true);
  97. expect(
  98. FieldEventUtils.shouldTriggerFieldChangeEvent(
  99. {
  100. values: {},
  101. prevValues: {},
  102. name: '',
  103. options: {
  104. action: 'array-append',
  105. indexes: [0],
  106. },
  107. },
  108. '0',
  109. ),
  110. ).toBe(false);
  111. });
  112. it('array splice: should not trigger for array child or grand child only when index < first spliced index', () => {
  113. expect(
  114. FieldEventUtils.shouldTriggerFieldChangeEvent(
  115. {
  116. values: {},
  117. prevValues: {},
  118. name: 'arr',
  119. options: {
  120. action: 'array-splice',
  121. indexes: [0],
  122. },
  123. },
  124. 'arr.0',
  125. ),
  126. ).toBe(true);
  127. expect(
  128. FieldEventUtils.shouldTriggerFieldChangeEvent(
  129. {
  130. values: {},
  131. prevValues: {},
  132. name: 'arr',
  133. options: {
  134. action: 'array-splice',
  135. indexes: [0],
  136. },
  137. },
  138. 'arr.1',
  139. ),
  140. ).toBe(true);
  141. expect(
  142. FieldEventUtils.shouldTriggerFieldChangeEvent(
  143. {
  144. values: {},
  145. prevValues: {},
  146. name: 'arr',
  147. options: {
  148. action: 'array-splice',
  149. indexes: [1],
  150. },
  151. },
  152. 'arr.0',
  153. ),
  154. ).toBe(false);
  155. expect(
  156. FieldEventUtils.shouldTriggerFieldChangeEvent(
  157. {
  158. values: {},
  159. prevValues: {},
  160. name: 'arr',
  161. options: {
  162. action: 'array-splice',
  163. indexes: [1, 2],
  164. },
  165. },
  166. 'arr.1',
  167. ),
  168. ).toBe(true);
  169. expect(
  170. FieldEventUtils.shouldTriggerFieldChangeEvent(
  171. {
  172. values: {},
  173. prevValues: {},
  174. name: 'arr',
  175. options: {
  176. action: 'array-splice',
  177. indexes: [4, 5],
  178. },
  179. },
  180. 'arr.1',
  181. ),
  182. ).toBe(false);
  183. expect(
  184. FieldEventUtils.shouldTriggerFieldChangeEvent(
  185. {
  186. values: {},
  187. prevValues: {},
  188. name: 'arr',
  189. options: {
  190. action: 'array-splice',
  191. indexes: [],
  192. },
  193. },
  194. 'arr.1',
  195. ),
  196. ).toBe(true);
  197. });
  198. });
  199. });