fix(web): retry rejected route modules
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
|
||||
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { lazy, useEffect, useState } from 'react';
|
||||
import { MemoryRouter, useSearchParams } from 'react-router-dom';
|
||||
@@ -46,6 +46,23 @@ describe('route recovery boundary', () => {
|
||||
render(<MemoryRouter future={ROUTER_FUTURE} initialEntries={['/statistics']}><RoutePage page={StaleChunkPage} label="里程查询" /></MemoryRouter>);
|
||||
expect(screen.getByRole('alert')).toHaveTextContent('检测到页面版本更新');
|
||||
expect(screen.getByText(/旧标签页仍引用上一版本资源/)).toBeInTheDocument();
|
||||
expect(screen.getByRole('button', { name: /重试加载模块/ })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('recreates a rejected lazy route when the operator retries without a full reload', async () => {
|
||||
vi.spyOn(console, 'error').mockImplementation(() => undefined);
|
||||
window.sessionStorage.setItem('vehicle-platform:route-chunk-reload', JSON.stringify({ routeKey: '/history', at: Date.now() }));
|
||||
const failedPage = lazy(() => Promise.reject(new TypeError('Failed to fetch dynamically imported module: /assets/History-old.js')));
|
||||
const recreatePage = vi.fn(() => lazy(() => Promise.resolve({ default: () => <strong>历史模块已恢复</strong> })));
|
||||
|
||||
render(<MemoryRouter future={ROUTER_FUTURE} initialEntries={['/history']}><RoutePage page={failedPage} recreatePage={recreatePage} label="历史数据" /></MemoryRouter>);
|
||||
|
||||
expect(await screen.findByRole('alert')).toHaveTextContent('检测到页面版本更新');
|
||||
fireEvent.click(screen.getByRole('button', { name: /重试加载模块/ }));
|
||||
|
||||
await waitFor(() => expect(screen.getByText('历史模块已恢复')).toBeInTheDocument());
|
||||
expect(recreatePage).toHaveBeenCalledTimes(1);
|
||||
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('explains a bounded route timeout instead of leaving the loading skeleton forever', () => {
|
||||
|
||||
Reference in New Issue
Block a user