reactjs - 使用 enzyme 进行浅层渲染简单测试不起作用

标签 reactjs enzyme

我对 enzyme /浅渲染测试非常陌生。我可能还没有完全理解。

使用这个简化的组件:

export const PlacementOption = (props) => <div/>
const UpdatingSelectField = (props) => <div/>

export class DevicePlatforms extends Component{                                                                                                         
  render(){                                                                                                                                             
    return <div>                                                                                                                                        
      <UpdatingSelectField/>
      {this.props.value.device_platforms && this.props.children}                                                                                        
    </div>                                                                                                                                              
  }                                                                                                                                                     
}   

我正在尝试测试DevicePlatforms。如果 this.props.value.device_platforms 不存在,则不会渲染子项;如果存在,则会渲染子项。

import React from 'react';                                                                                                                              
import { shallow, mount, render } from 'enzyme';                                                                                                        
import { DevicePlatforms } from './Placement.js';                                                                                                       
import { PlacementOption } from './Placement.js'                                                                                                        

describe("<DevicePlatforms/>", function() {                                                                                                             
  it("with all devices selected renders all children", function() {                                                                                     
    const value = {                                                                                                                                     
      device_platforms: "mobile/desktop",                                                                                                               
    }                                                                                                                                                   
    const Component = <DevicePlatforms                                                                                                                  
      value={value}
    >                                                                                                                                                   
      <PlacementOption/>                                                                                                                                
      <PlacementOption/>                                                                                                                                
    </DevicePlatforms>                                                                                                                                  

    const wrapper = shallow(Component)                                                                                                                  
    expect(wrapper.find('PlacementOption')).toBe(2)
  })                                                                                                                                                    

  it("with no devices selected renders no children", function() {                                                                                       
    const value = {}                                                                                                                                    
    const Component = <DevicePlatforms                                                                                                                  
      value={value}                                                                                                                                     
    >                                                                                                                                                   
      <PlacementOption/>                                                                                                                                
      <PlacementOption/>                                                                                                                                
    </DevicePlatforms>                                                                                                                                  

    const wrapper = shallow(Component)                                                                                                                  
    expect(wrapper.find('PlacementOption')).toBe(0)                                                                                                     
  })                                                                                                                                                    
}) 

我在查找调用中尝试过'PlacementOption'PlacementOption

我得到的是:

Expected ShallowWrapper({ many many lines of shallow wrapper content }) to be 3
Expected ShallowWrapper({ many many lines of shallow wrapper content }) to be 0

错误。

如果需要,我可以粘贴“许多行浅层包装内容”,但我认为它不相关,而且我的问题在其他地方 - 可能在我周围的某个地方不知道如何使用浅层渲染内容。

最佳答案

您断言 enzyme ShallowWrapper 等于 3 或 0。这没有意义。

相反,从 .find() 返回的 ShallowWrapper 具有 .length 属性。尝试使用它来代替。

expect(wrapper.find('PlacementOption').length).toBe(2)

关于reactjs - 使用 enzyme 进行浅层渲染简单测试不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39977439/

相关文章:

css - 从 React 中的远程组件在一个组件中触发 CSS 动画

reactjs - 测试包含 React Router Link with Enzyme 的组件

reactjs - 如何用 enzyme 的mount方法测试React组件

reactjs - Jest 性能问题

reactjs - 设置导入的样式组件的样式

reactjs - 如何在 React-Native 中共享生成的二维码?

javascript - 在位于 SRC 文件夹的 index.html 文件中注册一个 service-worker - ReactJS

javascript - 键入对象时出现问题 Typescript 错误 - 无索引签名

reactjs - enzyme 不反射(reflect)状态更新

javascript - 如何在 Jest 中模拟单个类静态