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

126
Views
How to test button click on jest/enzyme without props

I want to test is the button clicked and some actions happens but I fail the test and receive 0 number of calls when clicked. Im not passing onclick function from props. I'm using materialUi Button

const LoginFunction = () => {

  const handleClick = () => {
    console.log('some action here')
  };

  return (
    <Button
        data-test="btn"
        className='btn'
        onClick={() => {
            handleClick();
        }}
    >
       Some text
    </Button>
 )};

I wrote test like this

describe('Test Login component', () => {

  it('should trigger click event', () => {
    const mockCallBack = jest.fn();
    const wrapper = shallow(<LoginFunction />);

    wrapper.simulate('click');
    expect(mockCallBack.mock.calls.length).toBe(1);
   }); 
 });

Tried this way too

 describe('Test Login component', () => {

  it('should trigger click event', () => {
    const mockCallBack = jest.fn();
    const wrapper = shallow(<LoginFunction />);

    wrapper.find('.btn').simulate('click');
    expect(mockCallBack.mock.calls.length).toBe(1);
   }); 
 });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

**Also in the main component you can directly call the handler in onclick like this** 

     <Button
            data-test="btn"
            className='btn'
            onClick={handleClick}
        >
           Some text
        </Button>

describe('Test Login component', () => {

  it('should trigger click event', () => {
  const wrapper=mount( <LoginFunction />);
    const result=wrapper.find('button').simulate('click')
   expect(result).toBe('some action here')
 });
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!