首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jest测试在react-loadable中失败

Jest测试在react-loadable中失败
EN

Stack Overflow用户
提问于 2020-06-15 20:46:38
回答 1查看 403关注 0票数 3

我们有一个使用react-loadable加载的组件,在组件内部也有使用react-loadable加载的子组件。我在jest中遇到的问题是无法加载子组件,尝试了几个来自互联网的建议。请对此提供帮助,以下是必需的详细信息:

lazyload.js

代码语言:javascript
复制
import Loadable from 'react-loadable';
import Loader from './components/ui/genericComponents/Loader';
export const lazyload = (func, fallback) => {
    return Loadable({
        loader: func,
        loading: fallback || Loader,
    });
};

Component.js

代码语言:javascript
复制
import {lazyload} from '../lazyload'
const childComponent1=lazyload(()=>import('./child/childComponent1'));
const childComponent2=lazyload(()=>import('./child/childComponent2'));
const childComponent3=lazyload(()=>import('./child/childComponent3'));

Component.test.js

代码语言:javascript
复制
import React from 'react';
import {configure,mount} from 'enzyme';
import {Component} from '../src/Component';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
configure({ adapter: new Adapter() });

let Wrapper;
beforeAll(() => {
    Wrapper= Enzyme.mount(<Component{...someprops} />);
});

describe('TestSuite for Component',()=>{
//some axios calls
console.log(wrapper.debug()) //I could see the entire wrapper
it('childComponent1',()=>{
console.log(wrapper.debug()) //I only see the loader and not the child Components
})
it('childComponent2',()=>{
console.log(wrapper.debug()) //I only see the loader and not the child Components
})
})
EN

回答 1

Stack Overflow用户

发布于 2020-09-21 03:43:58

这里的第一个答案与您的问题类似:How test a React Loadable component

这为我解决了这个问题!

简而言之,可以这样做:

代码语言:javascript
复制
// Import your components
    
import Loadable from 'react-loadable';
    
Loadable.preloadAll()
    
describe('TestName', () => {
    //tests
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62388532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档