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

464
Views
React Testing Library: OnClick isn't working in a child component

I have a test like this:

it('Side effect should trigger', async ()=>{
   await act(async () => {
      let container = render(<Grandparent/>);
      const btn = container.findByTestId('childBtn');
      fireEvent.click(btn)
   });
   const changedMsg = await container.findByText('Changed');
   expect(changedMsg).toBeInTheDocument();
})

My components look like this:

Grandparent:

const Grandparent = ()=>{
   const [labelText, setLabelText] = useState('Unchanged');
   return (<Parent labelText={labelText} setLabelText={setLabelText}/>);
}

Parent:

const Parent = ({labelText, setLabelText})=>{
   return (<div>
      <label>{labelText}</label>
      <Child setLabelText={setLabelText} />          
   </div>)
}

Child:

const Child = ({setLabelText}) =>{
   return(<div>
      <Button data-testid='childBtn' onClick={()=>setLabelText('Changed')}/>
   </div>)
}

My issue is that the assertion in my unit test fails. I've logged the return value from findByTextId('childBtn') and I am indeed getting a button (something like <button class='MuiButtonBase-root' data-testid='childBtn'></button>), but the side effect from clicking it is not going through. I added some logging in the onClick function and it never showed up when I re-ran the test, so the issue is that the onClick handler isn't firing at all.

How do I fix this? Is trying to test something that involves side effects from a bunch of nested components like this not feasible in the first place?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

EDIT: I think you have some code mistakes in your unit test since you are mixing react-testing-library API and testing-library, try this:

it("Side effect should trigger", async () => {
    const {findByTestId, findByText} = render(<Grandparent />);
    fireEvent.click(await findByTestId("childBtn"));
    const changedMsg = await findByText("Changed");
    expect(changedMsg).toBeInTheDocument();
  });
});

I did not test it, but it should work. act() wrapper is not needed in react-testing-library since the render method is using it internally.

With react-testing-library you dont' have to care about implementation details of your components, nor you care to test internal state, etc... You just test "What the end user sees on screen in response to certain events". If you need to test functions, like fetch, interactions with global object, etc... you need to mock all those methods.

The philosophy of this approach is to make your unit tests ( which are never funny to write ) so that they won't have to change too often even if you are going to refactor your React Components. You just care about what it' s being rendered as DOM nodes and that's what you have to test.

NOTE: In the code you pasted there are typos btw, props have to be destructured in your code if you want to use them directly without doing props.prop, like : const Child = ({setLabelText}) =>{ .

about 4 years ago · Juan Pablo Isaza Report
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!