开玩笑-在React中测试模态会出错 [英] Jest - Testing modals in React gives error

查看:171
本文介绍了开玩笑-在React中测试模态会出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Jc与react-test-renderer一起使用来测试反应组件.但是,如果我像这样测试react-mui模态对话框:

I am using react-test-renderer with Jest to test react components. But if I test a react-mui modal dialog like this:

describe('Dashboard', function () {
  let dashboard;
  beforeEach(async () => {
    testRenderer = TestRenderer.create(<MemoryRouter><Route component={Dashboard} /></MemoryRouter>);
    dashboard = testRenderer.root.findByType(Dashboard);
    await waitForExpect(() => expect(dashboard.instance.state.hasLoaded).toBeTruthy());
  });


  it('opens dialog on clicking the new class', async () => {
    const button = testRenderer.root.findByType(Button);
    expect(dashboard.instance.state.showDialog).toBeFalsy();
    button.props.onClick();
    expect(dashboard.instance.state.showDialog).toBeTruthy();
  });
});

但是,然后我得到一个错误:

But, then I get an error:

错误:失败:错误:未捕获'警告:无效的容器具有 已提供.这可能表明正在使用其他渲染器 除了测试渲染器. (例如,ReactDOM.createPortal 在ReactTestRenderer树中.)不支持此操作.%s'

Error: Failed: "Error: Uncaught 'Warning: An invalid container has been provided. This may indicate that another renderer is being used in addition to the test renderer. (For example, ReactDOM.createPortal inside of a ReactTestRenderer tree.) This is not supported.%s'

我应该如何测试然后对门户网站做出反应以使此测试正常进行?

How should I test then react portals to make this test work?

推荐答案

尝试将其放入测试中:

beforeAll(() => {
    ReactDOM.createPortal = jest.fn((element, node) => {
        return element
    })
});

这篇关于开玩笑-在React中测试模态会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆