访问 Enzyme 中的嵌套状态 [英] Access nested State in Enzyme

查看:27
本文介绍了访问 Enzyme 中的嵌套状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的状态是这样的

this.state = { 
    potato: {
        chips: 'yum',
        fries: 'even better',
    }
}

然后我想访问fries.wrapper.state('potato') 带我到第一层,如何深入?这不是

Then I want to access fries. wrapper.state('potato') get me to the first level, how to go deeper? It is not

  • wrapper.state('potato').state('fries')
  • wrapper.state('potato', 'fries')
  • wrapper.state(['potato', 'fries'])
  • wrapper.state('potato').fries
  • wrapper.state('potato')['fries']

当我这样做

const potato = wrapper.state('potato');

然后

console.log(potato);

我明白

{
     chips: 'yum',
     fries: 'even better',
}

但是,通过

console.log(potato.chips);
console.log(potato.fries);
console.log(potato.potato.chips)

所有退货

未定义

我不明白...

推荐答案

您所要做的就是首先获取状态并使用点符号访问属性.

All you have to do is get the state at first and access the property by using dot notation.

wrapper.state().potato.fries

wrapper.state('potato').fries

您也可以使用括号表示法.

You can also use bracket notation.

wrapper.state()['potato']['fries']

wrapper.state('potato')['fries']

这篇关于访问 Enzyme 中的嵌套状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆