简体   繁体   中英

Testing click event

I have a test - it's relatively simple:

import {
    fireEvent,
    render,
} from '@testing-library/react';

// ...

    it('Events should bubble when they aren\'t prevented from doing so', () => {
        const parentF = jest.fn();
        const childF = jest.fn();

        const testDom = render(
            <div onClick={parentF}>
                <button onClick={childF} id="test-button">Test</button>
            </div>
        );

        testDom.findByText('Test')
            .then(element => {
                fireEvent.click(element);

                expect(parentF).toBeCalled();
                expect(childF).toBeCalled();
            });
    });

// ...

This fails saying neither were called. I'm guessing I'm doing something stupid, but could someone tell me what it is?

You have a promise, but jest doesn't know about it. Try returning it:

return testDom.findByText('Test')
       /* rest of the code */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM