简体   繁体   English

我如何能开玩笑地遵守此组件测试

[英]how can i comply with this component test with jest

I have a component with some props but when i want to test if he rendered, the test failed and i have this error message: "TypeError: Cannot read property 'name' of undefined" 我有一个带有一些道具的组件,但是当我想测试他是否渲染时,测试失败,并且我收到以下错误消息:“ TypeError:无法读取未定义的属性'name'”

this is my component : 这是我的组件:

  render(){
   const character = this.props.character ? this.props.character : null;
   const characterName = this.props.character.name ? 
   this.props.character.name : null;
 const characterStatus = this.props.character.status  ? 
   this.props.character.status : null;

return(
  <TouchableOpacity 
  onPress={() => {}}
  style={styles.item_container}
  >
    <Image style={styles.image} source={{uri: character.image}}/>
    <View style={styles.content_container}>
      <View >
        <Text style={styles.name}>{characterName}</Text>
        <Text style={styles.status}>{characterStatus}</Text>
      </View>
    </View>
  </TouchableOpacity>
);

And my jest test: 我开玩笑的测试:

 it('renders ListItem without children', () => {
const rendered = renderer.create( <ListItem  /> ).toJSON();
expect(rendered).toBeTruthy();
 })   

how can I pass this test and test correctly if my component is well rendered? 如何通过此测试并正确测试我的组件是否呈现良好?

You've got a couple of problems. 你有几个问题。

Firstly in your component you are doing the following 首先,在您的组件中,您需要执行以下操作

const character = this.props.character ? this.props.character : null;
const characterName = this.props.character.name ? this.props.character.name : null;

This is going to cause an undefined error every time the this.props.character is null as you will not be able to get the name property from the character prop. 每当this.props.character为null时,这将导致未定义的错误,因为您将无法从character prop获取name属性。 You need to come up with a way of handling the response whenever the this.props.character is null. 每当this.props.character为null时,您需要想出一种处理响应的this.props.character Whether that is returning null for your component or using a default value. 是否为您的组件返回null或使用默认值。 The choice is up to you. 这个选择由你。

Secondly your test is failing because you are not passing the character prop which your component relies on, see the first point above. 其次,由于您没有通过组件所依赖的角色道具,因此测试失败了,请参见上面的第一点。 You need to create an object that is a sample character and pass it to your ListItem . 您需要创建一个作为示例字符的对象,并将其传递给ListItem Something like this, you can fill in the correct information. 这样的话,您可以填写正确的信息。

it('renders ListItem without children', () => {
  const character = { name: '<CHARACTER_NAME>', image: '<IMAGE_URI>', status: '<CHARACTER_STATUS>'};
  const rendered = renderer.create( <ListItem  character={character}/> ).toJSON();
  expect(rendered).toBeTruthy();
}) 

If you want your test to pass when you don't pass the character prop then you need to put some protections so that nothing is undefined when the character prop is null. 如果要在不通过字符道具的情况下通过测试,则需要设置一些保护措施,以便在字符道具为null时未定义任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM