|
3 | 3 | import React from 'react'; |
4 | 4 | import { render, screen, act, fireEvent } from '@testing-library/react'; |
5 | 5 | import userEvent from '@testing-library/user-event'; |
| 6 | +import cloneDeep from 'lodash/cloneDeep'; |
6 | 7 |
|
7 | 8 | import { useFieldApi, FormRenderer, componentTypes } from '@data-driven-forms/react-form-renderer'; |
8 | 9 |
|
@@ -501,7 +502,146 @@ describe('Select test', () => { |
501 | 502 | jest.useRealTimers(); |
502 | 503 | }); |
503 | 504 |
|
504 | | - it('initial values is not in options', async () => { |
| 505 | + it('initial value is object', async () => { |
| 506 | + const loadOptions = jest.fn().mockImplementation(() => |
| 507 | + Promise.resolve([ |
| 508 | + { value: { value: '111' }, label: 'first' }, |
| 509 | + { value: { value: '1111' }, label: 'second' }, |
| 510 | + ]) |
| 511 | + ); |
| 512 | + field = { ...field, loadOptions, options: [] }; |
| 513 | + |
| 514 | + await act(async () => { |
| 515 | + render( |
| 516 | + <FormRenderer |
| 517 | + {...rendererProps} |
| 518 | + schema={{ |
| 519 | + fields: [ |
| 520 | + { |
| 521 | + ...field, |
| 522 | + isMulti: true, |
| 523 | + isSearchable: true, |
| 524 | + isClearable: true, |
| 525 | + initialValue: [{ value: { value: '111' }, label: 'first' }], |
| 526 | + component: componentTypes.SELECT, |
| 527 | + name: 'select', |
| 528 | + }, |
| 529 | + ], |
| 530 | + }} |
| 531 | + /> |
| 532 | + ); |
| 533 | + }); |
| 534 | + |
| 535 | + expect(inputValue).toEqual([{ value: { value: '111' }, label: 'first' }]); |
| 536 | + }); |
| 537 | + |
| 538 | + describe('options compareValues', () => { |
| 539 | + const loadOptions = jest.fn().mockImplementation(() => |
| 540 | + Promise.resolve([ |
| 541 | + { value: { value: '111' }, label: 'first' }, |
| 542 | + { value: { value: '1111' }, label: 'second' }, |
| 543 | + { value: { value: '3' }, label: 'third' }, |
| 544 | + ]) |
| 545 | + ); |
| 546 | + |
| 547 | + const customCompareField = { |
| 548 | + ...field, |
| 549 | + component: componentTypes.SELECT, |
| 550 | + name: 'select', |
| 551 | + loadOptions, |
| 552 | + options: [], |
| 553 | + isMulti: true, |
| 554 | + isSearchable: true, |
| 555 | + isClearable: true, |
| 556 | + }; |
| 557 | + |
| 558 | + it('custom function compares options with object value', async () => { |
| 559 | + await act(async () => { |
| 560 | + render( |
| 561 | + <FormRenderer |
| 562 | + {...rendererProps} |
| 563 | + schema={{ |
| 564 | + fields: [ |
| 565 | + { |
| 566 | + ...customCompareField, |
| 567 | + compareValues: (a, b) => { |
| 568 | + return a.value.value === b.value.value; |
| 569 | + }, |
| 570 | + initialValue: [{ value: { value: '111' }, label: 'first' }], |
| 571 | + }, |
| 572 | + ], |
| 573 | + }} |
| 574 | + /> |
| 575 | + ); |
| 576 | + }); |
| 577 | + |
| 578 | + expect(inputValue).toEqual([{ value: { value: '111' }, label: 'first' }]); |
| 579 | + }); |
| 580 | + |
| 581 | + it('default function compares options with object value', async () => { |
| 582 | + await act(async () => { |
| 583 | + render( |
| 584 | + <FormRenderer |
| 585 | + {...rendererProps} |
| 586 | + schema={{ |
| 587 | + fields: [ |
| 588 | + { |
| 589 | + ...customCompareField, |
| 590 | + initialValue: [{ value: { value: '111' }, label: 'first' }], |
| 591 | + }, |
| 592 | + ], |
| 593 | + }} |
| 594 | + /> |
| 595 | + ); |
| 596 | + }); |
| 597 | + |
| 598 | + expect(inputValue).toEqual([{ value: { value: '111' }, label: 'first' }]); |
| 599 | + }); |
| 600 | + |
| 601 | + it('default function logs an error for highly nested values ', async () => { |
| 602 | + const consoleSpy = jest.spyOn(console, 'error').mockImplementationOnce(() => undefined); |
| 603 | + const nestedOptions = { |
| 604 | + label: 'Nested', |
| 605 | + value: { |
| 606 | + value: { |
| 607 | + value: { |
| 608 | + value: { |
| 609 | + value: { |
| 610 | + value: 'foo', |
| 611 | + }, |
| 612 | + }, |
| 613 | + }, |
| 614 | + }, |
| 615 | + }, |
| 616 | + }; |
| 617 | + const loadOptions = jest.fn().mockImplementation(() => Promise.resolve([{ ...nestedOptions }])); |
| 618 | + await act(async () => { |
| 619 | + render( |
| 620 | + <FormRenderer |
| 621 | + {...rendererProps} |
| 622 | + schema={{ |
| 623 | + fields: [ |
| 624 | + { |
| 625 | + ...customCompareField, |
| 626 | + loadOptions, |
| 627 | + initialValue: [cloneDeep(nestedOptions)], |
| 628 | + }, |
| 629 | + ], |
| 630 | + }} |
| 631 | + /> |
| 632 | + ); |
| 633 | + }); |
| 634 | + |
| 635 | + // Console error should be triggered for large object depth |
| 636 | + expect(consoleSpy).toHaveBeenCalledWith('Recursion limit of 5 has been exceeded.'); |
| 637 | + // Should have no value because he options are not matched if the depth limit was exceeded |
| 638 | + expect(inputValue).toEqual(''); |
| 639 | + |
| 640 | + consoleSpy.mockReset(); |
| 641 | + }); |
| 642 | + }); |
| 643 | + |
| 644 | + it('initial values are not in options', async () => { |
505 | 645 | const loadOptions = jest.fn().mockImplementation(() => |
506 | 646 | Promise.resolve([ |
507 | 647 | { value: '111', label: 'first' }, |
|
0 commit comments