gisMapClustering.test.mjs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { mergeMapPoints } from '../src/utils/gisMapClustering.js'
  4. function projectPoint({ lng, lat }) {
  5. return { x: lng * 100, y: lat * 100 }
  6. }
  7. test('mergeMapPoints keeps distant points as individual markers', () => {
  8. const points = [
  9. { id: 'a', lng: 110.3, lat: 28.4 },
  10. { id: 'b', lng: 111, lat: 29 }
  11. ]
  12. const merged = mergeMapPoints(points, projectPoint, 48)
  13. assert.equal(merged.length, 2)
  14. assert.deepEqual(merged.map(item => item.type), ['point', 'point'])
  15. assert.deepEqual(merged.map(item => item.point.id), ['a', 'b'])
  16. })
  17. test('mergeMapPoints combines nearby points into one countable cluster', () => {
  18. const points = [
  19. { id: 'a', lng: 110.3, lat: 28.4, iconUrl: 'green.png' },
  20. { id: 'b', lng: 110.3005, lat: 28.4004, iconUrl: 'red.png' }
  21. ]
  22. const merged = mergeMapPoints(points, projectPoint, 48)
  23. assert.equal(merged.length, 1)
  24. assert.equal(merged[0].type, 'cluster')
  25. assert.equal(merged[0].count, 2)
  26. assert.deepEqual(merged[0].points.map(point => point.id), ['a', 'b'])
  27. })
  28. test('mergeMapPoints keeps the average render position for a cluster', () => {
  29. const merged = mergeMapPoints([
  30. { id: 'a', lng: 110.3, lat: 28.4 },
  31. { id: 'b', lng: 110.301, lat: 28.402 }
  32. ], projectPoint, 48)
  33. assert.equal(merged[0].position.lng, 110.3005)
  34. assert.equal(merged[0].position.lat, 28.401)
  35. })
  36. test('mergeMapPoints ignores invalid coordinates', () => {
  37. const merged = mergeMapPoints([
  38. { id: 'valid', lng: 110.3, lat: 28.4 },
  39. { id: 'bad-lng', lng: Number.NaN, lat: 28.4 },
  40. { id: 'bad-lat', lng: 110.3, lat: Number.NaN }
  41. ], projectPoint, 48)
  42. assert.equal(merged.length, 1)
  43. assert.equal(merged[0].type, 'point')
  44. assert.equal(merged[0].point.id, 'valid')
  45. })