我正在为我的应用程序构建脱机功能,并且无法弄清楚如何正确测试它们。
我正在使用包装在上下文中的net-info库为应用提供有关设备连接状态的信息:
const Connection = ({ children })=>{
const netInfo = useNetInfo();
const {
isConnected,
type,
} = netInfo;
useEffect(()=>{
console..log(`CONNECTION ${isConnected} ${type}`);
},[ isConnected, type]);
return (
<ConnectionContext.Provider
value={{
online: netInfo.isConnected,
type: netInfo.type,
}}
>
{ children }
</ConnectionContext.Provider>
)
}
如果我手动断开wifi和数据连接,则该应用将失去与开发服务器的连接,并停止向我提供信息/日志,并且我无法真正了解正在发生的事情。
我该如何解决?
答案 0 :(得分:0)
有两种测试方法。简单的方法和困难的方法。
困难的方法,为android创建一个apk并将其放入仿真器中进行安装和测试。
模拟isConnected变量。这将在5秒钟后更改该变量,因此您可以看到它从在线到离线。
const [isConnected,setIsConnected] = useState(netInfo.isConnected);
const [type, setType] = useState(netInfo.type);
useEffect(()=>{
setTimeout(()=>{
setIsConnected(false);
},5000);
},[]);
//还需要将上下文提供程序的参数从netInfo更改为状态变量。