perf(web): release monitor resources on unmount

This commit is contained in:
lingniu
2026-07-16 00:59:57 +08:00
parent c00f50551a
commit e80e1dcea9
9 changed files with 194 additions and 37 deletions

View File

@@ -13,6 +13,9 @@ const markerSetMap = vi.fn();
const markerSetPosition = vi.fn();
const setZoomAndCenter = vi.fn();
const panTo = vi.fn();
const mapOff = vi.fn();
const mapDestroy = vi.fn();
const massOff = vi.fn();
const getZoom = vi.fn(() => 5);
const getBounds = vi.fn((): ReturnType<NonNullable<AMapMap['getBounds']>> => ({}));
const mapHandlers = new Map<string, (event: unknown) => void>();
@@ -26,8 +29,9 @@ class TestMap {
}
add = vi.fn();
addControl = vi.fn();
destroy = vi.fn();
destroy = mapDestroy;
on = vi.fn((event: string, handler: (value: unknown) => void) => mapHandlers.set(event, handler));
off = mapOff;
getZoom = getZoom;
getBounds = getBounds;
setZoomAndCenter = setZoomAndCenter;
@@ -36,6 +40,7 @@ class TestMap {
class TestMassMarks {
on = vi.fn();
off = massOff;
setMap = vi.fn();
setData = setData;
setStyle = setStyle;
@@ -145,6 +150,9 @@ afterEach(() => {
markerSetPosition.mockReset();
setZoomAndCenter.mockReset();
panTo.mockReset();
mapOff.mockReset();
mapDestroy.mockReset();
massOff.mockReset();
getZoom.mockReset();
getZoom.mockReturnValue(5);
getBounds.mockReset();
@@ -187,6 +195,21 @@ test('renders data that arrives before the delayed AMap SDK is ready', async ()
expect(decodeURIComponent(clusterStyles[5].url)).not.toContain('10+');
});
test('detaches AMap listeners and destroys the map on unmount', async () => {
window.__LINGNIU_APP_CONFIG__ = { amapWebJsKey: 'amap-web-key' };
window.AMapLoader = { load: vi.fn(async () => amapMock()) };
const view = render(<FleetMap vehicles={[]} monitorMap={pointMap} onSelect={() => undefined} />);
await waitFor(() => expect(setData).toHaveBeenCalled());
view.unmount();
expect(massOff).toHaveBeenCalledWith('click', expect.any(Function));
expect(mapOff).toHaveBeenCalledWith('moveend', expect.any(Function));
expect(mapOff).toHaveBeenCalledWith('zoomend', expect.any(Function));
expect(mapOff).toHaveBeenCalledWith('dragstart', expect.any(Function));
expect(mapDestroy).toHaveBeenCalledTimes(1);
});
test('converts AMap GCJ-02 bounds back to WGS-84 before requesting monitor data', async () => {
const [west, south] = wgs84ToGcj02(113, 22);
const [east, north] = wgs84ToGcj02(114, 24);