hitTest.test.ts
4.05 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { describe, expect, it } from 'vitest';
import { hitTest, type HitLayers } from './hitTest';
import { useHeatmapStore } from '@/store/useHeatmapStore';
import type { BucketHit } from './ClusterLayer';
const PX = { x: 100, y: 100 };
const BUCKET: BucketHit = {
gx: 1,
gy: 2,
count: 3,
lng: 114,
lat: 22.6,
screenX: 100,
screenY: 100,
radius: 12,
};
const SPOT_UNIT = {
idx: 0,
label: '粤海街道',
count: 12,
roster: { source: '聚焦 · 粤海街道', where: {} },
};
const SPOT = {
kind: 'streets' as const,
source: '在地图上看前 6',
base: { from: 0, to: 0, districts: [], keyword: '' },
cellM: 100,
active: null,
units: [SPOT_UNIT],
note: '',
};
/** 四层都不命中;各测试按需覆盖某一层,避免每个用例重建整份 store 状态 */
function baseState() {
return useHeatmapStore.getState();
}
function baseLayers(): HitLayers {
return {
pin: { hitClose: () => false },
spot: { hitAt: () => -1 },
hotspot: { hitAt: () => -1 },
cluster: { hitAt: () => null },
};
}
function normalState() {
return { ...baseState(), analysisOn: false, spotlight: null, casePin: null };
}
describe('hitTest 四层命中优先级', () => {
it('卡片关闭叉最优先:命中时不再问下面三层', () => {
const calls: string[] = [];
const layers: HitLayers = {
pin: { hitClose: () => true },
spot: { hitAt: () => (calls.push('spot'), 0) },
hotspot: { hitAt: () => (calls.push('hotspot'), 0) },
cluster: { hitAt: () => (calls.push('cluster'), BUCKET) },
};
expect(hitTest(layers, normalState(), PX)).toEqual({ kind: 'pinClose' });
expect(calls).toEqual([]);
});
it('聚焦形状压过黑点与数字桶', () => {
const state = { ...normalState(), spotlight: SPOT };
const layers: HitLayers = {
...baseLayers(),
spot: { hitAt: () => 0 },
hotspot: { hitAt: () => 3 },
cluster: { hitAt: () => BUCKET },
};
expect(hitTest(layers, state, PX)).toEqual({ kind: 'spot', index: 0, unit: SPOT_UNIT });
});
it('黑点压过数字桶', () => {
const layers: HitLayers = {
...baseLayers(),
hotspot: { hitAt: () => 7 },
cluster: { hitAt: () => BUCKET },
};
expect(hitTest(layers, normalState(), PX)).toEqual({ kind: 'hotspot', index: 7 });
});
it('常态下命中聚合桶', () => {
const layers: HitLayers = { ...baseLayers(), cluster: { hitAt: () => BUCKET } };
expect(hitTest(layers, normalState(), PX)).toEqual({ kind: 'bucket', hit: BUCKET });
});
it('降噪 / 聚焦 / 定位期间数字层收起:连命中都不问,返回空白', () => {
for (const state of [
{ ...normalState(), analysisOn: true },
{ ...normalState(), spotlight: SPOT },
{ ...normalState(), casePin: { lng: 114, lat: 22.6, address: '南山区·测试路' } },
{ ...normalState(), layerVisible: { ...normalState().layerVisible, cluster: false } },
]) {
let asked = false;
const layers: HitLayers = {
...baseLayers(),
cluster: {
hitAt: () => {
asked = true;
return BUCKET;
},
},
};
expect(hitTest(layers, state, PX)).toBeNull();
expect(asked).toBe(false);
}
});
it('四层都没命中返回 null,调用方据此清选择', () => {
expect(hitTest(baseLayers(), normalState(), PX)).toBeNull();
});
it('命中坐标原样传给每一层', () => {
const seen: Array<[number, number]> = [];
const layers: HitLayers = {
pin: {
hitClose: (x, y) => {
seen.push([x, y]);
return false;
},
},
spot: {
hitAt: (x, y) => {
seen.push([x, y]);
return -1;
},
},
hotspot: {
hitAt: (x, y) => {
seen.push([x, y]);
return -1;
},
},
cluster: null,
};
hitTest(layers, normalState(), { x: 12, y: 34 });
// 常态没有聚焦时不问 spot 层,只有关闭叉与黑点两层被调用
expect(seen).toEqual([
[12, 34],
[12, 34],
]);
});
});