field-model.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { beforeEach, describe, expect, it, vi } from 'vitest';
  6. import { Errors, FeedbackLevel, ValidateTrigger } from '@/types';
  7. import { FormModel } from '@/core/form-model';
  8. describe('FieldModel', () => {
  9. let formModel = new FormModel();
  10. describe('state', () => {
  11. beforeEach(() => {
  12. formModel.dispose();
  13. formModel = new FormModel();
  14. });
  15. it('can bubble', () => {
  16. formModel.createField('parent');
  17. formModel.createField('parent.child');
  18. const childField = formModel.getField('parent.child')!;
  19. const parentField = formModel.getField('parent')!;
  20. childField.value = 1;
  21. expect(childField.state.isTouched).toBe(true);
  22. expect(parentField.state.isTouched).toBe(true);
  23. expect(formModel.state.isTouched).toBe(true);
  24. });
  25. it('can bubble with array', () => {
  26. formModel.createField('parent');
  27. formModel.createField('parent.arr');
  28. formModel.createField('parent.arr.1');
  29. const arrChild = formModel.getField('parent.arr.1')!;
  30. const arrField = formModel.getField('parent.arr')!;
  31. const parentField = formModel.getField('parent')!;
  32. arrChild.value = 1;
  33. expect(arrChild.state.isTouched).toBe(true);
  34. expect(arrField.state.isTouched).toBe(true);
  35. expect(parentField.state.isTouched).toBe(true);
  36. expect(formModel.state.isTouched).toBe(true);
  37. });
  38. it('do not set isTouched for init value set', () => {
  39. formModel.createField('parent');
  40. formModel.createField('parent.child');
  41. const childField = formModel.getField('parent.child')!;
  42. const parentField = formModel.getField('parent')!;
  43. expect(childField.state.isTouched).toBe(false);
  44. expect(parentField.state.isTouched).toBe(false);
  45. expect(formModel.state.isTouched).toBe(false);
  46. });
  47. });
  48. describe('validate', () => {
  49. beforeEach(() => {
  50. formModel.dispose();
  51. formModel = new FormModel();
  52. });
  53. it('when validate func return only a message', async () => {
  54. formModel.init({ validate: { 'parent.*': () => 'some message' } });
  55. formModel.createField('parent');
  56. formModel.createField('parent.child');
  57. const childField = formModel.getField('parent.child')!;
  58. expect(childField.state.errors).toBeUndefined();
  59. await childField.validate();
  60. expect(childField.state.errors?.['parent.child'][0].message).toBe('some message');
  61. expect(childField.state.errors?.['parent.child'][0].level).toBe(FeedbackLevel.Error);
  62. });
  63. it('when validate func return a FieldWarning', async () => {
  64. formModel.init({
  65. validate: {
  66. 'parent.*': () => ({
  67. level: FeedbackLevel.Warning,
  68. message: 'some message',
  69. }),
  70. },
  71. });
  72. formModel.createField('parent');
  73. formModel.createField('parent.child');
  74. const childField = formModel.getField('parent.child')!;
  75. expect(childField.state.errors).toBeUndefined();
  76. await childField.validate();
  77. expect(childField.state.warnings?.['parent.child'][0].message).toBe('some message');
  78. expect(childField.state.warnings?.['parent.child'][0].level).toBe(FeedbackLevel.Warning);
  79. });
  80. it('when validate return a FormError', async () => {
  81. formModel.init({
  82. validate: {
  83. 'parent.*': () => ({
  84. level: FeedbackLevel.Error,
  85. message: 'some message',
  86. }),
  87. },
  88. });
  89. formModel.createField('parent');
  90. formModel.createField('parent.child');
  91. const childField = formModel.getField('parent.child')!;
  92. expect(childField.state.errors?.length).toBeUndefined();
  93. await childField.validate();
  94. expect(childField.state.errors?.['parent.child'][0].message).toBe('some message');
  95. expect(childField.state.errors?.['parent.child'][0].level).toBe(FeedbackLevel.Error);
  96. });
  97. it('should bubble errors to parent field', async () => {
  98. formModel.init({
  99. validate: {
  100. 'parent.*': () => ({
  101. level: FeedbackLevel.Error,
  102. message: 'some message',
  103. }),
  104. },
  105. });
  106. formModel.createField('parent');
  107. formModel.createField('parent.child');
  108. const childField = formModel.getField('parent.child')!;
  109. const parentField = formModel.getField('parent')!;
  110. await childField.validate();
  111. expect(parentField.state.errors?.['parent.child'][0].message).toBe('some message');
  112. expect(parentField.state.errors?.['parent.child'][0].level).toBe(FeedbackLevel.Error);
  113. });
  114. it('should bubble errors to form', async () => {
  115. formModel.init({
  116. validate: {
  117. 'parent.*': () => ({
  118. level: FeedbackLevel.Error,
  119. message: 'some message',
  120. }),
  121. },
  122. });
  123. formModel.createField('parent');
  124. formModel.createField('parent.child');
  125. const childField = formModel.getField('parent.child')!;
  126. await childField.validate();
  127. expect(formModel.state.errors?.['parent.child'][0].message).toBe('some message');
  128. expect(formModel.state.errors?.['parent.child'][0].level).toBe(FeedbackLevel.Error);
  129. });
  130. it('should correctly set and bubble invalid', async () => {
  131. formModel.init({
  132. validate: {
  133. 'parent.*': () => ({
  134. level: FeedbackLevel.Error,
  135. message: 'some message',
  136. }),
  137. },
  138. });
  139. const parent = formModel.createField('parent');
  140. const child = formModel.createField('parent.child');
  141. await child.validate();
  142. expect(child.state.invalid).toBe(true);
  143. expect(parent.state.invalid).toBe(true);
  144. expect(formModel.state.invalid).toBe(true);
  145. });
  146. it('should validate self ancestors and child', async () => {
  147. formModel.init({
  148. validateTrigger: ValidateTrigger.onChange,
  149. });
  150. const root = formModel.createField('root');
  151. const l1 = formModel.createField('root.l1');
  152. const l2 = formModel.createField('root.l1.l2');
  153. const l3 = formModel.createField('root.l1.l2.l3');
  154. const l4 = formModel.createField('root.l1.l2.l3.l4');
  155. const other = formModel.createField('root.other');
  156. vi.spyOn(root, 'validate');
  157. vi.spyOn(l1, 'validate');
  158. vi.spyOn(l2, 'validate');
  159. vi.spyOn(l3, 'validate');
  160. vi.spyOn(l4, 'validate');
  161. vi.spyOn(other, 'validate');
  162. formModel.setValueIn('root.l1.l2', 1);
  163. expect(root.validate).toHaveBeenCalledTimes(1);
  164. expect(l1.validate).toHaveBeenCalledTimes(1);
  165. expect(l2.validate).toHaveBeenCalledTimes(1);
  166. expect(l3.validate).toHaveBeenCalledTimes(1);
  167. expect(l4.validate).toHaveBeenCalledTimes(1);
  168. expect(other.validate).toHaveBeenCalledTimes(0);
  169. });
  170. it('should validate when multiple pattern match ', async () => {
  171. const validate1 = vi.fn();
  172. const validate2 = vi.fn();
  173. formModel.init({
  174. validateTrigger: ValidateTrigger.onChange,
  175. validate: {
  176. 'a.*.input': validate1,
  177. 'a.1.input': validate2,
  178. },
  179. initialValues: {
  180. a: [{ input: '0' }, { input: '1' }],
  181. },
  182. });
  183. const root = formModel.createField('a');
  184. const i0 = formModel.createField('a.0.input');
  185. const i1 = formModel.createField('a.1.input');
  186. formModel.setValueIn('a.1.input', 'xxx');
  187. expect(validate1).toHaveBeenCalledTimes(1);
  188. expect(validate2).toHaveBeenCalledTimes(1);
  189. });
  190. // 暂时注释了从 parent 触发validate 的能力,所以注释这个单测
  191. // it('can trigger validate from parent', async () => {
  192. // formModel.init({
  193. // validate: {
  194. // 'parent.child1': () => ({
  195. // level: FeedbackLevel.Error,
  196. // message: 'error',
  197. // }),
  198. // 'parent.child2': () => ({
  199. // level: FeedbackLevel.Warning,
  200. // message: 'warning',
  201. // }),
  202. // },
  203. // });
  204. // const parent = formModel.createField('parent');
  205. // formModel.createField('parent.child1');
  206. // formModel.createField('parent.child2');
  207. //
  208. // await parent.validate();
  209. //
  210. // expect(formModel.state.errors?.['parent.child1'][0].message).toBe('error');
  211. // expect(formModel.state.warnings?.['parent.child2'][0].level).toBe('warning');
  212. // });
  213. });
  214. describe('onValueChange', () => {
  215. let formEffect = vi.fn();
  216. beforeEach(() => {
  217. formModel.dispose();
  218. formModel = new FormModel();
  219. formEffect = vi.fn();
  220. formModel.onFormValuesChange(formEffect);
  221. });
  222. it('should bubble value change', () => {
  223. const parent = formModel.createField('parent');
  224. const child1 = formModel.createField('parent.child1');
  225. const childOnChange = vi.fn();
  226. const parentOnChange = vi.fn();
  227. child1.onValueChange(childOnChange);
  228. parent.onValueChange(parentOnChange);
  229. child1.value = 1;
  230. expect(parentOnChange).toHaveBeenCalledTimes(1);
  231. expect(childOnChange).toHaveBeenCalledTimes(1);
  232. expect(formEffect).toHaveBeenCalledTimes(1);
  233. });
  234. it('should bubble value change in array when delete', () => {
  235. const parent = formModel.createField('parent');
  236. const arr = formModel.createFieldArray('parent.arr');
  237. const item1 = formModel.createField('parent.arr.0');
  238. const parentOnChange = vi.fn();
  239. const arrOnChange = vi.fn();
  240. const item1OnChange = vi.fn();
  241. parent.onValueChange(parentOnChange);
  242. arr.onValueChange(arrOnChange);
  243. item1.onValueChange(item1OnChange);
  244. formModel.setValueIn('parent.arr.0', 1);
  245. arr.delete(0);
  246. expect(item1OnChange).toHaveBeenCalledTimes(2);
  247. expect(arrOnChange).toHaveBeenCalledTimes(2);
  248. expect(parentOnChange).toHaveBeenCalledTimes(2);
  249. });
  250. it('should bubble value change in array when append', () => {
  251. const parent = formModel.createField('parent');
  252. const arr = formModel.createFieldArray('parent.arr');
  253. const parentOnChange = vi.fn();
  254. const arrOnChange = vi.fn();
  255. parent.onValueChange(parentOnChange);
  256. arr.onValueChange(arrOnChange);
  257. arr.append('1');
  258. expect(arrOnChange).toHaveBeenCalledTimes(1);
  259. expect(parentOnChange).toHaveBeenCalledTimes(1);
  260. expect(formEffect).toHaveBeenCalledTimes(1);
  261. });
  262. it('should not trigger child field change when array append', () => {
  263. formModel.createField('parent');
  264. const arr = formModel.createFieldArray('parent.arr');
  265. const item0 = formModel.createField('parent.arr.0');
  266. const item0x = formModel.createField('parent.arr.0.x');
  267. const item0OnChange = vi.fn();
  268. const item0xOnChange = vi.fn();
  269. item0.onValueChange(item0OnChange);
  270. item0x.onValueChange(item0xOnChange);
  271. arr.append('1');
  272. expect(item0OnChange).toHaveBeenCalledTimes(0);
  273. expect(item0xOnChange).toHaveBeenCalledTimes(0);
  274. });
  275. it('should clear and fire change', () => {
  276. const parent = formModel.createField('parent');
  277. const child1 = formModel.createField('parent.child1');
  278. const child1OnChange = vi.fn();
  279. const parentOnChange = vi.fn();
  280. child1.onValueChange(child1OnChange);
  281. parent.onValueChange(parentOnChange);
  282. formModel.setValueIn('parent.child1', 1);
  283. child1.clear();
  284. expect(child1OnChange).toHaveBeenCalledTimes(2);
  285. expect(parentOnChange).toHaveBeenCalledTimes(2);
  286. expect(formEffect).toHaveBeenCalledTimes(2);
  287. });
  288. it('should bubble change in array delete', () => {
  289. const arr = formModel.createFieldArray('arr');
  290. const child1 = formModel.createField('arr.0');
  291. const childOnChange = vi.fn();
  292. const arrOnChange = vi.fn();
  293. child1.onValueChange(childOnChange);
  294. arr.onValueChange(arrOnChange);
  295. formModel.setValueIn('arr.0', 1);
  296. arr.delete(0);
  297. expect(childOnChange).toHaveBeenCalledTimes(2);
  298. expect(arrOnChange).toHaveBeenCalledTimes(2);
  299. // formModel.setValueIn 一次,arr.delete 中 arr 本身触发一次
  300. expect(formEffect).toHaveBeenCalledTimes(2);
  301. });
  302. it('should bubble change in array append', () => {
  303. const arr = formModel.createFieldArray('arr');
  304. const item0 = formModel.createField('arr.0');
  305. const item0OnChange = vi.fn();
  306. const arrOnChange = vi.fn();
  307. item0.onValueChange(item0OnChange);
  308. arr.onValueChange(arrOnChange);
  309. formModel.setValueIn('arr.0', 'a');
  310. arr.append('b');
  311. expect(item0OnChange).toHaveBeenCalledTimes(1);
  312. });
  313. it('should ignore unchanged items when array delete', () => {
  314. const other = formModel.createField('other');
  315. const parent = formModel.createField('parent');
  316. const arr = formModel.createFieldArray('parent.arr');
  317. const item0 = formModel.createField('parent.arr.0');
  318. const item1 = formModel.createField('parent.arr.1');
  319. const item2 = formModel.createField('parent.arr.2');
  320. formModel.setValueIn('parent.arr', [1, 2, 3]);
  321. const item0OnChange = vi.fn();
  322. const item1OnChange = vi.fn();
  323. const item2OnChange = vi.fn();
  324. const arrOnChange = vi.fn();
  325. const parentOnChange = vi.fn();
  326. const otherOnChange = vi.fn();
  327. item0.onValueChange(item0OnChange);
  328. item1.onValueChange(item1OnChange);
  329. item2.onValueChange(item2OnChange);
  330. arr.onValueChange(arrOnChange);
  331. parent.onValueChange(parentOnChange);
  332. other.onValueChange(otherOnChange);
  333. arr.delete(1);
  334. expect(arrOnChange).toHaveBeenCalledTimes(1);
  335. expect(parentOnChange).toHaveBeenCalledTimes(1);
  336. expect(item0OnChange).not.toHaveBeenCalled();
  337. expect(item1OnChange).toHaveBeenCalledTimes(1);
  338. expect(item2OnChange).toHaveBeenCalledTimes(1);
  339. expect(otherOnChange).not.toHaveBeenCalled();
  340. });
  341. });
  342. describe('dispose', () => {
  343. beforeEach(() => {
  344. formModel.dispose();
  345. formModel = new FormModel();
  346. });
  347. it('should correctly cleanup when field dispose', () => {
  348. const parent = formModel.createField('parent');
  349. const child1 = formModel.createField('parent.child1');
  350. child1.state.errors = { 'parent.child1': 'errors' } as unknown as Errors;
  351. child1.bubbleState();
  352. expect(formModel.state.errors?.['parent.child1']).toEqual('errors');
  353. expect(parent.state.errors?.['parent.child1']).toEqual('errors');
  354. parent.dispose();
  355. // Ref 'dispose' method in field-model.ts
  356. // 1. expect state has been cleared
  357. // expect(child1.state.errors).toBeUndefined();
  358. // expect(parent.state.errors?.['parent.child1']).toBeUndefined();
  359. // 2. expect field model has been cleared
  360. expect(formModel.fieldMap.get('parent')).toBeUndefined();
  361. expect(formModel.fieldMap.get('parent.child1')).toBeUndefined();
  362. });
  363. });
  364. });