【问题标题】:React testing library to cover the lazy loadReact 测试库覆盖延迟加载
【发布时间】:2021-06-14 11:32:06
【问题描述】:

如何覆盖 react 测试库中的延迟加载组件。

import React, {lazy} from 'react';
const ownerInfo = lazy(() => import('../abc'))

const compone = () => {
  return <Suspense><abc /></Suspense>
}
export default compone

test.spec.js

 import React from 'react'
 import {render, fireEvent} from '@testing-library/react'
 import configureStore from 'redux-mock-store'

 ...

【问题讨论】:

标签: reactjs jestjs react-hooks react-testing-library redux-mock-store


【解决方案1】:

观看视频后,我能够弄清楚如何覆盖延迟加载。假设您有延迟加载组件。

Lazyload.js

import React, {lazy} from 'react'
const LazyComponent = lazy(() => import('./LazyComponent'))
const LazyLoad = () => {
   return (
      <div>
         <div> Lazy component is here: </div>
         <React.Suspense fallback={null}>
             <LazyComponent />
         </React.Suspense>
      </div>
   )
}
export default LazyLoad

LazyComponent.js

import React from 'react'
export default () => <div>I am lazy ! </div>

lazyLoad.spec.js

import React from 'react'
import {render, waitFor } from 'react-testing-library'
import LazyLoad from 'LazyLoad'

test('renders lazy component', async () => {
    const { getByText } = render(<LazyLoad />)
    await waitFor(() => expect(getByText('I am lazy !' )).toBeInTheDocument())
})

【讨论】:

    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 2021-05-30
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多