fix(web): recover route errors on scope change

This commit is contained in:
lingniu
2026-07-16 05:08:40 +08:00
parent c4f6587120
commit 379f1ce941
3 changed files with 46 additions and 1 deletions

View File

@@ -58,4 +58,29 @@ describe('route recovery boundary', () => {
expect(screen.getByText('本地状态 1')).toBeInTheDocument();
expect(mounts).toBe(1);
});
it('retries a failed route when its search scope changes without remounting healthy scopes', () => {
vi.spyOn(console, 'error').mockImplementation(() => undefined);
let healthyMounts = 0;
function ScopeSensitivePage() {
const [params] = useSearchParams();
if (params.get('scope') === 'broken') throw new Error('invalid route scope');
useEffect(() => { healthyMounts += 1; }, []);
return <strong></strong>;
}
function RecoveryHarness() {
const [, setSearchParams] = useSearchParams();
return <><button type="button" onClick={() => setSearchParams({ scope: 'healthy' })}></button><RoutePage page={ScopeSensitivePage} label="告警中心" /></>;
}
render(<MemoryRouter future={ROUTER_FUTURE} initialEntries={['/alerts?scope=broken']}><RecoveryHarness /></MemoryRouter>);
expect(screen.getByRole('alert')).toHaveTextContent('当前模块暂时无法显示');
expect(screen.getByText(/更换筛选条件会自动重试/)).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: '修正筛选条件' }));
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
expect(screen.getByText('新筛选范围已恢复')).toBeInTheDocument();
expect(healthyMounts).toBe(1);
});
});