perf(web): reschedule route preloads after navigation

This commit is contained in:
lingniu
2026-07-16 07:09:55 +08:00
parent fa886abff9
commit 970ef5d7c0
2 changed files with 45 additions and 2 deletions

View File

@@ -0,0 +1,43 @@
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { afterEach, expect, test, vi } from 'vitest';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { ROUTER_FUTURE } from '../routing/routerConfig';
const routing = vi.hoisted(() => ({
preloadRoute: vi.fn(async () => undefined),
scheduleIdleRoutePreloads: vi.fn(() => vi.fn()),
shouldPreloadRouteOnIntent: vi.fn(() => true)
}));
vi.mock('../routing/routeModules', () => routing);
vi.mock('../auth/AuthGate', () => ({
usePlatformSession: () => ({ session: { name: 'test-user', role: 'viewer' }, logout: vi.fn() })
}));
import { AppShell } from './AppShell';
afterEach(() => {
cleanup();
vi.clearAllMocks();
});
test('reschedules likely route preloads when the active module changes', async () => {
const firstCleanup = vi.fn();
const secondCleanup = vi.fn();
routing.scheduleIdleRoutePreloads
.mockReturnValueOnce(firstCleanup)
.mockReturnValueOnce(secondCleanup);
render(<MemoryRouter future={ROUTER_FUTURE} initialEntries={['/monitor']}>
<Routes><Route element={<AppShell />}><Route path="*" element={<span></span>} /></Route></Routes>
</MemoryRouter>);
expect(routing.scheduleIdleRoutePreloads).toHaveBeenCalledWith({ activePathname: '/monitor' });
fireEvent.click(screen.getByRole('link', { name: '历史数据' }));
await waitFor(() => expect(routing.scheduleIdleRoutePreloads).toHaveBeenLastCalledWith({ activePathname: '/history' }));
expect(firstCleanup).toHaveBeenCalledTimes(1);
cleanup();
expect(secondCleanup).toHaveBeenCalledTimes(1);
});