|
1 | 1 | import type {Section} from '@libs/OptionsListUtils'; |
2 | 2 | import type {SelectedTagOption} from '@libs/TagsOptionsListUtils'; |
3 | | -import {getTagListSections, sortTags} from '@libs/TagsOptionsListUtils'; |
| 3 | +import {getTagListSections, getTagVisibility, sortTags} from '@libs/TagsOptionsListUtils'; |
4 | 4 | import CONST from '@src/CONST'; |
5 | 5 | import IntlStore from '@src/languages/IntlStore'; |
| 6 | +import type {PolicyTagLists} from '@src/types/onyx'; |
| 7 | +import createRandomPolicy from '../utils/collections/policies'; |
| 8 | +import createRandomTransaction from '../utils/collections/transaction'; |
6 | 9 | import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; |
7 | 10 |
|
8 | 11 | describe('TagsOptionsListUtils', () => { |
@@ -611,4 +614,166 @@ describe('TagsOptionsListUtils', () => { |
611 | 614 | expect(sorted.at(1)?.name).toBe('DisabledTag'); |
612 | 615 | expect(sorted.at(2)?.name).toBe('OfficeSupplies'); |
613 | 616 | }); |
| 617 | + |
| 618 | + describe('getTagVisibility', () => { |
| 619 | + const mockPolicy = createRandomPolicy(1, 'corporate', 'Test Policy'); |
| 620 | + const mockTransaction = createRandomTransaction(1); |
| 621 | + const mockPolicyTags: PolicyTagLists = { |
| 622 | + tagList1: { |
| 623 | + name: 'Category', |
| 624 | + required: true, |
| 625 | + tags: { |
| 626 | + tag1: {name: 'Tag1', enabled: true}, |
| 627 | + tag2: {name: 'Tag2', enabled: false}, |
| 628 | + }, |
| 629 | + orderWeight: 0, |
| 630 | + }, |
| 631 | + tagList2: { |
| 632 | + name: 'Subcategory', |
| 633 | + required: false, |
| 634 | + tags: { |
| 635 | + tag3: {name: 'Tag3', enabled: true}, |
| 636 | + tag4: {name: 'Tag4', enabled: true}, |
| 637 | + }, |
| 638 | + orderWeight: 1, |
| 639 | + }, |
| 640 | + }; |
| 641 | + |
| 642 | + it('should hide all tags when shouldShowTags is false', () => { |
| 643 | + const result = getTagVisibility({ |
| 644 | + shouldShowTags: false, |
| 645 | + policy: mockPolicy, |
| 646 | + policyTags: mockPolicyTags, |
| 647 | + transaction: mockTransaction, |
| 648 | + }); |
| 649 | + |
| 650 | + expect(result).toEqual([ |
| 651 | + {isTagRequired: true, shouldShow: false}, |
| 652 | + {isTagRequired: false, shouldShow: false}, |
| 653 | + ]); |
| 654 | + }); |
| 655 | + |
| 656 | + it('should show all tags when shouldShowTags is true and no dependent/multilevel tags', () => { |
| 657 | + const result = getTagVisibility({ |
| 658 | + shouldShowTags: true, |
| 659 | + policy: mockPolicy, |
| 660 | + policyTags: mockPolicyTags, |
| 661 | + transaction: mockTransaction, |
| 662 | + }); |
| 663 | + |
| 664 | + expect(result).toEqual([ |
| 665 | + {isTagRequired: true, shouldShow: true}, |
| 666 | + {isTagRequired: false, shouldShow: true}, |
| 667 | + ]); |
| 668 | + }); |
| 669 | + |
| 670 | + it('should show tags when multilevel tags are enabled and have enabled options', () => { |
| 671 | + const policyTagsWithEnabledOptions: PolicyTagLists = { |
| 672 | + tagList1: { |
| 673 | + name: 'Category', |
| 674 | + required: true, |
| 675 | + tags: { |
| 676 | + tag1: {name: 'Tag1', enabled: true}, |
| 677 | + tag2: {name: 'Tag2', enabled: true}, |
| 678 | + }, |
| 679 | + orderWeight: 0, |
| 680 | + }, |
| 681 | + tagList2: { |
| 682 | + name: 'Subcategory', |
| 683 | + required: false, |
| 684 | + tags: { |
| 685 | + tag3: {name: 'Tag3', enabled: true}, |
| 686 | + tag4: {name: 'Tag4', enabled: true}, |
| 687 | + }, |
| 688 | + orderWeight: 1, |
| 689 | + }, |
| 690 | + }; |
| 691 | + |
| 692 | + const result = getTagVisibility({ |
| 693 | + shouldShowTags: true, |
| 694 | + policy: mockPolicy, |
| 695 | + policyTags: policyTagsWithEnabledOptions, |
| 696 | + transaction: mockTransaction, |
| 697 | + }); |
| 698 | + |
| 699 | + expect(result).toEqual([ |
| 700 | + {isTagRequired: true, shouldShow: true}, |
| 701 | + {isTagRequired: false, shouldShow: true}, |
| 702 | + ]); |
| 703 | + }); |
| 704 | + |
| 705 | + it('should hide tags when multilevel tags are enabled but have no enabled options', () => { |
| 706 | + const policyTagsWithDisabledOptions: PolicyTagLists = { |
| 707 | + tagList1: { |
| 708 | + name: 'Category', |
| 709 | + required: true, |
| 710 | + tags: { |
| 711 | + tag1: {name: 'Tag1', enabled: false}, |
| 712 | + tag2: {name: 'Tag2', enabled: false}, |
| 713 | + }, |
| 714 | + orderWeight: 0, |
| 715 | + }, |
| 716 | + tagList2: { |
| 717 | + name: 'Subcategory', |
| 718 | + required: false, |
| 719 | + tags: { |
| 720 | + tag3: {name: 'Tag3', enabled: false}, |
| 721 | + tag4: {name: 'Tag4', enabled: false}, |
| 722 | + }, |
| 723 | + orderWeight: 1, |
| 724 | + }, |
| 725 | + }; |
| 726 | + |
| 727 | + const result = getTagVisibility({ |
| 728 | + shouldShowTags: true, |
| 729 | + policy: mockPolicy, |
| 730 | + policyTags: policyTagsWithDisabledOptions, |
| 731 | + transaction: mockTransaction, |
| 732 | + }); |
| 733 | + |
| 734 | + expect(result).toEqual([ |
| 735 | + {isTagRequired: true, shouldShow: false}, |
| 736 | + {isTagRequired: false, shouldShow: false}, |
| 737 | + ]); |
| 738 | + }); |
| 739 | + |
| 740 | + it('should handle empty policyTags', () => { |
| 741 | + const result = getTagVisibility({ |
| 742 | + shouldShowTags: true, |
| 743 | + policy: mockPolicy, |
| 744 | + policyTags: undefined, |
| 745 | + transaction: mockTransaction, |
| 746 | + }); |
| 747 | + |
| 748 | + expect(result).toEqual([]); |
| 749 | + }); |
| 750 | + |
| 751 | + it('should handle undefined policy', () => { |
| 752 | + const result = getTagVisibility({ |
| 753 | + shouldShowTags: true, |
| 754 | + policy: undefined, |
| 755 | + policyTags: mockPolicyTags, |
| 756 | + transaction: mockTransaction, |
| 757 | + }); |
| 758 | + |
| 759 | + expect(result).toEqual([ |
| 760 | + {isTagRequired: true, shouldShow: true}, |
| 761 | + {isTagRequired: false, shouldShow: true}, |
| 762 | + ]); |
| 763 | + }); |
| 764 | + |
| 765 | + it('should handle undefined transaction', () => { |
| 766 | + const result = getTagVisibility({ |
| 767 | + shouldShowTags: true, |
| 768 | + policy: mockPolicy, |
| 769 | + policyTags: mockPolicyTags, |
| 770 | + transaction: undefined, |
| 771 | + }); |
| 772 | + |
| 773 | + expect(result).toEqual([ |
| 774 | + {isTagRequired: true, shouldShow: true}, |
| 775 | + {isTagRequired: false, shouldShow: true}, |
| 776 | + ]); |
| 777 | + }); |
| 778 | + }); |
614 | 779 | }); |
0 commit comments