-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add equals
method to ShallowWrapper.
#124
Add equals
method to ShallowWrapper.
#124
Conversation
expect(wrapper.equals(b)).to.equal(true); | ||
}); | ||
|
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add 2 tests to this?
say we have a composite componente <Foo />
which renders `...
expect(shallow(<Foo />).equals(<Bar />)).to.equal(true);
expect(shallow(<Foo />).equals(<Foo />)).to.equal(false);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
This is great. Thanks for contributing! One thing I'd like us to consider: What if we had this be an extension of the The issue in this case would be the fact that this would actually conflict with #110 unless we had some way of differentiating a JSX element from an object that just looks like a JSX element... thoughts? cc @blainekasten @ljharb @hartzis
|
cc @vesln for how we might want to use this in chai-enzyme. |
fb0caf5
to
97af9e0
Compare
const Bar = () => <p />;
const Foo = () => <div><Bar /></div>;
// obviously this returns true:
render(<Foo />).is(<div><p /></div>);
// should this returns true as well?
render(<Foo />).is(<div><Bar /></div>); |
Thanks for the cc @lelandrichardson. I'm personally not that excited about this API in general. My thoughts are that this gives the wrong intention to testing components. What I think should be the primary goal in testing react components, is that given a certain set of state/props/context, an expected HTML render is asserted. Not an expected JSX call. At that point, you're just duplicating code and making sure it doesn't change. But the purpose of tests is to assert results, not implementations. Because I don't care if code get's refactored in a different way as long as the end result is the same. If I used this method, I could see somebody doing something like this: const component = render(
<div><b/></div>
);
component.equals(<div><b/></div>) // true But then, say a refactor happens to this Foo = <b/>
const component = render(
<div><Foo/></div>
); now all of a sudden, the result is the exact same, but my tests have failed. So I just think this encourages the wrong habits. |
@lelandrichardson it should be quite straight forward for chai-enzyme to implement.
|
Great explanation @blainekasten. Though I don't agree completely that the primary goal of testing react components needs to be html output. Using shallow render you're able to keep the scope of a test to a single component and make assertions about props being passed and what nodes/components will be 'rendered'. I personally wouldn't test using this, but I could see it possibly being useful for shallow render testing. I also like @vesln's idea, but it does tie you to chai. |
I agree with @hartzis . While I think this API could be used in a way that I wouldn't encourage (as you describe), I think the same could be said for most of the APIs on enzyme. This provides the ability for someone to describe what a specific sub-tree of the resulting output looks like, and I think that is powerful. After giving it some thought, I think this is fine to be on As a result, this PR is ready to merge. |
* @returns {Boolean} | ||
*/ | ||
equals(node) { | ||
return nodeEqual(this.node, node); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you wrap this inside a return this.single(() => nodeEqual(this.node, node));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
97af9e0
to
8d3e5c3
Compare
rebased onto master. |
Add `equals` method to ShallowWrapper.
Fixes #122