【发布时间】:2018-11-09 16:57:11
【问题描述】:
我正在尝试用 jest 在应用程序中创建一个测试,这是我的一些代码行:
import React, { Component } from 'react';
import {...} from 'react-native';
import jwt_decode from 'jwt-decode';
class CreateProduct extends Component {
constructor(props) {
super(props);
this.keyboardHeight = new Animated.Value(0);
this.imageHeight = new Animated.Value(199);
this.state = {
isButtonsHidden: false,
title: '',
price: '',
description: '',
isDialogVisible: false,
messageError: '',
};
}
_goBack = async () => {
const {state} = this.props.navigation;
var token = state.params ? state.params.token : undefined;
this.props.navigation.navigate('MyProducts', {token:token});
}
我想测试一下导航:
this.props.navigation.navigate('MyProducts', {token:token});
现在这是测试的尝试:
describe('Testing navigation', () =>{
let wrapper = null
const spyNavigate = jest.fn()
const props = {
navigation:{
navigate: spyNavigate
}
}
const params = {
token: 'randomToken'
}
beforeEach(() => {
wrapper = shallow(<CreateProduct {...props}/>)
wrapper.setState({params: params})
})
it('should test navigation', () => {
wrapper.instance()._goBack(params)
expect(spyNavigate).toHaveBeenCalled()
})
})
但我收到了this error。
我假设我传递const params 的方式有误。你能帮我告诉我模拟令牌以及在屏幕中导航的最佳方式是什么吗?
谢谢。
【问题讨论】:
标签: react-native jestjs react-native-navigation