Jest/Enzyme Class Component testing with React Suspense and React.lazy child component(JEST/酶类组件测试中的反应暂停和反应延迟子组件)
问题描述
因此,我将类组件中使用的导入转换为React.lazy导入API,并将其包装在一个挂起标记中。当我测试那个类组件时,酶抛出一个错误"Enzyme Internal Error: unknown node with tag 13"
。是否有一种方法可以呈现和测试延迟加载组件的装载,而不是使用浅层呈现?
我已经尝试了异步等待,直到延迟加载的承诺解决,但也不起作用,如下所示:
it('async await mount', () async () => {
const wrapper = await mount(<Component />)
}
以下是示例代码:
Component.js
import React, { PureComponent, Suspense } from 'react'
const ChildComponent = React.lazy(() => import('../ChildComponent'))
export default class Component extends PureComponent {
constructor() {
super()
this.state = {
example: null
}
}
doSomething = () => this.setState({
example: 'example'
})
render() {
return (
<div>
<p>Example</p>
<Suspense fallback={<div>...loading</div>}>
<ChildComponent
example={this.state.example}
doSomething={this.doSomething}
/>
</Suspense>
</div>
)
}
}
Component.test.js
import React from 'react'
import renderer from 'react-test-renderer'
import { mount } from 'enzyme'
import Component from '../../Component'
describe('Component', () => {
// snapshot renders loading and not children
it('example snapshot of tree', () => {
const tree = renderer.create(<Component />).toJSON()
expect(tree).toMatchSnapshot()
})
it('example mount test', () => {
// this test fails and throws error I state above
const wrapper = mount(<Component />)
wrapper.setState({ example: 'example' })
expect(wrapper.state.example).toBe('example')
})
})
我读到酶还不支持Reaction 16.6 Suspense API,但我想知道是否还有方法测试挂载的,以便我可以使用酶的simulate
和find
API。
推荐答案
解决方案
ChuckJHardy链接的GitHub问题已解决、合并并发布。从1.14.0开始,您可以在酵素中使用挂载API。
引用
https://github.com/airbnb/enzyme/pull/1975
这篇关于JEST/酶类组件测试中的反应暂停和反应延迟子组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JEST/酶类组件测试中的反应暂停和反应延迟子组件


- 失败的 Canvas 360 jquery 插件 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Fetch API 如何获取响应体? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Flexslider 箭头未正确显示 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 400或500级别的HTTP响应 2022-01-01