0

How to get HTML elements in react tests with Enzyme? I am able to find wrappers. But I want HTML element. Becuase block of my code which needs to be covered has offsetParent check on HTML element which is not getting covered. Below is the sample.

 it('add class test', async () => {

    const wrapper = mount((
        <div class='ag-cell'>
            <div class='ag-react-conatiner'></div>
            <span anum={3} abool={false} />
            <span anum="3" abool="false" />
        </div>
    ));

    const x = wrapper.find('.ag-react-conatiner').offsetParent;

    console.log(wrapper.find('.ag-react-conatiner').offsetParent);
 }

I am able to get wrapper.find('.ag-react-conatiner'). How i can get offsetParent of it?

1 Answer 1

1

Why not add the attribute data-testid='something' to your elements use

const wrapper = mount(<MyComponent />);
expect(wrapper.find('[data-testid="something"]')).to.have.lengthOf(1);

Using the above attribute makes it clear its used for testing , unlike classes which can easily be renamed etc in development.

Sign up to request clarification or add additional context in comments.

5 Comments

Problem is not about finding wrapper. With out data-testid also i could get proper wrapper output. But in my case i need to get offsetParent of HTMLElement which i can't find on wrapper object
As wrapper.find('.ag-react-conatiner') is not HTML element i can't find offsetParent on it. I need a way to get HTML element from corresponding wrappers.
Does this help const wrapper = shallow(<MyComponent />); expect(wrapper.find(Foo).closest('.bar')).to.have.lengthOf(1);
Shallow won't help. I have tested it. Shallow only render s single level.
and you cannot use mount , I only though find().closest might help. When you say offsetParent check can you show me the code ? I'm not sure I fully understood

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.