camera.test.ts
2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { describe, expect, it, vi } from 'vitest';
import { BaiduBasemap } from './BaiduBasemap';
import type { BaiduGLMap, BaiduGLNamespace, BaiduGLPoint } from './load';
/**
* 取景适配的守卫:百度 GL 没有高德的 setZoomAndCenter,只有 centerAndZoom。
* 曾经门面直接透传 setZoomAndCenter,用户点一次统计取景就抛
* "this.raw.setZoomAndCenter is not a function",整页白屏(2026-11-24 现场)。
* 这条测试用一份"只有 centerAndZoom"的假地图,保证门面走的是对的那个 API。
*/
function fakeMap(): { map: BaiduGLMap; centerAndZoom: ReturnType<typeof vi.fn> } {
const centerAndZoom = vi.fn();
const point = (lng: number, lat: number): BaiduGLPoint =>
({ lng, lat }) as unknown as BaiduGLPoint;
const map = {
centerAndZoom,
getSize: () => ({ width: 800, height: 600 }),
getCenter: () => point(114.06, 22.62),
getZoom: () => 11,
getBounds: () => ({
getSouthWest: () => point(114, 22.5),
getNorthEast: () => point(114.2, 22.8),
}),
setZoom: vi.fn(),
setMapStyle: vi.fn(),
getContainer: () => document.createElement('div'),
pointToOverlayPixel: () => ({ x: 0, y: 0 }),
overlayPixelToPoint: () => point(114.06, 22.62),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
addLayer: vi.fn(),
removeLayer: vi.fn(),
destroy: vi.fn(),
} as unknown as BaiduGLMap;
return { map, centerAndZoom };
}
const B = {
Point: class {
constructor(
public lng: number,
public lat: number,
) {}
},
} as unknown as BaiduGLNamespace;
describe('百度取景适配', () => {
it('setZoomAndCenter 门面调用百度真实的 centerAndZoom,不碰不存在的同名方法', () => {
const { map, centerAndZoom } = fakeMap();
const basemap = new BaiduBasemap(B, map);
expect(() => basemap.setZoomAndCenter(11.24, [114.07275, 22.63436], false, 400)).not.toThrow();
expect(centerAndZoom).toHaveBeenCalledTimes(1);
const [pt, zoom, opts] = centerAndZoom.mock.calls[0];
expect((pt as BaiduGLPoint).lng).toBeCloseTo(114.07275, 6);
expect((pt as BaiduGLPoint).lat).toBeCloseTo(22.63436, 6);
expect(zoom).toBeCloseTo(11.24, 6);
// immediately=false → 不强制关动画
expect(opts).toMatchObject({ noAnimation: false });
});
it('immediately=true 时关动画', () => {
const { map, centerAndZoom } = fakeMap();
const basemap = new BaiduBasemap(B, map);
basemap.setZoomAndCenter(13, [114.06, 22.62], true);
expect(centerAndZoom.mock.calls[0][2]).toMatchObject({ noAnimation: true });
});
});