Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
Jest and Enzyme onClick event should toggle class - Test does not pick up the updated wrapper (??)

I'm trying to test a component which holds a tree-hierarchy.

The tree hierarchy is build with <ul/> and <li/> elements.

The highest level of my tree is not hidden, which means that I can see my individual li elements below the first ul element. However, the next nested ul element has a class called myHiddenClass which amongst other things hides the element. When I click on the li element above it, it toggles myHiddenClass on the ul element.

The tree-hierarchy therefore looks something like this:

<ul>
    <li onClick={(e) => unHideUl(e)}></li>
    <ul className='myHiddenClass'>
        <li></li>
        ...
    </ul>
</ul>

The function unHideUl looks as follows:

const unHideUl = (e) => {
    e.stopPropagation()
    e.target.nextSibling.classList.toggle('myHiddenClass')
}

I've tested the onClick event, and it triggers as it should. Obviously I also tested this functionality with a regular compile, and it works flawless.

My test looks as follows:

let wrapper

beforeEach(() => {
    wrapper = mount(<MyComponent/>)
});

afterEach(() => {
    wrapper.unmount();
})

test('Expect to be expanded', () => {
    const item = wrapper.find('li').first()
    item.simulate('click')
    const uList = wrapper.find('ul').first().children().find('ul').first()
    expect(uList.hasClass('myHiddenClass')).toBeFalsy()
})

I have tried printing the wrapper like this console.log(wrapper.debug()) after simulate('click), but also here, none of my ul are having their myHiddenClass removed.

Obviously, I would appreciate if I could get some insights on what I'm doing wrong here - For some reason I can't help thinking that my wrapper may not update after the unHideUl has toggled the class.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!