@@ -53,8 +53,8 @@ const BasicForm = (props: FormProps & { operation }) => {
5353 ) ;
5454} ;
5555
56- describe ( 'Form List 组件测试' , ( ) => {
57- test ( 'form list 测试 ' , async ( ) => {
56+ describe ( 'FormList 组件测试' , ( ) => {
57+ test ( 'FormList basic API ' , async ( ) => {
5858 const TestView = ( ) => {
5959 const [ form ] = Form . useForm ( ) ;
6060
@@ -143,7 +143,7 @@ describe('Form List 组件测试', () => {
143143 expect ( queryByText ( '地区必填' ) ) . not . toBeTruthy ( ) ;
144144 } ) ;
145145
146- test ( 'reset to initial data' , async ( ) => {
146+ test ( 'FormList reset to initial data' , async ( ) => {
147147 const TestView = ( ) => {
148148 const [ form ] = Form . useForm ( ) ;
149149
@@ -165,14 +165,40 @@ describe('Form List 组件测试', () => {
165165 } ;
166166
167167 const { container, queryByText, getByPlaceholderText } = render ( < TestView /> ) ;
168- const resetBtn = queryByText ( 'reset' ) ;
169168
170- const removeBtn = container . querySelector ( '.test-remove-0' ) ;
171- fireEvent . click ( removeBtn ) ;
169+ // 验证初始数据渲染正确
170+ expect ( ( getByPlaceholderText ( 'area-input-0' ) as HTMLInputElement ) . value ) . toBe ( 'shenzhen' ) ;
171+ expect ( ( getByPlaceholderText ( 'area-input-1' ) as HTMLInputElement ) . value ) . toBe ( 'beijing' ) ;
172+
173+ // 删除 beijing
174+ const removeBtn1 = container . querySelector ( '.test-remove-1' ) ;
175+ fireEvent . click ( removeBtn1 ) ;
172176 await mockTimeout ( ( ) => true ) ;
173- expect ( ( getByPlaceholderText ( 'area-input-0' ) as HTMLInputElement ) . value ) . toBe ( 'beijing' ) ;
177+ // 只剩 shenzhen
178+ expect ( ( getByPlaceholderText ( 'area-input-0' ) as HTMLInputElement ) . value ) . toBe ( 'shenzhen' ) ;
179+ expect ( container . querySelector ( '[placeholder="area-input-1"]' ) ) . toBeFalsy ( ) ;
180+
181+ // 添加空数据
182+ const addBtn = container . querySelector ( '#test-add' ) ;
183+ fireEvent . click ( addBtn ) ;
184+ await mockTimeout ( ( ) => true ) ;
185+ expect ( ( getByPlaceholderText ( 'area-input-0' ) as HTMLInputElement ) . value ) . toBe ( 'shenzhen' ) ;
186+ expect ( container . querySelector ( '[placeholder="area-input-1"]' ) ) . toBeTruthy ( ) ;
187+ expect ( ( getByPlaceholderText ( 'area-input-1' ) as HTMLInputElement ) . value ) . toBe ( '' ) ;
188+
189+ // 再删除 shenzhen
190+ const removeBtn0 = container . querySelector ( '.test-remove-0' ) ;
191+ fireEvent . click ( removeBtn0 ) ;
192+ await mockTimeout ( ( ) => true ) ;
193+ expect ( ( getByPlaceholderText ( 'area-input-0' ) as HTMLInputElement ) . value ) . toBe ( '' ) ;
194+ expect ( container . querySelector ( '[placeholder="area-input-1"]' ) ) . toBeFalsy ( ) ;
195+
196+ // 点击 reset 重置
197+ const resetBtn = queryByText ( 'reset' ) ;
174198 fireEvent . click ( resetBtn ) ;
175199 await mockTimeout ( ( ) => true ) ;
200+
201+ // 恢复到初始化数据
176202 expect ( ( getByPlaceholderText ( 'area-input-0' ) as HTMLInputElement ) . value ) . toBe ( 'shenzhen' ) ;
177203 expect ( ( getByPlaceholderText ( 'area-input-1' ) as HTMLInputElement ) . value ) . toBe ( 'beijing' ) ;
178204 } ) ;
@@ -220,7 +246,7 @@ describe('Form List 组件测试', () => {
220246 expect ( fn ) . toHaveBeenCalledTimes ( 1 ) ;
221247 } ) ;
222248
223- test ( 'Multiple nested FormList ' , async ( ) => {
249+ test ( 'FormList with nested structures ' , async ( ) => {
224250 const TestView = ( ) => {
225251 const [ form ] = Form . useForm ( ) ;
226252
@@ -343,88 +369,74 @@ describe('Form List 组件测试', () => {
343369 < FormList name = "users" >
344370 { ( userFields , { add : addUser , remove : removeUser } ) => (
345371 < >
346- { userFields . map ( ( { key : userKey , name : userName , ... userRestField } , userIndex ) => (
372+ { userFields . map ( ( { key : userKey , name : userName } , userIndex ) => (
347373 < FormItem key = { userKey } >
348- < FormItem
349- { ...userRestField }
350- name = { [ userName , 'name' ] }
351- label = "用户名"
352- rules = { [ { required : true , type : 'error' } ] }
353- >
374+ < FormItem name = { [ userName , 'name' ] } label = "用户名" rules = { [ { required : true , type : 'error' } ] } >
354375 < Input placeholder = { `user-name-${ userIndex } ` } />
355376 </ FormItem >
356377
357378 < FormList name = { [ userName , 'projects' ] } >
358379 { ( projectFields , { add : addProject , remove : removeProject } ) => (
359380 < >
360- { projectFields . map (
361- ( { key : projectKey , name : projectName , ...projectRestField } , projectIndex ) => (
362- < FormItem key = { projectKey } >
363- < FormItem
364- { ...projectRestField }
365- name = { [ projectName , 'projectName' ] }
366- label = "项目名称"
367- rules = { [ { required : true , type : 'error' } ] }
368- >
369- < Input placeholder = { `project-name-${ userIndex } -${ projectIndex } ` } />
370- </ FormItem >
371-
372- < FormList name = { [ projectName , 'tasks' ] } >
373- { ( taskFields , { add : addTask , remove : removeTask } ) => (
374- < >
375- { taskFields . map (
376- ( { key : taskKey , name : taskName , ...taskRestField } , taskIndex ) => (
377- < FormItem key = { taskKey } >
378- < FormItem
379- { ...taskRestField }
380- name = { [ taskName , 'taskName' ] }
381- label = "任务名称"
382- rules = { [ { required : true , type : 'error' } ] }
383- >
384- < Input
385- placeholder = { `task-name-${ userIndex } -${ projectIndex } -${ taskIndex } ` }
386- />
387- </ FormItem >
388- < FormItem
389- { ...taskRestField }
390- name = { [ taskName , 'status' ] }
391- label = "状态"
392- rules = { [ { required : true , type : 'error' } ] }
393- >
394- < Input
395- placeholder = { `task-status-${ userIndex } -${ projectIndex } -${ taskIndex } ` }
396- />
397- </ FormItem >
398- < FormItem >
399- < MinusCircleIcon
400- className = { `test-remove-task-${ userIndex } -${ projectIndex } -${ taskIndex } ` }
401- onClick = { ( ) => removeTask ( taskName ) }
402- />
403- </ FormItem >
404- </ FormItem >
405- ) ,
406- ) }
407- < FormItem >
408- < Button
409- id = { `test-add-task-${ userIndex } -${ projectIndex } ` }
410- onClick = { ( ) => addTask ( { taskName : 'New Task' , status : 'pending' } ) }
381+ { projectFields . map ( ( { key : projectKey , name : projectName } , projectIndex ) => (
382+ < FormItem key = { projectKey } >
383+ < FormItem
384+ name = { [ projectName , 'projectName' ] }
385+ label = "项目名称"
386+ rules = { [ { required : true , type : 'error' } ] }
387+ >
388+ < Input placeholder = { `project-name-${ userIndex } -${ projectIndex } ` } />
389+ </ FormItem >
390+
391+ < FormList name = { [ projectName , 'tasks' ] } >
392+ { ( taskFields , { add : addTask , remove : removeTask } ) => (
393+ < >
394+ { taskFields . map ( ( { key : taskKey , name : taskName } , taskIndex ) => (
395+ < FormItem key = { taskKey } >
396+ < FormItem
397+ name = { [ taskName , 'taskName' ] }
398+ label = "任务名称"
399+ rules = { [ { required : true , type : 'error' } ] }
400+ >
401+ < Input placeholder = { `task-name-${ userIndex } -${ projectIndex } -${ taskIndex } ` } />
402+ </ FormItem >
403+ < FormItem
404+ name = { [ taskName , 'status' ] }
405+ label = "状态"
406+ rules = { [ { required : true , type : 'error' } ] }
411407 >
412- Add Task
413- </ Button >
408+ < Input
409+ placeholder = { `task-status-${ userIndex } -${ projectIndex } -${ taskIndex } ` }
410+ />
411+ </ FormItem >
412+ < FormItem >
413+ < MinusCircleIcon
414+ className = { `test-remove-task-${ userIndex } -${ projectIndex } -${ taskIndex } ` }
415+ onClick = { ( ) => removeTask ( taskName ) }
416+ />
417+ </ FormItem >
414418 </ FormItem >
415- </ >
416- ) }
417- </ FormList >
418-
419- < FormItem >
420- < MinusCircleIcon
421- className = { `test-remove-project-${ userIndex } -${ projectIndex } ` }
422- onClick = { ( ) => removeProject ( projectName ) }
423- />
424- </ FormItem >
419+ ) ) }
420+ < FormItem >
421+ < Button
422+ id = { `test-add-task-${ userIndex } -${ projectIndex } ` }
423+ onClick = { ( ) => addTask ( { taskName : 'New Task' , status : 'pending' } ) }
424+ >
425+ Add Task
426+ </ Button >
427+ </ FormItem >
428+ </ >
429+ ) }
430+ </ FormList >
431+
432+ < FormItem >
433+ < MinusCircleIcon
434+ className = { `test-remove-project-${ userIndex } -${ projectIndex } ` }
435+ onClick = { ( ) => removeProject ( projectName ) }
436+ />
425437 </ FormItem >
426- ) ,
427- ) }
438+ </ FormItem >
439+ ) ) }
428440 < FormItem >
429441 < Button
430442 id = { `test-add-project-${ userIndex } ` }
0 commit comments