Skip to content

Commit

Permalink
[New] mount: add .equals()
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 30, 2018
1 parent fe14611 commit dcc8ab1
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
104 changes: 104 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,110 @@ describeWithDOM('mount', () => {
});
});

describe('.equals(node)', () => {
it('should allow matches on the root node', () => {
const a = <div className="foo" />;
const b = <div className="foo" />;
const c = <div className="bar" />;
expect(mount(a).equals(b)).to.equal(true);
expect(mount(a).equals(c)).to.equal(false);
});

it('should NOT allow matches on a nested node', () => {
const wrapper = mount((
<div>
<div className="foo" />
</div>
));
const b = <div className="foo" />;
expect(wrapper.equals(b)).to.equal(false);
});

it('should match composite components', () => {
class Foo extends React.Component {
render() { return <div />; }
}
const wrapper = mount((
<div>
<Foo />
</div>
));
const b = <div><Foo /></div>;
expect(wrapper.equals(b)).to.equal(true);
});

it.skip('should not expand `node` content', () => {
class Bar extends React.Component {
render() { return <div />; }
}

class Foo extends React.Component {
render() { return <Bar />; }
}

expect(mount(<Foo />).equals(<Bar />)).to.equal(true);
expect(mount(<Foo />).equals(<Foo />)).to.equal(false);
});

describeIf(is('> 0.13'), 'stateless function components (SFCs)', () => {
it('should match composite SFCs', () => {
const Foo = () => (
<div />
);

const wrapper = mount((
<div>
<Foo />
</div>
));
const b = <div><Foo /></div>;
expect(wrapper.equals(b)).to.equal(true);
});

it.skip('should not expand `node` content', () => {
const Bar = () => (
<div />
);

const Foo = () => (
<Bar />
);

expect(mount(<Foo />).equals(<Bar />)).to.equal(true);
expect(mount(<Foo />).equals(<Foo />)).to.equal(false);
});
});

it.skip('flattens arrays of children to compare', () => {
class TwoChildren extends React.Component {
render() {
return (
<div className="parent-component-class">
<div key="a" className="asd" />
<div key="b" className="fgh" />
</div>
);
}
}

class TwoChildrenOneArrayed extends React.Component {
render() {
return (
<div className="parent-component-class">
<div key="a" className="asd" />
{[<div key="b" className="fgh" />]}
</div>
);
}
}
const twoChildren = mount(<TwoChildren />);
const twoChildrenOneArrayed = mount(<TwoChildrenOneArrayed />);

expect(twoChildren.equals(twoChildrenOneArrayed.getElement())).to.equal(true);
expect(twoChildrenOneArrayed.equals(twoChildren.getElement())).to.equal(true);
});
});

describe('.hostNodes()', () => {
it('should strip out any non-hostNode', () => {
class Foo extends React.Component {
Expand Down
16 changes: 16 additions & 0 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,22 @@ class ReactWrapper {
return Array.isArray(nodes) && nodes.some(node => this.containsMatchingElement(node));
}

/**
* Whether or not a given react element exists in the current render tree.
*
* Example:
* ```
* const wrapper = mount(<MyComponent />);
* expect(wrapper.contains(<div className="foo bar" />)).to.equal(true);
* ```
*
* @param {ReactElement} node
* @returns {Boolean}
*/
equals(node) {
return this.single('equals', () => nodeEqual(this.getNodeInternal(), node));
}

/**
* Finds every node in the render tree of the current wrapper that matches the provided selector.
*
Expand Down

0 comments on commit dcc8ab1

Please sign in to comment.