|
1 | 1 | import React from 'react'; |
2 | 2 | import { expect } from 'chai'; |
| 3 | +import wrap from 'mocha-wrap'; |
| 4 | +import sinon from 'sinon'; |
3 | 5 | import { |
4 | 6 | childrenToSimplifiedArray, |
5 | 7 | nodeEqual, |
6 | 8 | nodeMatches, |
7 | 9 | displayNameOfNode, |
8 | 10 | spyMethod, |
9 | 11 | nodeHasType, |
| 12 | + getAdapter, |
10 | 13 | } from 'enzyme/build/Utils'; |
11 | 14 | import { |
12 | 15 | flatten, |
@@ -616,56 +619,84 @@ describe('Utils', () => { |
616 | 619 | }); |
617 | 620 | }); |
618 | 621 |
|
619 | | - describe('nodeHasType', () => { |
620 | | - it('is `false` if either argument is falsy', () => { |
621 | | - expect(nodeHasType(null, {})).to.equal(false); |
622 | | - expect(nodeHasType({}, null)).to.equal(false); |
623 | | - }); |
624 | | - |
625 | | - it('is `false` if `node` has a falsy `type`', () => { |
626 | | - expect(nodeHasType({}, {})).to.equal(false); |
627 | | - expect(nodeHasType({ type: null }, {})).to.equal(false); |
628 | | - expect(nodeHasType({ type: false }, {})).to.equal(false); |
629 | | - expect(nodeHasType({ type: '' }, {})).to.equal(false); |
630 | | - expect(nodeHasType({ type: 0 }, {})).to.equal(false); |
631 | | - }); |
632 | | - |
633 | | - it('compares `node.type` to `type` when `node.type` is a non-empty string', () => { |
634 | | - expect(nodeHasType({ type: 'foo' }, 'foo')).to.equal(true); |
635 | | - expect(nodeHasType({ type: 'foo' }, 'bar')).to.equal(false); |
636 | | - }); |
| 622 | + wrap() |
| 623 | + .withOverride(() => getAdapter(), 'displayNameOfNode', () => undefined) |
| 624 | + .describe('nodeHasType', () => { |
| 625 | + it('is `false` if either argument is falsy', () => { |
| 626 | + expect(nodeHasType(null, {})).to.equal(false); |
| 627 | + expect(nodeHasType({}, null)).to.equal(false); |
| 628 | + }); |
637 | 629 |
|
638 | | - describe('when only `node.type.displayName` matches `type`', () => { |
639 | | - const x = {}; |
640 | | - it('is `true` when `node.type` is an object', () => { |
641 | | - expect(nodeHasType( |
642 | | - { type: { displayName: x } }, |
643 | | - x, |
644 | | - )).to.equal(true); |
| 630 | + it('is `false` if `node` has a falsy `type`', () => { |
| 631 | + expect(nodeHasType({}, {})).to.equal(false); |
| 632 | + expect(nodeHasType({ type: null }, {})).to.equal(false); |
| 633 | + expect(nodeHasType({ type: false }, {})).to.equal(false); |
| 634 | + expect(nodeHasType({ type: '' }, {})).to.equal(false); |
| 635 | + expect(nodeHasType({ type: 0 }, {})).to.equal(false); |
645 | 636 | }); |
646 | 637 |
|
647 | | - it('is `true` when `node.type` is a function', () => { |
648 | | - expect(nodeHasType( |
649 | | - { type: Object.assign(() => {}, { displayName: x }) }, |
650 | | - x, |
651 | | - )).to.equal(true); |
| 638 | + it('compares `node.type` to `type` when `node.type` is a non-empty string', () => { |
| 639 | + expect(nodeHasType({ type: 'foo' }, 'foo')).to.equal(true); |
| 640 | + expect(nodeHasType({ type: 'foo' }, 'bar')).to.equal(false); |
652 | 641 | }); |
653 | | - }); |
654 | 642 |
|
655 | | - describe('when only `node.type.name` matches `type`', () => { |
656 | | - const x = {}; |
657 | | - it('is `true` when `node.type` is an object', () => { |
658 | | - expect(nodeHasType( |
659 | | - { type: { name: x } }, |
660 | | - x, |
661 | | - )).to.equal(true); |
| 643 | + describe('when only `node.type.displayName` matches `type`', () => { |
| 644 | + const x = {}; |
| 645 | + it('is `true` when `node.type` is an object', () => { |
| 646 | + expect(nodeHasType( |
| 647 | + { type: { displayName: x } }, |
| 648 | + x, |
| 649 | + )).to.equal(true); |
| 650 | + }); |
| 651 | + |
| 652 | + it('is `true` when `node.type` is a function', () => { |
| 653 | + expect(nodeHasType( |
| 654 | + { type: Object.assign(() => {}, { displayName: x }) }, |
| 655 | + x, |
| 656 | + )).to.equal(true); |
| 657 | + }); |
662 | 658 | }); |
663 | 659 |
|
664 | | - it('is `true` when `node.type` is a function', () => { |
665 | | - function namedType() {} |
| 660 | + describe('when only `node.type.name` matches `type`', () => { |
| 661 | + const x = {}; |
| 662 | + it('is `true` when `node.type` is an object', () => { |
| 663 | + expect(nodeHasType( |
| 664 | + { type: { name: x } }, |
| 665 | + x, |
| 666 | + )).to.equal(true); |
| 667 | + }); |
| 668 | + |
| 669 | + it('is `true` when `node.type` is a function', () => { |
| 670 | + function namedType() {} |
666 | 671 |
|
667 | | - expect(nodeHasType({ type: namedType }, 'namedType')).to.equal(true); |
| 672 | + expect(nodeHasType({ type: namedType }, 'namedType')).to.equal(true); |
| 673 | + }); |
668 | 674 | }); |
| 675 | + |
| 676 | + wrap() |
| 677 | + .withOverride(() => getAdapter(), 'displayNameOfNode', () => sinon.stub()) |
| 678 | + .describe('when the adapter has a `displayNameOfNode` function', () => { |
| 679 | + it('is `true` when `displayNameOfNode` matches `type`', () => { |
| 680 | + const stub = getAdapter().displayNameOfNode; |
| 681 | + const sentinel = {}; |
| 682 | + stub.returns(sentinel); |
| 683 | + |
| 684 | + const node = {}; |
| 685 | + expect(nodeHasType(node, sentinel)).to.equal(true); |
| 686 | + |
| 687 | + expect(stub).to.have.property('callCount', 1); |
| 688 | + const { args } = stub.firstCall; |
| 689 | + expect(args).to.eql([node]); |
| 690 | + }); |
| 691 | + |
| 692 | + it('is `false` when `displayNameOfNode` does not match `type`', () => { |
| 693 | + const stub = getAdapter().displayNameOfNode; |
| 694 | + const sentinel = {}; |
| 695 | + stub.returns(sentinel); |
| 696 | + |
| 697 | + const node = {}; |
| 698 | + expect(nodeHasType(node, {})).to.equal(false); |
| 699 | + }); |
| 700 | + }); |
669 | 701 | }); |
670 | | - }); |
671 | 702 | }); |
0 commit comments