|
| 1 | +import { fireEvent, mockTimeout, render, vi } from '@test/utils'; |
1 | 2 | import React from 'react'; |
2 | 3 | import { MinusCircleIcon } from 'tdesign-icons-react'; |
3 | | -import { fireEvent, mockTimeout, render, vi } from '@test/utils'; |
4 | 4 |
|
5 | 5 | import Button from '../../button'; |
6 | 6 | import Input from '../../input'; |
@@ -305,6 +305,38 @@ describe('Form List 组件测试', () => { |
305 | 305 | form.clearValidate(); |
306 | 306 | } |
307 | 307 |
|
| 308 | + function getFieldsValueAll() { |
| 309 | + const allValues = form.getFieldsValue(true); |
| 310 | + console.log('getFieldsValue(true):', allValues); |
| 311 | + document.getElementById('all-values-result')?.setAttribute('data-result', JSON.stringify(allValues)); |
| 312 | + } |
| 313 | + |
| 314 | + function getFieldsValueByPath() { |
| 315 | + // Test 1: Get specific user's data |
| 316 | + const usersPath = form.getFieldsValue(['users']); |
| 317 | + |
| 318 | + // Test 2: Get specific nested path - first user's projects |
| 319 | + const firstUserProjects = form.getFieldsValue([['users', 0, 'projects']]); |
| 320 | + |
| 321 | + // Test 3: Get specific nested path - first user's first project's tasks |
| 322 | + const firstProjectTasks = form.getFieldsValue([['users', 0, 'projects', 0, 'tasks']]); |
| 323 | + |
| 324 | + // Test 4: Get multiple paths at once |
| 325 | + const multiplePaths = form.getFieldsValue([ |
| 326 | + ['users', 0, 'name'], |
| 327 | + ['users', 1, 'name'], |
| 328 | + ['users', 0, 'projects', 0, 'projectName'], |
| 329 | + ]); |
| 330 | + |
| 331 | + const result = { |
| 332 | + usersPath, |
| 333 | + firstUserProjects, |
| 334 | + firstProjectTasks, |
| 335 | + multiplePaths, |
| 336 | + }; |
| 337 | + document.getElementById('path-values-result')?.setAttribute('data-result', JSON.stringify(result)); |
| 338 | + } |
| 339 | + |
308 | 340 | return ( |
309 | 341 | <Form form={form}> |
310 | 342 | <FormList name="users"> |
@@ -425,7 +457,11 @@ describe('Form List 组件测试', () => { |
425 | 457 | <Button onClick={addNestedData}>addNestedData</Button> |
426 | 458 | <Button onClick={validate}>validate</Button> |
427 | 459 | <Button onClick={clearValidate}>clearValidate</Button> |
| 460 | + <Button onClick={getFieldsValueAll}>getFieldsValue(true)</Button> |
| 461 | + <Button onClick={getFieldsValueByPath}>getFieldsValue(namePath)</Button> |
428 | 462 | </FormItem> |
| 463 | + <div id="all-values-result" data-result="" /> |
| 464 | + <div id="path-values-result" data-result="" /> |
429 | 465 | </Form> |
430 | 466 | ); |
431 | 467 | }; |
@@ -472,6 +508,59 @@ describe('Form List 组件测试', () => { |
472 | 508 | expect((getByPlaceholderText('task-name-1-0-0') as HTMLInputElement).value).toBe('Write API docs'); |
473 | 509 | expect((getByPlaceholderText('task-status-1-0-0') as HTMLInputElement).value).toBe('in-progress'); |
474 | 510 |
|
| 511 | + // Test getFieldsValue(true) - should get all form values |
| 512 | + fireEvent.click(queryByText('getFieldsValue(true)')); |
| 513 | + await mockTimeout(); |
| 514 | + const allValuesResult = container.querySelector('#all-values-result')?.getAttribute('data-result'); |
| 515 | + const allValues = JSON.parse(allValuesResult || '{}'); |
| 516 | + expect(allValues).toHaveProperty('users'); |
| 517 | + expect(Array.isArray(allValues.users)).toBe(true); |
| 518 | + expect(allValues.users.length).toBe(2); |
| 519 | + expect(allValues.users[0].name).toBe('Charlie'); |
| 520 | + expect(allValues.users[0].projects.length).toBe(2); |
| 521 | + expect(allValues.users[0].projects[0].projectName).toBe('Backend Service'); |
| 522 | + expect(allValues.users[0].projects[0].tasks.length).toBe(3); |
| 523 | + expect(allValues.users[1].name).toBe('David'); |
| 524 | + |
| 525 | + // Test getFieldsValue with complex namePath |
| 526 | + fireEvent.click(queryByText('getFieldsValue(namePath)')); |
| 527 | + await mockTimeout(); |
| 528 | + const pathValuesResult = container.querySelector('#path-values-result')?.getAttribute('data-result'); |
| 529 | + const pathValues = JSON.parse(pathValuesResult || '{}'); |
| 530 | + |
| 531 | + // Test 1: Verify users path returns all users data |
| 532 | + expect(pathValues.usersPath).toHaveProperty('users'); |
| 533 | + expect(Array.isArray(pathValues.usersPath.users)).toBe(true); |
| 534 | + expect(pathValues.usersPath.users.length).toBe(2); |
| 535 | + expect(pathValues.usersPath.users[0].name).toBe('Charlie'); |
| 536 | + expect(pathValues.usersPath.users[1].name).toBe('David'); |
| 537 | + |
| 538 | + // Test 2: Verify nested path returns first user's projects |
| 539 | + expect(pathValues.firstUserProjects).toHaveProperty('users'); |
| 540 | + expect(pathValues.firstUserProjects.users[0]).toHaveProperty('projects'); |
| 541 | + expect(Array.isArray(pathValues.firstUserProjects.users[0].projects)).toBe(true); |
| 542 | + expect(pathValues.firstUserProjects.users[0].projects.length).toBe(2); |
| 543 | + expect(pathValues.firstUserProjects.users[0].projects[0].projectName).toBe('Backend Service'); |
| 544 | + expect(pathValues.firstUserProjects.users[0].projects[1].projectName).toBe('DevOps'); |
| 545 | + |
| 546 | + // Test 3: Verify deep nested path returns first project's tasks |
| 547 | + expect(pathValues.firstProjectTasks).toHaveProperty('users'); |
| 548 | + expect(pathValues.firstProjectTasks.users[0].projects[0]).toHaveProperty('tasks'); |
| 549 | + expect(Array.isArray(pathValues.firstProjectTasks.users[0].projects[0].tasks)).toBe(true); |
| 550 | + expect(pathValues.firstProjectTasks.users[0].projects[0].tasks.length).toBe(3); |
| 551 | + expect(pathValues.firstProjectTasks.users[0].projects[0].tasks[0].taskName).toBe('Database design'); |
| 552 | + expect(pathValues.firstProjectTasks.users[0].projects[0].tasks[1].taskName).toBe('API development'); |
| 553 | + expect(pathValues.firstProjectTasks.users[0].projects[0].tasks[2].taskName).toBe('Testing'); |
| 554 | + |
| 555 | + // Test 4: Verify multiple paths returns specific fields |
| 556 | + expect(pathValues.multiplePaths).toHaveProperty('users'); |
| 557 | + expect(pathValues.multiplePaths.users[0]).toHaveProperty('name'); |
| 558 | + expect(pathValues.multiplePaths.users[0].name).toBe('Charlie'); |
| 559 | + expect(pathValues.multiplePaths.users[1]).toHaveProperty('name'); |
| 560 | + expect(pathValues.multiplePaths.users[1].name).toBe('David'); |
| 561 | + expect(pathValues.multiplePaths.users[0].projects[0]).toHaveProperty('projectName'); |
| 562 | + expect(pathValues.multiplePaths.users[0].projects[0].projectName).toBe('Backend Service'); |
| 563 | + |
475 | 564 | // Test remove nested task - remove first task of Charlie's Backend Service project |
476 | 565 | const removeTaskBtn = container.querySelector('.test-remove-task-0-0-0'); |
477 | 566 | fireEvent.click(removeTaskBtn); |
|
0 commit comments