代码之家  ›  专栏  ›  技术社区  ›  Bertrand P

酶,如何获取道具中传递的成分

  •  9
  • Bertrand P  · 技术社区  · 8 年前

    我想测试的是什么 MyComponent 它的价值是什么 checked 电影的道具 Switch 组件,它是 FormControlLabel 组成部分:

    class MyComponent extends Component {
    
     (...)
    
     render() {
      return (
       <FormControlLabel
        name={`formControl`}
        control={
          <Switch
            name={`switch`}
            data
            checked={this.state.isChecked}
            onClick={this.handleChange}
            value={checked}
            />
        }
        />
      );
     }
    }
    

    我可以访问 FormControlLabel 像这样的组件:

    const wrapper = shallow(<MyComponent />);
    
    wrapper.find('[name="formControl"]');
    

    我试图进入 转换 像这样的组件,但它不工作:

    wrapper.find('[name="switch"]');
    

    我怎样才能访问 选中的 道具 转换 组成部分

    美国石油学会
    • 浅的
    版本
    • 酶:3.3.0
    • 反应:16.2.0
    适配器
    • 酶-适配器-反应-16
    2 回复  |  直到 8 年前
        1
  •  4
  •   Bertrand P    8 年前

    我刚刚找到了一个解决方案:

    expect(wrapper.find('[name="formControl"]').prop('control').props.checked).toEqual(true);
    
        2
  •  0
  •   Up209d    7 年前
    const controlComponent = shallow(wrapper.find(FormControlLabel).props().control);
    expect(controlComponent.props().checked).toBeTruthy();
    
    推荐文章