繁体   English   中英

测试点击事件

[英]Testing click event

我有一个测试 - 它相对简单:

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();
            });
    });

// ...

这失败了,说两者都没有被调用。 我猜我在做一些愚蠢的事情,但是有人可以告诉我它是什么吗?

你有一个 promise,但 jest 不知道。 尝试退货:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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