reactjs - 如何测试 React Loadable 组件

标签 reactjs unit-testing jestjs enzyme react-loadable

我有这个组件:

import React from 'react';

const UploadAsync = Loadable({
  loader: () => import('components/Upload'),
  loading: () => <LoadingComponent full />
});

const Component = () => {
  return <UploadAsync />
}

export default Component

测试:

import React from 'react';
import Component from './index';

describe('Component', () => {
  it('should render correctly with upload component', () => {
    const tree = create(<Component />).toJSON();

    expect(tree).toMatchSnapshot();
  });
});

如何在快照中看到上传组件而不是加载组件?

exports[`Content should render correctly with form component 1`] = `
  <div>
    <Loading
      full={true}
    />
  </div>
`;

到目前为止,我已经尝试过 setTimeOutPromises

最佳答案

在测试之前使用Loadable.preloadAll()即可访问所需的组件。

Docs

简单的例子:

all imports here

Loadable.preloadAll()

describe('TestName', () => {
 //tests
})

关于reactjs - 如何测试 React Loadable 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50176706/

相关文章:

unit-testing - VueJS-开 Jest : How to test/spy the methods which are getting called inside the watcher?

javascript - 如何在 next.js 应用中使用谷歌分析?

javascript - 无法使用 microbundle 和 React 渲染自定义组件包中的 useState、useEffect Hook

reactjs - i18next-icu 在 react 中翻译时不支持 {{(双括号)

TDD 的 JavaScript 单元测试工具

c# - 编写自定义测试框架

javascript - 在 react-router-dom 中将 id 作为 props 传递

unit-testing - 服务类的 Grails 单元测试中的参数数据绑定(bind)

react-native - Jest 和 React Native 图标导入问题

javascript - 开 Jest : Cannot spy the property because it is not a function; undefined given instead