perf(tracks): defer empty map runtime

This commit is contained in:
lingniu
2026-07-16 06:35:34 +08:00
parent e6d26faad9
commit f1f0bab590
2 changed files with 69 additions and 3 deletions

View File

@@ -98,6 +98,68 @@ afterEach(() => {
mapInstances.length = 0;
});
test('defers the map runtime until valid track data exists and releases it when data is cleared', async () => {
window.__LINGNIU_APP_CONFIG__ = { amapWebJsKey: 'amap-web-key' };
const load = vi.fn(async () => amapMock());
window.AMapLoader = { load };
const view = render(<TrackMap
points={[]}
stops={[]}
activeIndex={0}
showStops={false}
follow
onSelectIndex={() => undefined}
onFollowChange={() => undefined}
/>);
expect(load).not.toHaveBeenCalled();
expect(mapInstances).toHaveLength(0);
expect(view.container.querySelector('.v2-map-state')).not.toBeInTheDocument();
view.rerender(<TrackMap
points={points}
stops={[]}
activeIndex={0}
showStops={false}
follow
onSelectIndex={() => undefined}
onFollowChange={() => undefined}
/>);
await waitFor(() => expect(mapInstances).toHaveLength(1));
expect(load).toHaveBeenCalledTimes(1);
view.rerender(<TrackMap
points={[]}
stops={[]}
activeIndex={0}
showStops={false}
follow
onSelectIndex={() => undefined}
onFollowChange={() => undefined}
/>);
await waitFor(() => expect(mapDestroy).toHaveBeenCalledTimes(1));
expect(mapInstances).toHaveLength(1);
});
test('does not initialize the map for rows without a valid coordinate', () => {
window.__LINGNIU_APP_CONFIG__ = { amapWebJsKey: 'amap-web-key' };
const load = vi.fn(async () => amapMock());
window.AMapLoader = { load };
render(<TrackMap
points={[{ ...points[0], longitude: 0, latitude: 0 }]}
stops={[]}
activeIndex={0}
showStops={false}
follow
onSelectIndex={() => undefined}
onFollowChange={() => undefined}
/>);
expect(load).not.toHaveBeenCalled();
expect(mapInstances).toHaveLength(0);
expect(document.body).toHaveTextContent('当前轨迹没有可展示的有效坐标');
});
test('uses the playback cadence for follow animation and advances the passed path', async () => {
window.__LINGNIU_APP_CONFIG__ = { amapWebJsKey: 'amap-web-key' };
window.AMapLoader = { load: vi.fn(async () => amapMock()) };