import { cleanup, render, screen } from '@testing-library/react'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { MemoryRouter } from 'react-router-dom'; import { isRouteChunkError, RoutePage } from './RouteBoundary'; afterEach(() => { cleanup(); vi.restoreAllMocks(); window.sessionStorage.clear(); }); describe('route recovery boundary', () => { it('recognizes deployment-related lazy chunk failures', () => { expect(isRouteChunkError(new TypeError('Failed to fetch dynamically imported module: /assets/Monitor-old.js'))).toBe(true); expect(isRouteChunkError(new Error('Loading chunk 18 failed'))).toBe(true); expect(isRouteChunkError(new Error('Cannot read properties of undefined'))).toBe(false); }); it('isolates a page render failure and keeps a visible recovery action', () => { vi.spyOn(console, 'error').mockImplementation(() => undefined); const BrokenPage = () => { throw new Error('render exploded'); }; render(); expect(screen.getByRole('alert')).toHaveTextContent('当前模块暂时无法显示'); expect(screen.getByRole('button', { name: /刷新当前页面/ })).toBeInTheDocument(); expect(screen.getByRole('link', { name: /返回全局监控/ })).toHaveAttribute('href', '/monitor'); }); it('shows a deployment recovery page when an automatic chunk reload already ran', () => { vi.spyOn(console, 'error').mockImplementation(() => undefined); window.sessionStorage.setItem('vehicle-platform:route-chunk-reload', JSON.stringify({ routeKey: '/statistics', at: Date.now() })); const StaleChunkPage = () => { throw new TypeError('Failed to fetch dynamically imported module: /assets/Statistics-old.js'); }; render(); expect(screen.getByRole('alert')).toHaveTextContent('检测到页面版本更新'); expect(screen.getByText(/旧标签页仍引用上一版本资源/)).toBeInTheDocument(); }); });