enzyme: attribute test now returning 2 elements
I have just upgraded from enzyme 2 to 3 and the following test that previously returned 1 element now returns 2:
it('should find element', () => {
const wrapper = mount(<MyComponent/>);
expect(wrapper.find('[name="element"]')).toHaveLength(1);
});
I found this link that seems to suggest that this is the expected behaviour.
It does not say why this is good or how should I now be writing my tests?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 21 (8 by maintainers)
@dagda1 there’s nothing to fix; it’s expected behavior.
.find('select')
finds tags named “select”, which of course are only host nodes..find('[foo]')
finds props named “foo”, which might be on a host node, OR on a custom component.In other words, it’s supposed to search both component props and HTML element attributes (because they’re all props).
@duro it makes the adapter pattern more generic, and smooths out API inconsistencies. In other words, in v2, some APIs implicitly filtered to host nodes, and some didn’t. This change merely requires you to explicitly filter to host nodes if that’s the thing you want, which makes your tests more explicit, and allows greater flexibility in adapters (which might not be using React at all).
@ljharb I ran into this issue today, and luckily found this thread. I’m wondering if you wouldn’t mind providing just a little more background on the use case that this change was made to support. As I sit presently, this seems like a mighty strange behavior with little justification for existing.
Yes sir. I’m on it.