已开启
test:add demo for react-native-community-netinfo #1943
linsanjiu创建于 6 天前
test:add demo for react-native-community-netinfo #1943
已开启
linsanjiu创建于 6 天前
linsanjiu
linsanjiu
6 天前

test:add demo for react-native-community-netinfo

likedislike
合并受阻
openharmony_ci
openharmony_ci成员
6 天前 评论:

感谢提交 Pull Requests!
Thanks for submitting a pull request.

likedislike
openharmony_ciopenharmony_ci成员
6 天前 添加了label:dco检查成功
linsanjiulinsanjiu
5 天前 强制推送  1 个提交:ccb2e366-test:add demo for react-native-community-netinfo
openharmony_ci
openharmony_ci成员
5 天前 评论:

感谢提交 Pull Requests!
Thanks for submitting a pull request.

likedislike
cpf-manager
cpf-manager5 天前进行代码检视2
24P4/rn-tester/examples/react-native-community-netinfo/test/xtsCases.tsx
已过期
@@ -0,0 +396,4 @@
396+ 
397+ const run = React.useCallback(async () => {
398+ setPhase('running');
399+ refresh();
cpf-manager
cpf-manager5 天前评论:

【AI-Review】【一般】【基础代码问题】【代码逻辑错误】refresh() 未 await 导致测试可能误判

● 问题:在 UseNetInfoInstanceActiveTestrun 函数中(xtsCases.tsx:399),refresh() 被调用后未 await,而是通过固定 800ms 超时(line 400 await new Promise((resolve) => setTimeout(resolve, 800)))等待刷新完成,然后从 latestRef.current(line 404)读取网络状态。useNetInfoInstance 返回的 refresh 函数返回 Promise<NetInfoState>,调用后异步触发原生模块查询并通过 React state 更新 netInfo。在慢速设备或原生模块响应延迟的场景下,800ms 内 refresh 可能未完成,latestRef.current 仍持有初始的 type: 'unknown' 状态。

● 影响:line 405 的断言 assertMainNetInfoFields(current) && current.type !== 'unknown' 在 refresh 未完成时会因 current.type === 'unknown' 而判定为失败,导致该 XTS 用例在正常设备功能无异常的情况下报出假阴性失败,测试结果不稳定。此外,refresh() 返回的 Promise 未被 await 也未 .catch(),若原生模块抛出异常,将产生未处理的 Promise rejection。

● 建议:直接 await refresh() 确保刷新完成后再读取状态,或直接使用 refresh() 的返回值(Promise<NetInfoState>)进行断言:

const run = React.useCallback(async () => {
  setPhase('running');
  try {
    const state = await refresh();
    if (!mountedRef.current) { return; }
    const passed = assertMainNetInfoFields(state) && state.type !== 'unknown';
    setResult(JSON.stringify({ type: state.type, isConnected: state.isConnected, ok: passed }));
    setOk(passed);
    setPhase('done');
    setState(passed);
  } catch (error) {
    if (!mountedRef.current) { return; }
    setResult(String(error));
    setOk(false);
    setPhase('done');
    setState(false);
  }
}, [mountedRef, refresh, setState]);
likedislike
System
系统消息系统
2 天前 评论:

changed this line on 3629cb50 view diff detail

cpf-manager
cpf-manager5 天前进行代码检视1
24P4/rn-tester/examples/react-native-community-netinfo/test/NetInfoTest.tsx
@@ -61,0 +29,4 @@
29+ };
30+ 
31+ const instanceRefresh = async () => {
32+ netInfoInstance.refresh();
cpf-manager
cpf-manager5 天前评论:

【AI-Review】【一般】【基础代码问题】【代码逻辑错误】instanceRefresh 移除了 await,引入时序风险

● 问题:在 NetInfoTest.tsx:32instanceRefresh 函数中,netInfoInstance.refresh() 被调用但未 await,随后通过固定 300ms 超时(line 33)等待。对比 base 版本的实现 await netInfoInstance.refresh(),本 PR 将 await 替换为固定 300ms 超时。useNetInfoInstance 返回的 refresh 返回 Promise<NetInfoState>,在慢速设备上 300ms 内可能未完成,line 37 的 setInstanceState(netInfoInstance.netInfo) 将读取到刷新前的旧状态,用户点击"instanceRefresh"按钮后 UI 仍显示旧数据。

● 影响:用户点击"instanceRefresh"按钮后,若原生模块在 300ms 内未完成刷新,UI 显示的网络状态仍为刷新前的值,功能表现与用户预期不一致。此外 refresh() 返回的 Promise 未被 await 也未 .catch(),若原生模块抛出异常将产生未处理的 Promise rejection。

● 建议:恢复 await 写法,直接使用 refresh 的返回值更新状态:

const instanceRefresh = async () => {
  try {
    const state = await netInfoInstance.refresh();
    if (!mountedRef.current) { return; }
    setInstanceState(state);
  } catch (error) {
    if (!mountedRef.current) { return; }
    setInstanceState(netInfoInstance.netInfo);
  }
};
likedislike
cpf-manager
cpf-manager5 天前进行代码检视2
24P4/rn-tester/examples/react-native-community-netinfo/test/xtsCases.tsx
已过期
@@ -0,0 +340,4 @@
340+ const [hookMounted, setHookMounted] = React.useState(false);
341+ 
342+ React.useEffect(() => {
343+ NetInfo.configure(SAMPLE_CONFIGURE);
cpf-manager
cpf-manager5 天前评论:

【AI-Review】【建议】【软件设计】【其他软件设计问题】NetInfo.configure 全局配置缺少 cleanup

● 问题:在 UseNetInfoWithConfigTestuseEffect(xtsCases.tsx:342-345)中调用 NetInfo.configure(SAMPLE_CONFIGURE) 修改了 NetInfo 全局单例配置,但该 useEffect 依赖数组为空且无 cleanup 函数。NetInfo.configure 修改的是模块级全局状态,组件卸载后配置不会被还原。

● 影响:当用户离开 NetInfoXtsTest 页面后再次进入时,全局配置仍为 SAMPLE_CONFIGUREuseNativeReachability: true),先于该用例执行的 FetchWithStateFieldsTestFetchInterfacesTestRefreshMergedTest 等用例将使用非默认配置运行。虽然当前这些用例的断言(assertMainNetInfoFields / isValidNetInfoState)仅校验结构而非具体值,不会直接失败,但 isInternetReachable 等字段的语义会因 useNativeReachability 差异而不同,不利于测试结果的可重复性。

● 建议:在 useEffect 的 cleanup 中将配置还原为默认值,或在每个用例的 useEffect 中独立设置所需配置并在卸载时清理:

React.useEffect(() => {
  NetInfo.configure(SAMPLE_CONFIGURE);
  setHookMounted(true);
  return () => {
    // 还原为默认配置
    NetInfo.configure({
      reachabilityUrl: 'https://clients3.google.com/generate_204',
      reachabilityMethod: 'HEAD',
      reachabilityHeaders: {},
      reachabilityTest: async () => true,
      reachabilityLongTimeout: 60 * 1000,
      reachabilityShortTimeout: 5 * 1000,
      reachabilityRequestTimeout: 15 * 1000,
      reachabilityShouldRun: () => true,
      shouldFetchWiFiSSID: false,
      useNativeReachability: false,
    });
  };
}, []);
likedislike
System
系统消息系统
2 天前 评论:

changed this line on 3629cb50 view diff detail

linsanjiulinsanjiu
2 天前 强制推送  1 个提交:3629cb50-test:add demo for react-native-community-netinfo
openharmony_ci
openharmony_ci成员
2 天前 评论:

感谢提交 Pull Requests!
Thanks for submitting a pull request.

likedislike
linsanjiulinsanjiu
2 天前 强制推送  7 个提交:fb70b892-6 commits from branch sig9625aad5-test:add demo for react-native-community-netinfo
openharmony_ci
openharmony_ci成员
2 天前 评论:

感谢提交 Pull Requests!
Thanks for submitting a pull request.

likedislike
cpf-manager
cpf-manager2 天前进行代码检视1
24P4/rn-tester/examples/react-native-community-netinfo/test/NetInfoTest.tsx
@@ -61,0 +38,4 @@
38+ };
39+ 
40+ const fetch = async () => {
41+ const fetchState = await NetInfo.fetch();
cpf-manager
cpf-manager2 天前评论:

【AI-Review】【一般】【基础代码问题】【异常处理】fetch 和 refresh 未捕获 Promise reject

● 问题:fetch 函数(NetInfoTest.tsx:40-46)的 await NetInfo.fetch()refresh 函数(NetInfoTest.tsx:48-54)的 await NetInfo.refresh() 均未使用 try/catch 包裹。当 NetInfo 原生模块调用失败(如模块未注册、权限不足、原生侧抛异常)导致 Promise reject 时,异常作为未处理的 Promise rejection 冒泡。触发路径:用户点击 "fetch" 或 "refresh" 按钮 → onPress 调用 fetch()/refresh() → NetInfo.fetch()/refresh() reject → await 抛出异常 → async 函数返回 rejected Promise → onPress 未捕获 → 未处理的 Promise rejection。

● 影响:一般。Dev 模式下弹出 yellow/red box 警告干扰测试;在生产环境的特定 Promise rejection 处理配置下可能触发应用异常退出。测试页面不会显示错误信息,用户无法判断操作失败原因。

● 建议:用 try/catch 包裹 await 调用,在 catch 分支更新状态以提示失败:

const fetch = async () => {
  try {
    const fetchState = await NetInfo.fetch();
    if (!mountedRef.current) { return; }
    setNetInfoState(fetchState);
  } catch (error) {
    if (!mountedRef.current) { return; }
    setNetInfoState({ type: 'unknown', error: String(error) });
  }
};

refresh 函数同理。

likedislike