使用酶进行测试时,如何在 React 组件中模拟“this.props”?

马克斯·特拉维斯

所以,我的目标是cashedImageCheker根据从this.props-> 接收到的imageFetchedURL来达到必须返回 string的 func 我的问题是我无法理解如何将此值放入cashedImageCheker我的组件的相应 funcBanners

我尝试使用 Enzyme.setProps({})方法在我的测试组件中传输模拟道具,但现在我只得到undefined. 下面是我的测试和组件代码。

感谢您的任何帮助...

测试文件:

import React from 'react'
import { mount, shallow } from 'enzyme'

import Banners from '../'

describe('<Banners />', () => {
  it('imageFetchedURL must return true', () => {
    const Component = shallow(<Banners />)

    Component.setProps({
      imageFetchedURL: 'https://google.com/la-la-la/1.jpg'
    })
    const { imageFetchedURL } = Component.instance().cashedImageCheker()

    expect(imageFetchedURL.length).toBe(33)
  })
})

组件文件:

import PropTypes from 'prop-types'
import React, { Component } from 'react'

class Banners extends Component {
  cashedImageCheker = () => {
    const { imageFetchedURL } = this.props
    const imageFetchCached = imageFetchedURL && imageFetchedURL.length > 1

    return imageFetchCached
  }

  render() {
    const isLocalImgCached = this.cashedImageCheker()
    return (
      <div className='bannerWrap'>
        {isLocalImgCached}
      </div>
    )
  }
}

export default Banners
舒巴姆·哈特里

在测试你的组件时,你可以传递它所需的道具,同时浅安装组件,如

describe('<Banners />', () => {
  it('imageFetchedURL must return true', () => {
    const Component = shallow(<Banners imageFetchedURL={'https://google.com/la-la-la/1.jpg'}/>)

    const imageFetchedURL = Component.instance().cashedImageCheker()

    expect(imageFetchedURL.length).toBe(33)
  })
})

代码测试用例中的主要问题还在于,cashedImageChecker 不会返回一个对象而是一个值,因此您需要编写

const imageFetchedURL = Component.instance().cashedImageCheker()

代替

const { imageFetchedURL } = Component.instance().cashedImageCheker()

进行上述更改也将允许您setProps测试组件方法。

describe('<Banners />', () => {
  it('imageFetchedURL must return true', () => {
    const Component = shallow(<Banners />)

    Component.setProps({
      imageFetchedURL: 'https://google.com/la-la-la/1.jpg'
    })
    const imageFetchedURL = Component.instance().cashedImageCheker()

    expect(imageFetchedURL.length).toBe(33)
  })
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在React中引用组件的props?

来自分类Dev

如何测试使用 props 渲染其他组件的 React 组件?

来自分类Dev

当组件使用事件时使用酶测试 React 组件

来自分类Dev

如何在React样式组件中使用this.props

来自分类Dev

如何将props传递给在React中作为props传递的组件?

来自分类Dev

如何在 React 的类组件的实例方法中模拟 fetch?

来自分类Dev

如何使用react和typescript访问react功能组件中的props?

来自分类Dev

如何使用React +酶测试子组件不存在

来自分类Dev

如何模拟在React JS组件中创建的实例?

来自分类Dev

我该如何在React组件中模拟服务以隔离单元测试?

来自分类Dev

使用react-router从this.props.children中访问React组件

来自分类Dev

为什么要在React组件的props中传递回调,而不是使用react-redux的connect?

来自分类Dev

如何在React 0.12+中处理组件参考变量和props?

来自分类Dev

如何在 React JS 中的 MultiSelect 组件的 props 处分配数组

来自分类Dev

使用带有来自 React 组件的 props 的样式组件

来自分类Dev

如何使用Jest测试React中mapStateToProps中的方法-酶

来自分类Dev

在 React 中传递 props

来自分类Dev

如何通过React中的props将函数传递给不同组件中的函数?

来自分类Dev

如何在React中不使用props的情况下将函数传递给组件的层次结构?

来自分类Dev

React onClick在加载时触发多次,并且在组件props中不包含回调函数

来自分类Dev

如何将值附加到React组件子代的props中?

来自分类Dev

如何将 props 及其方法动态传递给 React 中的组件?

来自分类Dev

为什么在被调用的 React 函数组件中单独使用 props?

来自分类Dev

如何将组件 props 从 React 传递到 Redux?

来自分类Dev

在React子组件上使用props更新状态

来自分类Dev

像React中的{... props}一样,如何在Vue中破坏props?

来自分类Dev

用Jest测试时如何用模拟替换React组件

来自分类Dev

如何在React TypeScript中实现e和props?

来自分类Dev

当在 BrowserRouter 组件中更新 props 时,不会调用 react-router Route 组件构造函数

Related 相关文章

  1. 1

    如何在React中引用组件的props?

  2. 2

    如何测试使用 props 渲染其他组件的 React 组件?

  3. 3

    当组件使用事件时使用酶测试 React 组件

  4. 4

    如何在React样式组件中使用this.props

  5. 5

    如何将props传递给在React中作为props传递的组件?

  6. 6

    如何在 React 的类组件的实例方法中模拟 fetch?

  7. 7

    如何使用react和typescript访问react功能组件中的props?

  8. 8

    如何使用React +酶测试子组件不存在

  9. 9

    如何模拟在React JS组件中创建的实例?

  10. 10

    我该如何在React组件中模拟服务以隔离单元测试?

  11. 11

    使用react-router从this.props.children中访问React组件

  12. 12

    为什么要在React组件的props中传递回调,而不是使用react-redux的connect?

  13. 13

    如何在React 0.12+中处理组件参考变量和props?

  14. 14

    如何在 React JS 中的 MultiSelect 组件的 props 处分配数组

  15. 15

    使用带有来自 React 组件的 props 的样式组件

  16. 16

    如何使用Jest测试React中mapStateToProps中的方法-酶

  17. 17

    在 React 中传递 props

  18. 18

    如何通过React中的props将函数传递给不同组件中的函数?

  19. 19

    如何在React中不使用props的情况下将函数传递给组件的层次结构?

  20. 20

    React onClick在加载时触发多次,并且在组件props中不包含回调函数

  21. 21

    如何将值附加到React组件子代的props中?

  22. 22

    如何将 props 及其方法动态传递给 React 中的组件?

  23. 23

    为什么在被调用的 React 函数组件中单独使用 props?

  24. 24

    如何将组件 props 从 React 传递到 Redux?

  25. 25

    在React子组件上使用props更新状态

  26. 26

    像React中的{... props}一样,如何在Vue中破坏props?

  27. 27

    用Jest测试时如何用模拟替换React组件

  28. 28

    如何在React TypeScript中实现e和props?

  29. 29

    当在 BrowserRouter 组件中更新 props 时,不会调用 react-router Route 组件构造函数

热门标签

归档