cfaester / enzyme-adapter-react-18

MIT License
38 stars 12 forks source link

find() returns extra nodes #5

Open sschafercdd opened 1 year ago

sschafercdd commented 1 year ago

First of all, thanks for making this. I hope I can make it work in our project.

We have some code like this:

const component = mount(<EditAssignment {...props} />)
const inputs = component.find('.assayAnnotationInput-input')

In the current React 16 codebase, inputs.length is 1. In the 18 codebase with this adapter, length is 2, although I've verified that component.html() just has one element with the assayAnnotationInput-input classname.

component.find('.assayAnnotationInput-input').get(1).type returns 'input', but component.find('.assayAnnotationInput-input').get(0).type returns an object containing $$typeof: Symbol(react.forward_ref)

Any ideas?

sschafercdd commented 1 year ago

This is probably really just an enyzme thing - I don't know.

I've had some success with the following monkeypatch:

import { ReactWrapper } from 'enzyme';

ReactWrapper.prototype._find = ReactWrapper.prototype.find;
ReactWrapper.prototype.find = function (selector) {
  if (typeof selector === 'string') {
    return this._find(selector).hostNodes();
  }
  return this._find(selector);
};