【问题标题】:Testing click event测试点击事件
【发布时间】:2020-08-20 21:49:57
【问题描述】:

我有一个测试——比较简单:

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

// ...

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

【问题讨论】:

  • 走同步路线,测试通过 - 例如 fireEvent.click(testDom.getByText('Test'))

标签: javascript reactjs jestjs react-testing-library


【解决方案1】:

你有一个承诺,但开玩笑不知道。尝试退货:

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

【讨论】:

    猜你喜欢
    • 2016-01-27
    • 1970-01-01
    • 2013-05-29
    • 2021-05-08
    • 2019-01-04
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    相关资源
    最近更新 更多