我正在尝试对订阅函数进行测试,但是我对Jest和测试不熟悉,所以找不到解决方案。情况如下:
此订阅表单分为两部分,第一个部分将数据发送到第二个部分。
我的问题是,如何在第二种形式的测试中通过反应导航设置默认参数?
也许您会对代码有更好的了解:
我的测试结果:
TypeError: Cannot read property 'getParam' of undefined
> 33 | lastName: navigation.getParam('lastName', undefined),
我的测试:
describe('checking datas', () => {
it('checking good datas', () => {
const formUser = {
birthday: '17/07/1992',
country: 'France',
refGolf: 'Bruges',
};
const datas = renderer.create(<SubscribeNext />).getInstance();
expect(datas.checkValues(
formUser.birthday,
formUser.country,
formUser.refGolf,
)).toBeTruthy();
});
我的第一个表单正在发送数据:
toSubscribeNext() {
const {
navigation
} = this.props;
const {
lastName,
firstName,
email,
username,
password,
passwordCheck
} = this.state;
if (this.checkValues(lastName, firstName, email, username, password, passwordCheck)) {
navigation.navigate('SubscribeNext', {
lastName,
firstName,
email,
username,
password
});
}
}
我的第二种形式是得到它们:
export default class Subscribe extends Component {
constructor(props) {
super(props);
const { navigation } = props;
this.state = {
lastName: navigation.getParam('lastName', undefined),
}
}
...
答案 0 :(得分:2)
1-第一次使用:
this.props.navigation.navigate('SubscribeNext', { lastName: this.state.lastName, ... })}
2-以及您的第二种形式:
使用componentWillReceiveProps接收道具并更新状态
...
this.state = {lastName:''};
....
componentWillReceiveProps(nextProps) {
if (nextProps.navigation.state.params.lastName) {
this.setState({lastName:nextProps.navigation.state.params.lastName});
}
}
答案 1 :(得分:0)
第二次使用
this.state = {
name: this.props.navigation.state.params.name
.
.
.
}
以及您的第一种形式:
this.props.navigation.navigate('screen name', { email: this.state. email, name: name, ... })}