31 lines
3.3 KiB
JavaScript
31 lines
3.3 KiB
JavaScript
|
|
import {test} from 'node:test'
|
||
|
|
import assert from 'node:assert/strict'
|
||
|
|
import fs from 'node:fs'
|
||
|
|
import vm from 'node:vm'
|
||
|
|
import {visiblePeople,accuracyCircle,regionAt,trackCollection} from './frontend/src/maps/model.mjs'
|
||
|
|
|
||
|
|
function fixture(){
|
||
|
|
const timers=new Map(),elements=[],maps=[];let timer=0
|
||
|
|
const el=()=>{const names=new Set();return {classList:{add:n=>names.add(n),remove:n=>names.delete(n),toggle:(n,v)=>v?names.add(n):names.delete(n),contains:n=>names.has(n)},offsetWidth:40,setAttribute(){},append(...children){this.firstChild=children[0];this.lastChild=children.at(-1)}}}
|
||
|
|
class Marker{constructor({element}){this.element=element;elements.push(element)}setLngLat(pos){this.pos=pos;return this}addTo(){return this}remove(){}getElement(){return this.element}}
|
||
|
|
class MapView{constructor(){this.events={};this.moves=[];maps.push(this)}getCenter(){return {lng:117,lat:36}}getZoom(){return 10}getMaxZoom(){return 18}setMaxZoom(){}addControl(){}on(name,fn){this.events[name]=fn}once(){}isStyleLoaded(){return false}easeTo(v){this.moves.push(v)}fitBounds(){}setStyle(){}resize(){}remove(){this.removed=true}}
|
||
|
|
const source=fs.readFileSync('frontend/src/maps/controller.js','utf8').replace(/^import .*$/gm,'').replace('export function createFamilyMap','function createFamilyMap')+'\nthis.create=createFamilyMap'
|
||
|
|
const context=vm.createContext({Map,Set,Math,Array,String,Date,AbortController,LibreMap:MapView,Marker,NavigationControl:class{},ScaleControl:class{},AttributionControl:class{},workerUrl:'local',setWorkerUrl(){},setWorkerCount(){},layers:()=>[],namedFlavor:()=>({}),visiblePeople,accuracyCircle,regionAt,trackCollection,location:{origin:'http://local'},document:{createElement:el},ResizeObserver:class{observe(){}disconnect(){}},setTimeout:fn=>{timers.set(++timer,fn);return timer},clearTimeout:id=>timers.delete(id),setInterval:()=>0,clearInterval(){},fetch:async()=>({ok:true,json:async()=>({regions:[{id:'cn-37',name:'山东',bounds:[114,34,123,39],maxZoom:18}]})})
|
||
|
|
})
|
||
|
|
vm.runInContext(source,context)
|
||
|
|
return {controller:context.create({}),maps,elements,timers}
|
||
|
|
}
|
||
|
|
|
||
|
|
test('click focus pulses only selected fresh point and preserves the map marker transform',async()=>{
|
||
|
|
const f=fixture();await f.controller.ready
|
||
|
|
f.controller.setItems([{id:'a',name:'甲',position:{longitude:117,latitude:36,accuracy:20,at:Date.now()/1000}},{id:'b',name:'乙',position:{longitude:117.1,latitude:36.1,accuracy:20,at:Date.now()/1000}}])
|
||
|
|
assert.equal(f.elements.some(el=>el.classList.contains('pulsing')),false)
|
||
|
|
f.controller.focus('b',true);assert.equal(f.elements[1].classList.contains('pulsing'),true);assert.equal(f.elements[0].classList.contains('pulsing'),false)
|
||
|
|
assert.equal(f.maps[0].moves.at(-1).center[0],117.1)
|
||
|
|
f.controller.focus('a',true);assert.equal(f.elements[1].classList.contains('pulsing'),false);assert.equal(f.elements[0].classList.contains('pulsing'),true)
|
||
|
|
for(const fn of f.timers.values())fn();assert.equal(f.elements[0].classList.contains('pulsing'),false)
|
||
|
|
const before=f.maps[0].moves.length;f.controller.focus('missing',true);assert.equal(f.maps[0].moves.length,before)
|
||
|
|
f.controller.destroy();assert.equal(f.maps[0].removed,true)
|
||
|
|
const css=fs.readFileSync('frontend/src/maps/map.css','utf8');assert.match(css,/\.family-map-pin\.pulsing>span/);assert.match(css,/prefers-reduced-motion:reduce/)
|
||
|
|
})
|