-
-
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
hasClass does not work on root ReactWrapper #134
Comments
hmmm. @amccloud this is an interesting side effect of how both react + enzyme is implemented. The react render tree is sort of a two-dimensional graph, where-as in shallow it is one-dimensional. It's hard to know what the right implementations are. I need to look at what the side effects of this are, but I suspect relaxing the isDOMComponent requirement will end up doubling the number of results getting returned for class-based selection... const Bar = () => (
<div className="bar" />
);
const Foo = () => (
<div><Bar /></div>
);
const wrapper = mount(<Foo />); // with your suggested change:
wrapper.find('.bar');
// <Bar />
// <div className="bar" /> // right now it does this...
wrapper.find('.bar');
// <div className="bar" /> |
I have the exact same issue. Was working with < 1.6. I've also tested on 2.0.0, same issue. Using react 0.14 Here is a minimal example to repro const TestComponent = ({ className }) => (
<div className={className}/>
);
describe('TestComponent', () => {
it('mount allows us to set custom props', () => {
const wrapper = mount(<TestComponent className="test" />);
assert(wrapper.hasClass('test')); // false
});
it('shallow allows us to set custom props', () => {
const wrapper = shallow(<TestComponent className="test" />);
assert(wrapper.hasClass('test')); // true, as expected
});
}); Note:
|
Hmm. This seems like a real bug. Will try to take a look at this ASAP. It may be worthwhile to put the fix in as a patch to |
The crux of this behavior seems to be various incompatibilities with mounting Stateless Function Components, due to them not having proper react instances by design, but which enzyme relies on to render certain values. #220 contains workaround fixes for However, hasClass() and its cousins in MountedTraversal are all blocked behind the same |
I'm seeing this same behavior in 2.3.0 |
I'm still seeing this in 2.4.1 as well. I'm not sure if it's for the same reason, but it has the same behavior of |
I just spent hours trying to figure out why tests were failing using May I suggest we document this in the readme, with some alternatives for checking if the wrapper has a class? |
Has this commit made it into an updated npm package? I'm still seeing it in 2.7.1. |
Have the same problem on |
I'm also seeing it in 3.1.0. |
I think I've finally figured out my issue - because I'm using Meteor, the react components are wrapped with a ReactMeteorDataComponent. So I need to find my component first before I can then inspect its classes, props, etc. |
@rooch84 that's what |
@ljharb But I thought |
@rooch84 ah, true. then yes, if you're using |
I have the same issue whilst using React@15.5.4 and Enzyme@3.1.1.
I guess I have to find the component first i.e. |
yes; |
I think this is because
MountedTraversal#instHasClassName
short circuits to false if the instance does not pass theisDOMComponent
check. Could we relax the check?Example test case (React 0.14):
The text was updated successfully, but these errors were encountered: