我有一个create-react-native-app
,我需要编写一个测试,以防止用户将应用程序带回前台时出现问题的回归(似乎很难重新连接socket.io连接)。
目前我必须通过以下步骤手动测试:
我如何为此编写自动化测试?我在应用程序中使用jest
进行单元测试。
答案 0 :(得分:0)
基本用法
要查看当前状态,您可以检查AppState.currentState,它将保持最新状态。但是,当AppState通过网桥检索时,currentState在启动时将为null。
import React, {Component} from 'react'
import {AppState, Text} from 'react-native'
class AppStateExample extends Component {
state = {
appState: AppState.currentState
}
componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener('change', this._handleAppStateChange);
}
_handleAppStateChange = (nextAppState) => {
if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
console.log('App has come to the foreground!')
}
this.setState({appState: nextAppState});
}
render() {
return (
<Text>Current state is: {this.state.appState}</Text>
);
}
}