fix(web): harden route transitions and monitor memory

This commit is contained in:
lingniu
2026-07-15 23:54:21 +08:00
parent 3fabcf181a
commit c29ccdf2da
17 changed files with 332 additions and 36 deletions

View File

@@ -0,0 +1,35 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { cleanup, renderHook, waitFor } from '@testing-library/react';
import { afterEach, expect, test, vi } from 'vitest';
import type { ReactNode } from 'react';
import { api } from '../../api/client';
import { useMonitorData } from './useMonitorData';
afterEach(() => {
cleanup();
vi.restoreAllMocks();
});
test('rapid viewport changes retain at most one monitor map payload', async () => {
vi.spyOn(api, 'monitorSummary').mockResolvedValue({ totalVehicles: 0, onlineVehicles: 0, offlineVehicles: 0, drivingVehicles: 0, idleVehicles: 0, unknownVehicles: 0, alertVehicles: 0, activeToday: 0, frameToday: 0, alertDataAvailable: false, truncated: false, asOf: '' });
const monitorMap = vi.spyOn(api, 'monitorMap').mockResolvedValue({ mode: 'points', zoom: 13, total: 0, truncated: false, points: [], clusters: [], asOf: '' });
const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
const wrapper = ({ children }: { children: ReactNode }) => <QueryClientProvider client={client}>{children}</QueryClientProvider>;
const filters = { keyword: '', protocol: '', status: '' };
const view = renderHook(({ bounds }) => useMonitorData(filters, { zoom: 13, bounds }, '', true, false), {
wrapper,
initialProps: { bounds: '113.0000,22.0000,114.0000,23.0000' }
});
await waitFor(() => expect(monitorMap).toHaveBeenCalledTimes(1));
for (let index = 1; index <= 8; index += 1) {
view.rerender({ bounds: `${113 + index / 1000},22,${114 + index / 1000},23` });
await waitFor(() => expect(monitorMap.mock.calls.length).toBeGreaterThanOrEqual(index + 1));
}
await waitFor(() => {
const cachedMaps = client.getQueryCache().findAll({ queryKey: ['monitor', 'map'] });
expect(cachedMaps).toHaveLength(1);
});
view.unmount();
client.clear();
});