【问题标题】:How can you test functions and variables within your react component's render function?如何在反应组件的渲染函数中测试函数和变量?
【发布时间】:2017-11-23 11:17:24
【问题描述】:

我正在尝试提高我的 react 组件的测试覆盖率,但是我在测试组件的 render 方法中声明的变量和函数时遇到了问题。以下是我无法涵盖的几个示例:

1)

cityStateZip = `${cityStateZip} - ${location.zipExtension}`;

2)

directionsUrl = `maps://maps.apple.com/?saddr=My+Location&daddr=${gpsCoords.lat}+${gpsCoords.lng}`;

3)

const onClick = (pricingTileId) => {
  if (store.selectedPharmacy !== pricingTileId) {
    store.setPharmacy(pricingTileId);
  }
};

代码如下:

class Tile extends Component {
  const { location, store } = this.props;
  render() {
    let directionsUrl = `https://maps.google.com/?saddr=My+Location&daddr=${gpsCoords.lat}+${gpsCoords.lng}`;
    if (navigator.platform.indexOf('iPhone') !== -1
      || navigator.platform.indexOf('iPod') !== -1
      || navigator.platform.indexOf('iPad') !== -1) {
      directionsUrl = `maps://maps.apple.com/?saddr=My+Location&daddr=${gpsCoords.lat}+${gpsCoords.lng}`;
    }

    let cityStateZip = `${location.city}, ${location.state} ${location.zip}`;
    if (location.zipExtension) {
      cityStateZip = `${cityStateZip} - ${location.zipExtension}`;
    }

    const onClick = (pricingTileId) => {
      if (store.selectedLocation !== pricingTileId) {
        store.setLocation(pricingTileId);
      }
    };

    let selectedClass;
    if (store.selectedLocation === id) {
      selectedClass = 'selected';
    }

    return (

    )

有没有一种有效的方法来测试我忽略的渲染函数中声明的变量和函数? (我正在使用 Jest 和 Enzyme 进行测试)。谢谢!

【问题讨论】:

  • 作为一般规则,很难在 做很多事情的大型函数中测试逻辑。这就是为什么建议编写几个较小的方法供渲染函数使用的原因;让getCityStateZip() 处理 zip 扩展逻辑,getDirectionsUrl() 处理特定于设备的逻辑

标签: javascript reactjs testing jestjs enzyme


【解决方案1】:

你可以像这样重构你的组件:

class Tile extends Component {

  isMobile = () => {
    let mob = navigator.platform

    if (mob.indexOf('Iphone')) return true
    if (mob.indexOf('Ipad')) return true
    if (mob.indexOf('Ipod')) return true

    return false
  }

  isZipValid = () => !!this.props.location.zipExtension
  isLocationValid = (id) => this.props.store.location === id


  handleClick = (pricingTileId) => {
    const { store } = this.props;

    if (store.selectedLocation !== pricingTileId) {
      store.setLocation(pricingTileId);
    }
  }

  render() {


    let directionsUrl
    let selectedClass = isLocationValid() && 'selected';
    let cityStateZip = `${location.city}, ${location.state} ${location.zip}`;

    if (!isMobile()) {
      directionsUrl = `maps://maps.apple.com/?saddr=My+Location&daddr=${gpsCoords.lat}+${gpsCoords.lng}`;
    }

    if (isZipValid()) {
      cityStateZip = `${cityStateZip} - ${location.zipExtension}`;
    }
    return (
      <div> Anything</div>
    )
  }

..

============== 第 2 部分 ===================

您可以将它们提取到单独的文件中,例如 libhelpers 然后将其导入您的组件。

像这样:

助手:

//helpers.js

export const isMobile = (mob) => {

    if (mob.indexOf('Iphone')) return true
    if (mob.indexOf('Ipad')) return true
    if (mob.indexOf('Ipod')) return true

    return false
  }

最后在组件上:

   export { isMobile } from './helpers'

   if(isMobile(navigator.platform)){

    //you just abstracted the function

   }

结论:您的isMobile() 可以很容易地从助手测试并提供给任何组件:)

现在您可以轻松地逐个测试功能

希望对你有帮助 :D

请,

【讨论】:

  • 有道理,谢谢!这可能是一个愚蠢的问题,但我对测试这种性质非常陌生。如何在我的测试中访问这些方法?例如 isMobile 函数。我正在导入并控制台记录我的 Tile 组件,但似乎无法在任何地方找到这些方法。不太确定如何测试它们。非常感谢您的帮助:)
  • 查看我上面帖子的第二部分
猜你喜欢
  • 2016-05-17
  • 2019-09-15
  • 2018-01-26
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 2018-06-20
  • 2018-06-15
  • 2019-12-25
相关资源
最近更新 更多