开玩笑:TypeError:无法读取未定义的属性“值”

时间:2018-07-26 16:41:56

标签: unit-testing react-native jestjs

我一直在开玩笑测试我的React Native组件时遇到问题。我的大部分组件都返回通过prop传递的动态值。

我不断收到的错误消息告诉我,有一个TypeError:无法读取未定义的属性'values'。

我想知道是否必须模拟数据以访问“值”,如果是的话,我将如何去做呢?

VideoCard.js

import React, { Component } from 'react';
import { Text, View, TouchableOpacity, ImageBackground, Image } from 'react-native';

export default class VideoCard extends Component {
  constructor(props) {
    super(props)

  };

  pushScreen = () => { 
    this.props.navigator.push({
      screen: 'project.VideoScreen',
      title: `${this.props.data.values.name}`,
      passProps: {...this.props}
    });
  };

  render() {
    return (
      <TouchableOpacity onPress={this.pushScreen} style={styles.containerGrid}>
        <View style={styles.line}></View>
        <View style={styles.innerGrid}>
          <View style={styles.textBox}>
            <Text style={styles.text}>This HIIT Workout Gives At-Home Routines a Good Name</Text>
            <View style={styles.authorBox}>
              <Text style={styles.lightText}>By David Glenn Aveni | February 6th</Text> 
            </View>
          </View>
          <Image source={{ uri: `${this.props.data.values.imgURL}` }} style={styles.image} />
        </View>
      </TouchableOpacity>
    );
  }
}

VideoCard.test.js

import React from 'react';
import { shallow } from 'enzyme';
import renderer from 'react-test-renderer';

import VideoCard from '../../../src/screens/home/VideoCard';
import VideoData from '../../../src/screens/home/VideoData';

beforeEach(() => {
	// prevents error boundary logs from automatically being thrown
	spyOn(console, 'error');
})

test('renders correctly', () => {
	const wrapper = renderer.create(<VideoCard />).toJSON();
})

Console Error

0 个答案:

没有答案