酶浅渲染无法正常工作

时间:2018-12-22 06:32:04

标签: reactjs react-native jestjs enzyme

在我的组件之一中,我在决定返回哪个组件之前检查道具的价值。

但是我在测试中面临的问题是无法为这些组件正确创建快照。

当我自己测试一个组件时,可以正确创建快照,但是当我的组件在返回JSX之前检查prop值时却无法创建快照。

这是我的组件:

import React, {Component} from 'react'
import {Button, Text, View, FlatList, StyleSheet, ActivityIndicator} from 'react-native'
import CategoryCell from '../Components/CategoryCell'
import { connect } from 'react-redux'
import { fetchQuotes } from '../actions/Quotes'

class QuotesCategories extends Component {

    static navigationOptions = {
        title: 'Categories',
    }

    render() {

    return this.props.error ? (
            <View style={styles.Container}>
                <Text style={{color: 'red'}}>FAILED TO LOAD DATA</Text>
                <Button 
                    title='Reload'
                    onPress={this.props.fetchQuotes}
                />
            </View>
        ) : this.props.loading ? (
            <View style={styles.Container}>
                <ActivityIndicator size="large"/>
            </View>
        ) : (
            <View style={styles.Container}>
                <FlatList
                style= {{flex:1, width: '100%'}}
                data= {this.props.data}
                renderItem = {({item,index}) => {
                    return (
                        <CategoryCell Category={item} navigation={this.props.navigation} id={index}/>
                    )
                }}
                keyExtractor = {(item, index) => item.category}
                />  
                <Text>Additions</Text>
            </View>

        )

    }    
}

const styles = StyleSheet.create({
    Container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    }
})

export const mapStateToProps = (state) => {
    return {
        loading: state.loading,
        error: state.error,
        data: state.data
    }
}

export const mapDispatchToProps = (dispatch) => {
    return {
        fetchQuotes: () => {
            dispatch(fetchQuotes())
        }
    }
}
export default connect(mapStateToProps,mapDispatchToProps)(QuotesCategories)

我正在测试这三种情况

        
  1. 出现错误时
  2.     
  3. 加载数据时
  4.     
  5. 数据加载后

我正在测试这三种情况

        
  • 错误和加载是布尔值
  •     
  • 数据是JSON对象的数组

这是针对错误情况的测试:

import React from 'react'
import {shallow} from 'enzyme'

import QuoteCategories from '../../Components/QuoteCategories'
import quotes from '../fixtures/quotes-fixture'

describe('Testing QuoteCategories component', () => {

    it('should load error button when error loading', ( ) => {
            const wrapper = shallow(
                <QuoteCategories
                loading = {false}
                error = {true}
                data = {undefined} 
                />
            )
            expect(wrapper).toMatchSnapshot()
        }
    )
  }
)

但是在QuoteCategories.test.js.snap文件中,这是我看到的快照:

exports[`Testing QuoteCategories component should load error button when error loading 1`] = `
<ContextConsumer>
  <Component />
</ContextConsumer>
`;

为什么我看到这些标签<ContextConsumer>,<Component />

在其他直接返回组件的组件测试中,快照显示正确:

我的组件:

import React from 'react'
import { View, Text, TouchableHighlight, StyleSheet } from 'react-native'

const FavouriteQuoteCell = (props) => {
    return (
        <TouchableHighlight   
            onPress={() => props.navigation.navigate('Quotes',{id: props.item.parentId, category: props.item.category})}
            style={styles.TableCell}
        >
        <View>
            <Text style={styles.Quote}>{props.item.text}</Text>
            <Text style={styles.Author}>-- {props.item.person}</Text>
            <View style={styles.CategoryPill}>
                <Text style={styles.Category}>
                    {props.item.category}
                </Text>
            </View> 
        </View>

        </TouchableHighlight> 
    )
}

export default FavouriteQuoteCell

const styles = StyleSheet.create({
    TableCell: {
        backgroundColor: '#ff6347',
        margin:5,
        padding: 5,
        justifyContent: 'space-around',
        flexDirection: 'column',
        flex: 1 ,
        padding: 10,
        margin: 5,
        borderRadius: 15, 
    },
    "Quote": {
        fontWeight: 'bold',
        color: 'white'
    },
    "Author": {
        fontWeight:'200',
        color:'white',
        justifyContent: 'flex-end',
        alignItems: 'flex-end',
        height: 20
    }, 
    Category: {
        color: '#ff6347',
        fontWeight: 'bold',
        fontSize: 12,
        textTransform: 'capitalize',
        margin: 'auto'
    },
    CategoryPill: {
        marginTop: 10,
        padding: 2,
        height: 20,
        borderRadius: 10,
        backgroundColor: 'white',
        width: 100, 
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center'    
    }
})

测试:

import React from 'react'
import {shallow} from 'enzyme'

import FavouriteQuoteCell from '../../Components/FavouriteQuoteCell'
import {favouriteItem} from '../fixtures/favourites-fixture'


describe('testing FavouriteQuoteCell', () => {
    let wrapper,navigation 

    beforeEach(() => {
        navigation = {
            navigate: jest.fn()
        }
        wrapper = shallow(<FavouriteQuoteCell navigation={navigation} item={favouriteItem}/>)
    })

    it('should render FavouriteQuoteCell correctly', () => {
        expect(wrapper).toMatchSnapshot()
    })
})

快照:

exports[`testing FavouriteQuoteCell should render FavouriteQuoteCell correctly 1`] = `
<TouchableHighlight
  activeOpacity={0.85}
  delayPressOut={100}
  onPress={[Function]}
  style={
    Object {
      "backgroundColor": "#ff6347",
      "borderRadius": 15,
      "flex": 1,
      "flexDirection": "column",
      "justifyContent": "space-around",
      "margin": 5,
      "padding": 10,
    }
  }
  underlayColor="black"
>
  <View>
    <Text
      style={
        Object {
          "color": "white",
          "fontWeight": "bold",
        }
      }
    >
      Believe you can and you"re halfway there
    </Text>
    <Text
      style={
        Object {
          "alignItems": "flex-end",
          "color": "white",
          "fontWeight": "200",
          "height": 20,
          "justifyContent": "flex-end",
        }
      }
    >
      -- 
      Theodore Roosevelt
    </Text>
    <View
      style={
        Object {
          "alignItems": "center",
          "backgroundColor": "white",
          "borderRadius": 10,
          "flex": 1,
          "height": 20,
          "justifyContent": "center",
          "marginTop": 10,
          "padding": 2,
          "width": 100,
        }
      }
    >
      <Text
        style={
          Object {
            "color": "#ff6347",
            "fontSize": 12,
            "fontWeight": "bold",
            "margin": "auto",
            "textTransform": "capitalize",
          }
        }
      >
        inspirational
      </Text>
    </View>
  </View>
</TouchableHighlight>
`;

1 个答案:

答案 0 :(得分:2)

您的history.replaceState(null, null, 'first'); 组件通过以下方式连接到redux:

QuotesCategories

这就是为什么在进行浅层渲染时会在快照中看到redux包装器组件,而不是export default connect(mapStateToProps,mapDispatchToProps)(QuotesCategories) 的原因。

通常要解决此问题的原因还可以导出您的QuotesCategories并将其与您的测试中的名称一起导入:

因此,您的组件文件应具有两个导出:

QuotesCategories

在测试中,您应该使用以下内容导入export class QuotesCategories extends Component { ... } export default connect(mapStateToProps,mapDispatchToProps)(QuotesCategories)

QuotesCategories
相关问题