我们有一个加载有react-loadable的组件,在该组件内部,我们还有一个带有react-loadable加载的子组件。我开玩笑面对的问题是无法加载子组件,尝试了一些来自互联网的建议。请提供帮助,以下是必填信息:
lazyload.js
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
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
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
})
})
答案 0 :(得分:0)
答案是第一个回答与您的问题类似的问题: How test a React Loadable component
这为我解决了问题!
简而言之,可以这样做:
// Import your components
import Loadable from 'react-loadable';
Loadable.preloadAll()
describe('TestName', () => {
//tests
});