|
9 | 9 | displayNameOfNode, |
10 | 10 | spyMethod, |
11 | 11 | nodeHasType, |
| 12 | + isCustomComponentElement, |
12 | 13 | } from 'enzyme/build/Utils'; |
13 | 14 | import getAdapter from 'enzyme/build/getAdapter'; |
14 | 15 | import { |
@@ -639,6 +640,151 @@ describe('Utils', () => { |
639 | 640 | }); |
640 | 641 | }); |
641 | 642 |
|
| 643 | + describe('isCustomComponentElement()', () => { |
| 644 | + const adapter = getAdapter(); |
| 645 | + |
| 646 | + wrap() |
| 647 | + .withOverride(() => adapter, 'isCustomComponentElement', () => undefined) |
| 648 | + .describe('with an adapter lacking `.isCustomComponentElement`', () => { |
| 649 | + describe('given a valid CustomComponentElement', () => { |
| 650 | + it('returns true', () => { |
| 651 | + class Foo extends React.Component { |
| 652 | + render() { return <div />; } |
| 653 | + } |
| 654 | + expect(isCustomComponentElement(<Foo />, adapter)).to.equal(true); |
| 655 | + }); |
| 656 | + |
| 657 | + describeIf(is('> 0.13'), 'stateless function elements', () => { |
| 658 | + it('returns true', () => { |
| 659 | + const Foo = () => <div />; |
| 660 | + |
| 661 | + expect(isCustomComponentElement(<Foo />, adapter)).to.equal(true); |
| 662 | + }); |
| 663 | + }); |
| 664 | + |
| 665 | + describeIf(is('>=16.3.0'), 'forwardRef Elements', () => { |
| 666 | + it('returns false', () => { |
| 667 | + const Foo = React.forwardRef(() => <div />); |
| 668 | + expect(isCustomComponentElement(<Foo />, adapter)).to.equal(false); |
| 669 | + }); |
| 670 | + }); |
| 671 | + }); |
| 672 | + |
| 673 | + describe('given an invalid CustomComponentElement', () => { |
| 674 | + it('returns false for HTML elements', () => { |
| 675 | + expect(isCustomComponentElement(<div />, adapter)).to.equal(false); |
| 676 | + }); |
| 677 | + |
| 678 | + it('returns false for non-Components', () => { |
| 679 | + [ |
| 680 | + class Foo {}, |
| 681 | + {}, |
| 682 | + () => {}, |
| 683 | + 'div', |
| 684 | + 'Foo', |
| 685 | + null, |
| 686 | + ].forEach((nonComponent) => { |
| 687 | + expect(isCustomComponentElement(nonComponent, adapter)).to.equal(false); |
| 688 | + }); |
| 689 | + }); |
| 690 | + }); |
| 691 | + }); |
| 692 | + |
| 693 | + wrap() |
| 694 | + .withOverride(() => adapter, 'isCustomComponentElement', () => () => false) |
| 695 | + .describe('with an adapter that has `.isCustomComponentElement` that always returns false', () => { |
| 696 | + describe('given a valid CustomComponentElement', () => { |
| 697 | + it('returns false', () => { |
| 698 | + class Foo extends React.Component { |
| 699 | + render() { return <div />; } |
| 700 | + } |
| 701 | + expect(isCustomComponentElement(<Foo />, adapter)).to.equal(false); |
| 702 | + }); |
| 703 | + |
| 704 | + describeIf(is('> 0.13'), 'stateless function elements', () => { |
| 705 | + it('returns false', () => { |
| 706 | + const Foo = () => <div />; |
| 707 | + |
| 708 | + expect(isCustomComponentElement(<Foo />, adapter)).to.equal(false); |
| 709 | + }); |
| 710 | + }); |
| 711 | + |
| 712 | + describeIf(is('>=16.3.0'), 'forwardRef Elements', () => { |
| 713 | + it('returns false', () => { |
| 714 | + const Foo = React.forwardRef(() => <div />); |
| 715 | + expect(isCustomComponentElement(<Foo />, adapter)).to.equal(false); |
| 716 | + }); |
| 717 | + }); |
| 718 | + }); |
| 719 | + |
| 720 | + describe('given an invalid CustomComponentElement', () => { |
| 721 | + it('returns false for HTML elements', () => { |
| 722 | + expect(isCustomComponentElement(<div />, adapter)).to.equal(false); |
| 723 | + }); |
| 724 | + |
| 725 | + it('returns false for non-Components', () => { |
| 726 | + [ |
| 727 | + class Foo {}, |
| 728 | + {}, |
| 729 | + () => {}, |
| 730 | + 'div', |
| 731 | + 'Foo', |
| 732 | + null, |
| 733 | + ].forEach((nonComponent) => { |
| 734 | + expect(isCustomComponentElement(nonComponent, adapter)).to.equal(false); |
| 735 | + }); |
| 736 | + }); |
| 737 | + }); |
| 738 | + }); |
| 739 | + |
| 740 | + wrap() |
| 741 | + .withOverride(() => adapter, 'isCustomComponentElement', () => () => true) |
| 742 | + .describe('with an adapter that has `.isCustomComponentElement` that always returns true', () => { |
| 743 | + describe('given a valid CustomComponentElement', () => { |
| 744 | + it('returns true', () => { |
| 745 | + class Foo extends React.Component { |
| 746 | + render() { return <div />; } |
| 747 | + } |
| 748 | + expect(isCustomComponentElement(<Foo />, adapter)).to.equal(true); |
| 749 | + }); |
| 750 | + |
| 751 | + describeIf(is('> 0.13'), 'stateless function elements', () => { |
| 752 | + it('returns true', () => { |
| 753 | + const Foo = () => <div />; |
| 754 | + |
| 755 | + expect(isCustomComponentElement(<Foo />, adapter)).to.equal(true); |
| 756 | + }); |
| 757 | + }); |
| 758 | + |
| 759 | + describeIf(is('>=16.3.0'), 'forwardRef Elements', () => { |
| 760 | + it('returns true', () => { |
| 761 | + const Foo = React.forwardRef(() => <div />); |
| 762 | + expect(isCustomComponentElement(<Foo />, adapter)).to.equal(true); |
| 763 | + }); |
| 764 | + }); |
| 765 | + }); |
| 766 | + |
| 767 | + describe('given an invalid CustomComponentElement', () => { |
| 768 | + it('returns true for HTML elements', () => { |
| 769 | + expect(isCustomComponentElement(<div />, adapter)).to.equal(true); |
| 770 | + }); |
| 771 | + |
| 772 | + it('returns true for non-Components', () => { |
| 773 | + [ |
| 774 | + class Foo {}, |
| 775 | + {}, |
| 776 | + () => {}, |
| 777 | + 'div', |
| 778 | + 'Foo', |
| 779 | + null, |
| 780 | + ].forEach((nonComponent) => { |
| 781 | + expect(isCustomComponentElement(nonComponent, adapter)).to.equal(true); |
| 782 | + }); |
| 783 | + }); |
| 784 | + }); |
| 785 | + }); |
| 786 | + }); |
| 787 | + |
642 | 788 | wrap() |
643 | 789 | .withOverride(() => getAdapter(), 'displayNameOfNode', () => undefined) |
644 | 790 | .describe('nodeHasType', () => { |
|
0 commit comments