@m3ui-vue/m3ui-vue 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/API.md +636 -20
- package/dist/MMenuItem-DFyXflk9.js +707 -0
- package/dist/MMenuItem-DFyXflk9.js.map +1 -0
- package/dist/MSlider-CdNIH7jc.js +1376 -0
- package/dist/MSlider-CdNIH7jc.js.map +1 -0
- package/dist/MTextField-B4X92vH_.js +671 -0
- package/dist/MTextField-B4X92vH_.js.map +1 -0
- package/dist/api.json +1 -1
- package/dist/chart.js +1 -1
- package/dist/chart.js.map +1 -1
- package/dist/code-editor.js +1 -1
- package/dist/components/MIconButton.vue.d.ts +1 -1
- package/dist/components/MMap.vue.d.ts +74 -0
- package/dist/components/MMapBaseLayers.vue.d.ts +15 -0
- package/dist/components/MMapControl.vue.d.ts +23 -0
- package/dist/components/MMapFeatureInfo.vue.d.ts +25 -0
- package/dist/components/MMapFilter.vue.d.ts +19 -0
- package/dist/components/MMapGeoJson.vue.d.ts +34 -0
- package/dist/components/MMapHover.vue.d.ts +17 -0
- package/dist/components/MMapIdentify.vue.d.ts +37 -0
- package/dist/components/MMapLayers.vue.d.ts +31 -0
- package/dist/components/MMapMarker.vue.d.ts +42 -0
- package/dist/components/MMapMeasure.vue.d.ts +43 -0
- package/dist/components/MMapPopup.vue.d.ts +43 -0
- package/dist/components/MMapSearch.vue.d.ts +28 -0
- package/dist/components/MShader.vue.d.ts +9 -3
- package/dist/components/_MMapLayerItem.vue.d.ts +25 -0
- package/dist/composables/_hydration.d.ts +4 -0
- package/dist/composables/_mapGeometry.d.ts +13 -0
- package/dist/composables/_ssrState.d.ts +30 -0
- package/dist/composables/_storageKeys.d.ts +3 -0
- package/dist/composables/_useThemeTokens.d.ts +7 -0
- package/dist/composables/mapAdapters.d.ts +111 -0
- package/dist/composables/mapSearch.d.ts +31 -0
- package/dist/composables/themeInitScript.d.ts +22 -0
- package/dist/composables/useColorPalette.d.ts +176 -6
- package/dist/composables/useLocale.d.ts +30 -0
- package/dist/composables/useMap.d.ts +47 -0
- package/dist/composables/useMapFilter.d.ts +10 -0
- package/dist/composables/useMapLayers.d.ts +30 -0
- package/dist/composables/useTheme.d.ts +2 -0
- package/dist/index.d.ts +4 -1
- package/dist/locales/index.js +217 -7
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +6742 -7961
- package/dist/m3ui.js.map +1 -1
- package/dist/map.d.ts +24 -0
- package/dist/map.js +1931 -0
- package/dist/map.js.map +1 -0
- package/dist/plugin.d.ts +25 -2
- package/dist/rich-text-editor.js +27 -26
- package/dist/rich-text-editor.js.map +1 -1
- package/dist/styles.css +1 -1
- package/dist/{useLocale-CuvWRZ1h.js → useLocale-b5-hzsL3.js} +32 -2
- package/dist/useLocale-b5-hzsL3.js.map +1 -0
- package/package.json +11 -2
- package/src/__tests__/MFab.test.ts +52 -6
- package/src/__tests__/MMap.test.ts +292 -0
- package/src/__tests__/MMapLayers.test.ts +257 -0
- package/src/__tests__/MMapTools.test.ts +233 -0
- package/src/__tests__/MShader.test.ts +16 -0
- package/src/__tests__/_fakeMaplibre.ts +158 -0
- package/src/__tests__/hydration.test.ts +119 -0
- package/src/__tests__/ssr.test.ts +171 -0
- package/src/__tests__/ssrState.test.ts +155 -0
- package/src/__tests__/themeInitScript.test.ts +122 -0
- package/src/components/MChart.vue +3 -2
- package/src/components/MFab.vue +29 -7
- package/src/components/MIconButton.vue +1 -1
- package/src/components/MLightbox.vue +10 -1
- package/src/components/MMap.vue +522 -0
- package/src/components/MMapBaseLayers.vue +67 -0
- package/src/components/MMapControl.vue +61 -0
- package/src/components/MMapFeatureInfo.vue +41 -0
- package/src/components/MMapFilter.vue +80 -0
- package/src/components/MMapGeoJson.vue +136 -0
- package/src/components/MMapHover.vue +131 -0
- package/src/components/MMapIdentify.vue +103 -0
- package/src/components/MMapLayers.vue +111 -0
- package/src/components/MMapMarker.vue +102 -0
- package/src/components/MMapMeasure.vue +452 -0
- package/src/components/MMapPopup.vue +99 -0
- package/src/components/MMapSearch.vue +162 -0
- package/src/components/MSearchBar.vue +4 -1
- package/src/components/MShader.vue +11 -3
- package/src/components/_MMapLayerItem.vue +77 -0
- package/src/composables/_hydration.ts +10 -0
- package/src/composables/_mapGeometry.ts +75 -0
- package/src/composables/_ssrState.ts +80 -0
- package/src/composables/_storageKeys.ts +6 -0
- package/src/composables/_useThemeTokens.ts +42 -0
- package/src/composables/mapAdapters.ts +208 -0
- package/src/composables/mapSearch.ts +56 -0
- package/src/composables/themeInitScript.ts +51 -0
- package/src/composables/useColorPalette.ts +32 -14
- package/src/composables/useLocale.ts +62 -0
- package/src/composables/useMap.ts +98 -0
- package/src/composables/useMapFilter.ts +45 -0
- package/src/composables/useMapLayers.ts +124 -0
- package/src/composables/useTheme.ts +18 -7
- package/src/composables/useWindowSizeClass.ts +23 -4
- package/src/index.ts +4 -1
- package/src/locales/de.ts +31 -1
- package/src/locales/es.ts +31 -1
- package/src/locales/fr.ts +31 -1
- package/src/locales/ja.ts +31 -1
- package/src/locales/ko.ts +31 -1
- package/src/locales/pt.ts +31 -1
- package/src/locales/zh.ts +31 -1
- package/src/map.ts +32 -0
- package/src/plugin.ts +77 -13
- package/dist/MTextField-BPbFeI9M.js +0 -1368
- package/dist/MTextField-BPbFeI9M.js.map +0 -1
- package/dist/useLocale-CuvWRZ1h.js.map +0 -1
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { flushPromises, mount } from '@vue/test-utils'
|
|
3
|
+
import { defineComponent, h, nextTick } from 'vue'
|
|
4
|
+
import type { StyleSpecification } from 'maplibre-gl'
|
|
5
|
+
import { created, type FakeMap } from './_fakeMaplibre'
|
|
6
|
+
|
|
7
|
+
vi.mock('maplibre-gl', async () => (await import('./_fakeMaplibre')).fakeModule)
|
|
8
|
+
|
|
9
|
+
const { MMap, MMapLayers, MMapBaseLayers, MMapIdentify, MMapHover, useMapLayers, orvianAdapter, genericAdapter, formatFieldValue, identifyFeatures } = await import('../map')
|
|
10
|
+
|
|
11
|
+
// A style the way the Orvian viewers write it (served by mvt-rs)
|
|
12
|
+
const STYLE_URL = 'https://tiles.test/services/styles/public:city'
|
|
13
|
+
function orvianStyle() {
|
|
14
|
+
return {
|
|
15
|
+
version: 8,
|
|
16
|
+
name: 'City',
|
|
17
|
+
metadata: { groups: [{ name: 'land', alias: 'Land use' }, { name: 'net', alias: 'Networks' }] },
|
|
18
|
+
sources: {},
|
|
19
|
+
layers: [
|
|
20
|
+
{ id: 'bg', type: 'background' },
|
|
21
|
+
{ id: 'osm', type: 'raster', metadata: { isbaselayer: true, image: 'osm.png', alias: 'OpenStreetMap' } },
|
|
22
|
+
{ id: 'sat', type: 'raster', layout: { visibility: 'none' }, metadata: { isbaselayer: true, image: 'https://img.test/sat.png', alias: 'Satellite' } },
|
|
23
|
+
{
|
|
24
|
+
id: 'parcels',
|
|
25
|
+
type: 'fill',
|
|
26
|
+
paint: { 'fill-opacity': 0.5 },
|
|
27
|
+
metadata: {
|
|
28
|
+
alias: 'Parcels',
|
|
29
|
+
group: 'land',
|
|
30
|
+
description: 'Cadastral parcels',
|
|
31
|
+
identify: true,
|
|
32
|
+
childs: ['parcels-line'],
|
|
33
|
+
fields: { area: { alias: 'Area', suffix: 'm²', mask: '#,###.00' }, secret: { show: false }, exclude: ['gid'] },
|
|
34
|
+
hover: { enable: true, fields: [{ owner: { alias: 'Owner' } }, { area: { alias: 'Area', mask: '#,###' } }] },
|
|
35
|
+
download: 'https://tiles.test/parcels.zip',
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
{ id: 'parcels-line', type: 'line', metadata: { show_in_layerlist: false, group: 'land' } },
|
|
39
|
+
{ id: 'zoning', type: 'fill', metadata: { alias: 'Zoning', group: 'land', legend: { show: false } } },
|
|
40
|
+
{ id: 'roads', type: 'line', metadata: { alias: 'Roads', group: 'net', legend: { wmslegend: 'https://legend.test/roads.png' } } },
|
|
41
|
+
{ id: 'poi', type: 'circle', metadata: { alias: 'Points of interest' } },
|
|
42
|
+
],
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
beforeEach(() => {
|
|
47
|
+
created.maps = []
|
|
48
|
+
created.markers = []
|
|
49
|
+
created.popups = []
|
|
50
|
+
document.documentElement.lang = 'en'
|
|
51
|
+
})
|
|
52
|
+
afterEach(() => { document.body.innerHTML = '' })
|
|
53
|
+
|
|
54
|
+
describe('orvianAdapter', () => {
|
|
55
|
+
const model = orvianAdapter()(orvianStyle() as unknown as StyleSpecification, { styleUrl: STYLE_URL })
|
|
56
|
+
const layer = (id: string) => model.layers.find((l) => l.id === id)!
|
|
57
|
+
|
|
58
|
+
it('reads names, groups, base layers and the list', () => {
|
|
59
|
+
expect(model.title).toBe('City')
|
|
60
|
+
expect(model.groups).toEqual([{ name: 'land', title: 'Land use' }, { name: 'net', title: 'Networks' }])
|
|
61
|
+
expect(model.layers.map((l) => l.id)).not.toContain('bg')
|
|
62
|
+
expect(layer('osm')).toMatchObject({ title: 'OpenStreetMap', listed: false, base: { image: 'osm.png' } })
|
|
63
|
+
expect(layer('parcels')).toMatchObject({ title: 'Parcels', group: 'land', description: 'Cadastral parcels', listed: true, children: ['parcels-line'], download: 'https://tiles.test/parcels.zip' })
|
|
64
|
+
expect(layer('parcels-line').listed).toBe(false)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('reads identify formats and exclusions, and flattens hover fields', () => {
|
|
68
|
+
expect(layer('parcels').identify).toEqual({
|
|
69
|
+
format: { area: { label: 'Area', prefix: undefined, suffix: 'm²', mask: '#,###.00', hidden: false }, secret: { label: undefined, prefix: undefined, suffix: undefined, mask: undefined, hidden: true } },
|
|
70
|
+
exclude: ['gid'],
|
|
71
|
+
})
|
|
72
|
+
expect(layer('roads').identify).toBe(false)
|
|
73
|
+
expect(layer('parcels').hover).toEqual({ fields: [{ key: 'owner', label: 'Owner', prefix: undefined, suffix: undefined, mask: undefined }, { key: 'area', label: 'Area', prefix: undefined, suffix: undefined, mask: '#,###' }] })
|
|
74
|
+
expect(layer('roads').hover).toBe(false)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('takes legends from mvt-rs, a wmslegend, or none', () => {
|
|
78
|
+
expect(layer('parcels').legend).toBe('https://tiles.test/services/legends/public:city?has_label=false&include_raster=true&layer_id=parcels')
|
|
79
|
+
expect(layer('zoning').legend).toBeUndefined()
|
|
80
|
+
expect(layer('roads').legend).toBe('https://legend.test/roads.png')
|
|
81
|
+
expect(layer('osm').legend).toBeUndefined()
|
|
82
|
+
// Not an mvt-rs style: no legend service
|
|
83
|
+
const other = orvianAdapter()(orvianStyle() as unknown as StyleSpecification, { styleUrl: 'https://x.test/style.json' })
|
|
84
|
+
expect(other.layers.find((l) => l.id === 'parcels')!.legend).toBeUndefined()
|
|
85
|
+
})
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
describe('genericAdapter', () => {
|
|
89
|
+
it('lists every layer under its id, identifying vector ones', () => {
|
|
90
|
+
const model = genericAdapter(orvianStyle() as unknown as StyleSpecification, {})
|
|
91
|
+
expect(model.layers.find((l) => l.id === 'roads')).toMatchObject({ title: 'roads', listed: true, identify: { format: {}, exclude: [] }, hover: false })
|
|
92
|
+
expect(model.layers.find((l) => l.id === 'osm')!.identify).toBe(false)
|
|
93
|
+
})
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
describe('formatFieldValue', () => {
|
|
97
|
+
it('applies the mask in the page\'s language, then prefix and suffix', () => {
|
|
98
|
+
expect(formatFieldValue(1234.5, { mask: '#,###.00', suffix: 'm²' })).toBe('1,234.50 m²')
|
|
99
|
+
document.documentElement.lang = 'es'
|
|
100
|
+
expect(formatFieldValue('1234.5', { mask: '#,###.00' })).toBe('1.234,50')
|
|
101
|
+
expect(formatFieldValue(1234567, { mask: '#,###' })).toBe('1.234.567')
|
|
102
|
+
expect(formatFieldValue(3, { prefix: '$' })).toBe('$ 3')
|
|
103
|
+
expect(formatFieldValue('', {})).toBe('')
|
|
104
|
+
expect(formatFieldValue(null)).toBe('')
|
|
105
|
+
})
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
/** An MMap with the Orvian style loaded, and a probe exposing useMapLayers(). */
|
|
109
|
+
async function mountWithLayers(slot?: () => unknown) {
|
|
110
|
+
let api!: ReturnType<typeof useMapLayers>
|
|
111
|
+
const Probe = defineComponent({ setup() { api = useMapLayers(); return () => null } })
|
|
112
|
+
const wrapper = mount(MMap, {
|
|
113
|
+
props: { mapStyle: STYLE_URL, adapter: orvianAdapter() },
|
|
114
|
+
slots: { default: () => [h(Probe), slot?.()] },
|
|
115
|
+
attachTo: document.body,
|
|
116
|
+
})
|
|
117
|
+
await flushPromises()
|
|
118
|
+
const map = created.maps.at(-1)! as FakeMap
|
|
119
|
+
map.style = orvianStyle() as never
|
|
120
|
+
map.load()
|
|
121
|
+
await nextTick()
|
|
122
|
+
await nextTick()
|
|
123
|
+
return { wrapper, map, api: () => api }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
describe('useMapLayers', () => {
|
|
127
|
+
it('reads visibility and opacity, toggles layers with their children, and fades them by type', async () => {
|
|
128
|
+
const { map, api } = await mountWithLayers()
|
|
129
|
+
expect(api().isVisible('sat')).toBe(false)
|
|
130
|
+
expect(api().opacityOf('parcels')).toBe(0.5)
|
|
131
|
+
api().setVisible('parcels', false)
|
|
132
|
+
expect(map.getLayoutProperty('parcels', 'visibility')).toBe('none')
|
|
133
|
+
expect(map.getLayoutProperty('parcels-line', 'visibility')).toBe('none')
|
|
134
|
+
api().setOpacity('poi', 0.3)
|
|
135
|
+
expect(map.getPaintProperty('poi', 'circle-opacity')).toBe(0.3)
|
|
136
|
+
expect(map.getPaintProperty('poi', 'circle-stroke-opacity')).toBe(0.3)
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('shows and hides groups, with a partial state', async () => {
|
|
140
|
+
const { api } = await mountWithLayers()
|
|
141
|
+
expect(api().layersOf('land').map((l) => l.id)).toEqual(['parcels', 'zoning'])
|
|
142
|
+
expect(api().layersOf(null).map((l) => l.id)).toEqual(['poi'])
|
|
143
|
+
expect(api().groupState('land')).toBe('visible')
|
|
144
|
+
api().setVisible('zoning', false)
|
|
145
|
+
expect(api().groupState('land')).toBe('partial')
|
|
146
|
+
api().setGroupVisible('land', false)
|
|
147
|
+
expect(api().groupState('land')).toBe('hidden')
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
it('shows one base layer at a time, or none', async () => {
|
|
151
|
+
const { map, api } = await mountWithLayers()
|
|
152
|
+
expect(api().baseLayer.value).toBe('osm')
|
|
153
|
+
api().setBaseLayer('sat')
|
|
154
|
+
expect(api().baseLayer.value).toBe('sat')
|
|
155
|
+
expect(map.getLayoutProperty('osm', 'visibility')).toBe('none')
|
|
156
|
+
api().setBaseLayer(null)
|
|
157
|
+
expect(api().baseLayer.value).toBeNull()
|
|
158
|
+
})
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
describe('MMapLayers', () => {
|
|
162
|
+
it('lists the groups and their layers, filters by search, and toggles from the list', async () => {
|
|
163
|
+
const { wrapper, map } = await mountWithLayers(() => h(MMapLayers))
|
|
164
|
+
const text = wrapper.text()
|
|
165
|
+
expect(text).toContain('Land use')
|
|
166
|
+
expect(text).toContain('Networks')
|
|
167
|
+
expect(text).toContain('Parcels')
|
|
168
|
+
expect(text).toContain('Points of interest')
|
|
169
|
+
expect(text).not.toContain('parcels-line')
|
|
170
|
+
expect(text).toContain('2/2')
|
|
171
|
+
await wrapper.find('button[aria-label="Hide layer"]').trigger('click')
|
|
172
|
+
expect(map.getLayoutProperty('parcels', 'visibility')).toBe('none')
|
|
173
|
+
await wrapper.find('input').setValue('road')
|
|
174
|
+
expect(wrapper.text()).toContain('Roads')
|
|
175
|
+
expect(wrapper.text()).not.toContain('Parcels')
|
|
176
|
+
})
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
describe('MMapBaseLayers', () => {
|
|
180
|
+
it('switches base layers from their thumbnails, prefixing relative images', async () => {
|
|
181
|
+
const { wrapper, map } = await mountWithLayers(() => h(MMapBaseLayers, { imageBase: '/images/' }))
|
|
182
|
+
const imgs = wrapper.findAll('img').map((i) => i.attributes('src'))
|
|
183
|
+
expect(imgs).toEqual(['/images/osm.png', 'https://img.test/sat.png'])
|
|
184
|
+
await wrapper.find('button[aria-label="Satellite"]').trigger('click')
|
|
185
|
+
expect(map.getLayoutProperty('sat', 'visibility')).toBe('visible')
|
|
186
|
+
expect(map.getLayoutProperty('osm', 'visibility')).toBe('none')
|
|
187
|
+
await wrapper.find('button[aria-label="No base layer"]').trigger('click')
|
|
188
|
+
expect(map.getLayoutProperty('sat', 'visibility')).toBe('none')
|
|
189
|
+
})
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
describe('identify', () => {
|
|
193
|
+
it('lists the identify layers\' features under a point, formatted, minus hidden and excluded properties', async () => {
|
|
194
|
+
const { map } = await mountWithLayers()
|
|
195
|
+
map.rendered = [
|
|
196
|
+
{ layer: { id: 'parcels' }, properties: { area: 1500, owner: 'Ana', secret: 'x', gid: 7, layerId: 'parcels' } },
|
|
197
|
+
{ layer: { id: 'roads' }, properties: { name: 'Main St' } },
|
|
198
|
+
]
|
|
199
|
+
const model = orvianAdapter()(orvianStyle() as unknown as StyleSpecification, { styleUrl: STYLE_URL })
|
|
200
|
+
const results = identifyFeatures(map as never, [0, 0], model)
|
|
201
|
+
expect(results).toHaveLength(1)
|
|
202
|
+
expect(results[0]!.layer.title).toBe('Parcels')
|
|
203
|
+
expect(results[0]!.fields).toEqual([
|
|
204
|
+
{ key: 'area', label: 'Area', value: '1,500.00 m²' },
|
|
205
|
+
{ key: 'owner', label: 'Owner', value: 'Ana' },
|
|
206
|
+
])
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
it('MMapIdentify emits the results of a click and opens them in a popup', async () => {
|
|
210
|
+
const onIdentify = vi.fn()
|
|
211
|
+
const { map } = await mountWithLayers(() => h(MMapIdentify, { onIdentify }))
|
|
212
|
+
map.rendered = [{ layer: { id: 'parcels' }, properties: { owner: 'Ana' } }]
|
|
213
|
+
map.fire('click', { target: map, point: { x: 1, y: 2 }, lngLat: { lng: 3, lat: 4 } })
|
|
214
|
+
await nextTick()
|
|
215
|
+
await nextTick()
|
|
216
|
+
expect(onIdentify).toHaveBeenCalledWith(expect.arrayContaining([expect.objectContaining({ fields: [{ key: 'owner', label: 'Owner', value: 'Ana' }] })]), [3, 4])
|
|
217
|
+
const popup = created.popups.at(-1)!
|
|
218
|
+
expect(popup.isOpen()).toBe(true)
|
|
219
|
+
expect(popup.content?.textContent).toContain('Ana')
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
it('MMapIdentify ignores clicks while disabled', async () => {
|
|
223
|
+
const onIdentify = vi.fn()
|
|
224
|
+
const { map } = await mountWithLayers(() => h(MMapIdentify, { enabled: false, onIdentify }))
|
|
225
|
+
map.fire('click', { target: map, point: { x: 1, y: 2 }, lngLat: { lng: 3, lat: 4 } })
|
|
226
|
+
expect(onIdentify).not.toHaveBeenCalled()
|
|
227
|
+
})
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
describe('MMapHover', () => {
|
|
231
|
+
it('adds its on/off control and, while on, shows the hover fields of the feature under the pointer', async () => {
|
|
232
|
+
const Host = defineComponent({
|
|
233
|
+
props: { on: Boolean },
|
|
234
|
+
setup: (p) => () => h(MMap, { mapStyle: STYLE_URL, adapter: orvianAdapter() }, () => h(MMapHover, { modelValue: p.on })),
|
|
235
|
+
})
|
|
236
|
+
const wrapper = mount(Host, { props: { on: false }, attachTo: document.body })
|
|
237
|
+
await flushPromises()
|
|
238
|
+
const map = created.maps.at(-1)!
|
|
239
|
+
map.style = orvianStyle() as never
|
|
240
|
+
map.load()
|
|
241
|
+
await nextTick()
|
|
242
|
+
await nextTick()
|
|
243
|
+
expect(map.controls.some((c) => c.position === 'top-right')).toBe(true)
|
|
244
|
+
expect(map.count('mousemove:parcels')).toBe(0)
|
|
245
|
+
await wrapper.setProps({ on: true })
|
|
246
|
+
expect(map.count('mousemove:parcels')).toBe(1)
|
|
247
|
+
map.fire('mousemove:parcels', { features: [{ layer: { id: 'parcels' }, properties: { owner: 'Ana', area: 1500 } }], lngLat: { lng: 1, lat: 2 } })
|
|
248
|
+
await nextTick()
|
|
249
|
+
await nextTick()
|
|
250
|
+
const popup = created.popups.at(-1)!
|
|
251
|
+
expect(popup.isOpen()).toBe(true)
|
|
252
|
+
expect(popup.content?.textContent).toContain('Ana')
|
|
253
|
+
expect(popup.content?.textContent).toContain('1,500')
|
|
254
|
+
await wrapper.setProps({ on: false })
|
|
255
|
+
expect(map.count('mousemove:parcels')).toBe(0)
|
|
256
|
+
})
|
|
257
|
+
})
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { flushPromises, mount } from '@vue/test-utils'
|
|
3
|
+
import { defineComponent, h, nextTick } from 'vue'
|
|
4
|
+
import { created, type FakeMap } from './_fakeMaplibre'
|
|
5
|
+
import { centroid, distance, midpoint, pathLength, ringArea } from '../composables/_mapGeometry'
|
|
6
|
+
|
|
7
|
+
vi.mock('maplibre-gl', async () => (await import('./_fakeMaplibre')).fakeModule)
|
|
8
|
+
|
|
9
|
+
const { MMap, MMapMeasure, MMapIdentify, MMapFilter, MMapSearch, orvianAdapter, nominatimProvider } = await import('../map')
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
created.maps = []
|
|
13
|
+
created.markers = []
|
|
14
|
+
created.popups = []
|
|
15
|
+
document.documentElement.lang = 'en'
|
|
16
|
+
document.documentElement.style.setProperty('--color-primary', '#6750a4')
|
|
17
|
+
document.documentElement.style.setProperty('--color-surface', '#fef7ff')
|
|
18
|
+
})
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
document.body.innerHTML = ''
|
|
21
|
+
vi.unstubAllGlobals()
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
async function mountMap(slot: () => unknown, props: Record<string, unknown> = {}) {
|
|
25
|
+
const wrapper = mount(MMap, { props: { mapStyle: 's', ...props }, slots: { default: slot }, attachTo: document.body })
|
|
26
|
+
await flushPromises()
|
|
27
|
+
const map = created.maps.at(-1)! as FakeMap
|
|
28
|
+
map.load()
|
|
29
|
+
await nextTick()
|
|
30
|
+
await nextTick()
|
|
31
|
+
return { wrapper, map }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Controls are moved out of the wrapper into the map's corners (document.body with the stand-in)
|
|
35
|
+
const button = (label: string) => document.querySelector<HTMLButtonElement>(`button[title="${label}"], button[aria-label="${label}"]`)!
|
|
36
|
+
const press = async (label: string) => { button(label).click(); await nextTick() }
|
|
37
|
+
|
|
38
|
+
const click = (map: FakeMap, lng: number, lat: number) =>
|
|
39
|
+
map.fire('click', { target: map, lngLat: { lng, lat }, point: map.project([lng, lat]) })
|
|
40
|
+
|
|
41
|
+
describe('map geometry (turf\'s numbers)', () => {
|
|
42
|
+
// The same path checked against @turf/turf 7: length 8735.186 m, area 8967717.4 m²
|
|
43
|
+
const path: [number, number][] = [[-64.19, -31.42], [-64.17, -31.41], [-64.15, -31.43], [-64.18, -31.45]]
|
|
44
|
+
|
|
45
|
+
it('measures lengths, areas and midpoints on the sphere', () => {
|
|
46
|
+
expect(pathLength(path)).toBeCloseTo(8735.186, 2)
|
|
47
|
+
expect(ringArea(path)).toBeCloseTo(8967717.4, 0)
|
|
48
|
+
const [lng, lat] = midpoint(path[0]!, path[1]!)
|
|
49
|
+
expect(lng).toBeCloseTo(-64.1799995, 6)
|
|
50
|
+
expect(lat).toBeCloseTo(-31.4150004, 6)
|
|
51
|
+
expect(distance(path[0]!, path[0]!)).toBe(0)
|
|
52
|
+
expect(ringArea(path.slice(0, 2))).toBe(0)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('labels a polygon at its centroid', () => {
|
|
56
|
+
const [lng, lat] = centroid([[0, 0], [2, 0], [2, 2], [0, 2]])
|
|
57
|
+
expect(lng).toBeCloseTo(1)
|
|
58
|
+
expect(lat).toBeCloseTo(1)
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
describe('MMapMeasure', () => {
|
|
63
|
+
it('measures a distance: points on click, labels per segment and in total, finished with Enter', async () => {
|
|
64
|
+
const onChange = vi.fn()
|
|
65
|
+
const { map, wrapper } = await mountMap(() => h(MMapMeasure, { onChange }))
|
|
66
|
+
await press('Measure distance')
|
|
67
|
+
expect(map.canvas.style.cursor).toBe('crosshair')
|
|
68
|
+
expect(map.doubleClickZoom.enabled).toBe(false)
|
|
69
|
+
click(map, -64.19, -31.42)
|
|
70
|
+
click(map, -64.17, -31.41)
|
|
71
|
+
await nextTick()
|
|
72
|
+
const texts = () => created.markers.filter((m) => m.map).map((m) => m.element.textContent?.trim())
|
|
73
|
+
expect(texts()).toContain('Total: 2.20 km')
|
|
74
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter' }))
|
|
75
|
+
await nextTick()
|
|
76
|
+
const measurements = onChange.mock.calls.at(-1)![0]
|
|
77
|
+
expect(measurements).toHaveLength(1)
|
|
78
|
+
expect(measurements[0].mode).toBe('distance')
|
|
79
|
+
expect(measurements[0].length).toBeCloseTo(pathLength([[-64.19, -31.42], [-64.17, -31.41]]), 3)
|
|
80
|
+
expect(map.canvas.style.cursor).toBe('')
|
|
81
|
+
expect(map.doubleClickZoom.enabled).toBe(true)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('measures an area, closed at the end, with its label in m², ha or km²', async () => {
|
|
85
|
+
const onChange = vi.fn()
|
|
86
|
+
const { map, wrapper } = await mountMap(() => h(MMapMeasure, { onChange }))
|
|
87
|
+
await press('Measure area')
|
|
88
|
+
for (const [lng, lat] of [[-64.19, -31.42], [-64.17, -31.41], [-64.15, -31.43], [-64.18, -31.45]]) click(map, lng!, lat!)
|
|
89
|
+
await nextTick()
|
|
90
|
+
expect(created.markers.filter((m) => m.map).map((m) => m.element.textContent?.trim())).toContain('8.97 km²')
|
|
91
|
+
// Pressing the active button again finishes
|
|
92
|
+
await press('Measure area')
|
|
93
|
+
const [m] = onChange.mock.calls.at(-1)![0]
|
|
94
|
+
expect(m.closed).toBe(true)
|
|
95
|
+
expect(m.points.at(-1)).toEqual(m.points[0])
|
|
96
|
+
expect(m.area).toBeCloseTo(8967717, -1)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('closes a shape on its first point, and clears everything', async () => {
|
|
100
|
+
const onChange = vi.fn()
|
|
101
|
+
const { map, wrapper } = await mountMap(() => h(MMapMeasure, { onChange }))
|
|
102
|
+
await press('Measure distance')
|
|
103
|
+
click(map, -64.19, -31.42)
|
|
104
|
+
click(map, -64.17, -31.41)
|
|
105
|
+
click(map, -64.15, -31.43)
|
|
106
|
+
click(map, -64.1901, -31.4201) // within 20px of the first point
|
|
107
|
+
await nextTick()
|
|
108
|
+
expect(onChange.mock.calls.at(-1)![0][0].closed).toBe(true)
|
|
109
|
+
await press('Clear measurements')
|
|
110
|
+
expect(onChange.mock.calls.at(-1)![0]).toEqual([])
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('deletes one measurement from the ✕ on its label', async () => {
|
|
114
|
+
const onChange = vi.fn()
|
|
115
|
+
const { map } = await mountMap(() => h(MMapMeasure, { onChange }))
|
|
116
|
+
for (const pts of [[[-64.19, -31.42], [-64.17, -31.41]], [[-64.1, -31.3], [-64.08, -31.31]]]) {
|
|
117
|
+
await press('Measure distance')
|
|
118
|
+
for (const [lng, lat] of pts) click(map, lng!, lat!)
|
|
119
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter' }))
|
|
120
|
+
await nextTick()
|
|
121
|
+
}
|
|
122
|
+
expect(onChange.mock.calls.at(-1)![0]).toHaveLength(2)
|
|
123
|
+
const deletes = () => [...document.querySelectorAll<HTMLButtonElement>('button[aria-label="Delete measurement"]')].filter((b) => created.markers.some((m) => m.map && m.element.contains(b)))
|
|
124
|
+
expect(deletes()).toHaveLength(2)
|
|
125
|
+
deletes()[0]!.click()
|
|
126
|
+
await nextTick()
|
|
127
|
+
const left = onChange.mock.calls.at(-1)![0]
|
|
128
|
+
expect(left).toHaveLength(1)
|
|
129
|
+
expect(left[0].points[0]).toEqual([-64.1, -31.3])
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it('takes Escape from the browser while measuring in fullscreen (Keyboard Lock), and gives it back', async () => {
|
|
133
|
+
const lock = vi.fn(async () => {})
|
|
134
|
+
const unlock = vi.fn()
|
|
135
|
+
vi.stubGlobal('navigator', { ...navigator, keyboard: { lock, unlock } })
|
|
136
|
+
Object.defineProperty(document, 'fullscreenElement', { value: document.body, configurable: true })
|
|
137
|
+
try {
|
|
138
|
+
const { map } = await mountMap(() => h(MMapMeasure))
|
|
139
|
+
await press('Measure distance')
|
|
140
|
+
expect(lock).toHaveBeenCalledWith(['Escape'])
|
|
141
|
+
await Promise.resolve()
|
|
142
|
+
click(map, 1, 1)
|
|
143
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
|
|
144
|
+
expect(map.canvas.style.cursor).toBe('')
|
|
145
|
+
// Still locked until that Escape is let go: unlocking during its keydown lets the browser leave fullscreen
|
|
146
|
+
expect(unlock).not.toHaveBeenCalled()
|
|
147
|
+
document.dispatchEvent(new KeyboardEvent('keyup', { key: 'Escape' }))
|
|
148
|
+
await new Promise((r) => setTimeout(r))
|
|
149
|
+
expect(unlock).toHaveBeenCalled()
|
|
150
|
+
} finally {
|
|
151
|
+
Object.defineProperty(document, 'fullscreenElement', { value: null, configurable: true })
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
it('keeps MMapIdentify quiet while measuring', async () => {
|
|
156
|
+
const onIdentify = vi.fn()
|
|
157
|
+
const { map, wrapper } = await mountMap(() => [h(MMapMeasure), h(MMapIdentify, { onIdentify })], { adapter: orvianAdapter() })
|
|
158
|
+
await press('Measure distance')
|
|
159
|
+
click(map, 1, 1)
|
|
160
|
+
expect(onIdentify).not.toHaveBeenCalled()
|
|
161
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
|
|
162
|
+
click(map, 1, 1)
|
|
163
|
+
expect(onIdentify).toHaveBeenCalledTimes(1)
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('starts from v-model', async () => {
|
|
167
|
+
const onUpdate = vi.fn()
|
|
168
|
+
const Host = defineComponent({
|
|
169
|
+
props: { mode: { type: String, default: null } },
|
|
170
|
+
setup: (p) => () => h(MMap, { mapStyle: 's' }, () => h(MMapMeasure, { modelValue: p.mode as never, control: false, 'onUpdate:modelValue': onUpdate })),
|
|
171
|
+
})
|
|
172
|
+
const wrapper = mount(Host, { attachTo: document.body })
|
|
173
|
+
await flushPromises()
|
|
174
|
+
const map = created.maps.at(-1)!
|
|
175
|
+
map.load()
|
|
176
|
+
await nextTick()
|
|
177
|
+
await nextTick()
|
|
178
|
+
await wrapper.setProps({ mode: 'area' })
|
|
179
|
+
expect(map.canvas.style.cursor).toBe('crosshair')
|
|
180
|
+
await wrapper.setProps({ mode: null })
|
|
181
|
+
expect(map.canvas.style.cursor).toBe('')
|
|
182
|
+
})
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
describe('MMapFilter', () => {
|
|
186
|
+
it('filters a layer by the selected values, keeping the style\'s own filter', async () => {
|
|
187
|
+
const { map, wrapper } = await mountMap(() => h(MMapFilter, { layer: 'places', property: 'kind', values: ['Library', 'Market'] }))
|
|
188
|
+
map.style = { layers: [{ id: 'places', type: 'circle', filter: ['==', ['get', 'open'], true] } as never] }
|
|
189
|
+
await nextTick()
|
|
190
|
+
const chips = wrapper.findAll('button, [role="button"], .cursor-pointer').filter((c) => /Library|Market/.test(c.text()))
|
|
191
|
+
await chips.find((c) => c.text().includes('Market'))!.trigger('click')
|
|
192
|
+
expect(map.filters.get('places')).toEqual(['all', ['==', ['get', 'open'], true], ['in', ['get', 'kind'], ['literal', ['Market']]]])
|
|
193
|
+
await chips.find((c) => c.text().includes('Market'))!.trigger('click')
|
|
194
|
+
expect(map.filters.get('places')).toEqual(['==', ['get', 'open'], true])
|
|
195
|
+
})
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
describe('MMapSearch', () => {
|
|
199
|
+
it('searches on Enter through its provider, and flies to the picked result with a marker', async () => {
|
|
200
|
+
const provider = vi.fn(async () => [{ label: 'Plaza San Martín', lngLat: [-64.1836, -31.4167] as [number, number], type: 'park' }])
|
|
201
|
+
const onSelect = vi.fn()
|
|
202
|
+
const { map } = await mountMap(() => h(MMapSearch, { provider, onSelect }))
|
|
203
|
+
const input = document.querySelector('input[type="search"]') as HTMLInputElement
|
|
204
|
+
input.value = 'plaza'
|
|
205
|
+
input.dispatchEvent(new Event('input'))
|
|
206
|
+
expect(provider).not.toHaveBeenCalled()
|
|
207
|
+
input.form!.dispatchEvent(new Event('submit', { cancelable: true }))
|
|
208
|
+
await flushPromises()
|
|
209
|
+
expect(provider).toHaveBeenCalledWith('plaza', expect.any(AbortSignal))
|
|
210
|
+
const item = [...document.querySelectorAll<HTMLElement>('li, [role="listitem"], [role="button"]')].find((e) => e.textContent?.includes('Plaza San Martín'))!
|
|
211
|
+
expect(item).toBeTruthy()
|
|
212
|
+
item.click()
|
|
213
|
+
await nextTick()
|
|
214
|
+
expect(map.flights.at(-1)).toEqual(['flyTo', { center: [-64.1836, -31.4167], zoom: 17 }])
|
|
215
|
+
expect(onSelect).toHaveBeenCalled()
|
|
216
|
+
await nextTick()
|
|
217
|
+
expect(created.markers.some((m) => m.map && m.lngLat.lng === -64.1836)).toBe(true)
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
it('nominatimProvider asks Nominatim and reads its results and extents', async () => {
|
|
221
|
+
const fetchMock = vi.fn(async () => new Response(JSON.stringify([
|
|
222
|
+
{ display_name: 'Córdoba, Argentina', lat: '-31.41', lon: '-64.18', type: 'city', boundingbox: ['-31.5', '-31.3', '-64.3', '-64.0'] },
|
|
223
|
+
])))
|
|
224
|
+
vi.stubGlobal('fetch', fetchMock)
|
|
225
|
+
const [result] = await nominatimProvider({ countryCodes: ['ar'], language: 'es' })('córdoba', new AbortController().signal)
|
|
226
|
+
const url = new URL(String((fetchMock.mock.calls[0] as unknown[])[0]))
|
|
227
|
+
expect(url.origin + url.pathname).toBe('https://nominatim.openstreetmap.org/search')
|
|
228
|
+
expect(url.searchParams.get('q')).toBe('córdoba')
|
|
229
|
+
expect(url.searchParams.get('countrycodes')).toBe('ar')
|
|
230
|
+
expect(url.searchParams.get('accept-language')).toBe('es')
|
|
231
|
+
expect(result).toEqual({ label: 'Córdoba, Argentina', lngLat: [-64.18, -31.41], bbox: [-64.3, -31.5, -64.0, -31.3], type: 'city' })
|
|
232
|
+
})
|
|
233
|
+
})
|
|
@@ -47,4 +47,20 @@ describe('MShader', () => {
|
|
|
47
47
|
expect(typeof wrapper.vm.play).toBe('function')
|
|
48
48
|
expect(typeof wrapper.vm.restart).toBe('function')
|
|
49
49
|
})
|
|
50
|
+
|
|
51
|
+
it('resizes the canvas when pixelRatio changes', async () => {
|
|
52
|
+
const wrapper = mount(MShader, { props: { code: BASIC_CODE, pixelRatio: 1 } })
|
|
53
|
+
await flushPromises()
|
|
54
|
+
const canvas = wrapper.vm.canvas as HTMLCanvasElement
|
|
55
|
+
// happy-dom has no layout: give the canvas's container a size.
|
|
56
|
+
const container = canvas.parentElement!
|
|
57
|
+
Object.defineProperty(container, 'clientWidth', { value: 200 })
|
|
58
|
+
Object.defineProperty(container, 'clientHeight', { value: 100 })
|
|
59
|
+
|
|
60
|
+
await wrapper.setProps({ pixelRatio: 2 })
|
|
61
|
+
expect([canvas.width, canvas.height]).toEqual([400, 200])
|
|
62
|
+
|
|
63
|
+
await wrapper.setProps({ pixelRatio: 0.5 })
|
|
64
|
+
expect([canvas.width, canvas.height]).toEqual([100, 50])
|
|
65
|
+
})
|
|
50
66
|
})
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// MapLibre needs WebGL, which happy-dom lacks: a stand-in that records what the map components ask of it
|
|
2
|
+
type Listener = (e?: unknown) => void
|
|
3
|
+
class Evented {
|
|
4
|
+
listeners = new Map<string, Listener[]>()
|
|
5
|
+
on(type: string, a: unknown, b?: unknown) {
|
|
6
|
+
const key = typeof a === 'string' ? `${type}:${a}` : type
|
|
7
|
+
const fn = (typeof a === 'string' ? b : a) as Listener
|
|
8
|
+
this.listeners.set(key, [...(this.listeners.get(key) ?? []), fn])
|
|
9
|
+
return this
|
|
10
|
+
}
|
|
11
|
+
once(type: string, fn: Listener) { return this.on(type, fn) }
|
|
12
|
+
off(type: string, a: unknown, b?: unknown) {
|
|
13
|
+
const key = typeof a === 'string' ? `${type}:${a}` : type
|
|
14
|
+
const fn = typeof a === 'string' ? b : a
|
|
15
|
+
this.listeners.set(key, (this.listeners.get(key) ?? []).filter((l) => l !== fn))
|
|
16
|
+
return this
|
|
17
|
+
}
|
|
18
|
+
fire(key: string, e?: unknown) { for (const l of this.listeners.get(key) ?? []) l(e) }
|
|
19
|
+
count(key: string) { return (this.listeners.get(key) ?? []).length }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const created: { maps: FakeMap[]; markers: FakeMarker[]; popups: FakePopup[] } = { maps: [], markers: [], popups: [] }
|
|
23
|
+
|
|
24
|
+
export class FakeMap extends Evented {
|
|
25
|
+
options: Record<string, unknown>
|
|
26
|
+
center = { lng: 0, lat: 0 }
|
|
27
|
+
zoom = 1
|
|
28
|
+
bearing = 0
|
|
29
|
+
pitch = 0
|
|
30
|
+
controls: { control: { onAdd?: (m: unknown) => HTMLElement }; position: string }[] = []
|
|
31
|
+
sources = new Map<string, { data: unknown; setData: (d: unknown) => void }>()
|
|
32
|
+
layers: { id: string; [k: string]: unknown }[] = []
|
|
33
|
+
canvas = document.createElement('canvas')
|
|
34
|
+
styles: unknown[] = []
|
|
35
|
+
removed = false
|
|
36
|
+
constructor(options: Record<string, unknown>) {
|
|
37
|
+
super()
|
|
38
|
+
this.options = options
|
|
39
|
+
if (options.center) { const [lng, lat] = options.center as number[]; this.center = { lng: lng!, lat: lat! } }
|
|
40
|
+
if (options.zoom !== undefined) this.zoom = options.zoom as number
|
|
41
|
+
created.maps.push(this)
|
|
42
|
+
}
|
|
43
|
+
getCenter() { return this.center }
|
|
44
|
+
getZoom() { return this.zoom }
|
|
45
|
+
getBearing() { return this.bearing }
|
|
46
|
+
getPitch() { return this.pitch }
|
|
47
|
+
jumpTo(o: { center?: number[]; zoom?: number }) {
|
|
48
|
+
if (o.center) this.center = { lng: o.center[0]!, lat: o.center[1]! }
|
|
49
|
+
if (o.zoom !== undefined) this.zoom = o.zoom
|
|
50
|
+
}
|
|
51
|
+
addControl(control: FakeMap['controls'][number]['control'], position: string) {
|
|
52
|
+
this.controls.push({ control, position })
|
|
53
|
+
const el = control.onAdd?.(this)
|
|
54
|
+
if (el) document.body.appendChild(el)
|
|
55
|
+
return this
|
|
56
|
+
}
|
|
57
|
+
removeControl(control: unknown) { this.controls = this.controls.filter((c) => c.control !== control); return this }
|
|
58
|
+
hasControl(control: unknown) { return this.controls.some((c) => c.control === control) }
|
|
59
|
+
setStyle(style: unknown) {
|
|
60
|
+
this.styles.push(style)
|
|
61
|
+
this.sources.clear()
|
|
62
|
+
this.layers = []
|
|
63
|
+
}
|
|
64
|
+
addSource(id: string, spec: { data: unknown }) {
|
|
65
|
+
const source = { data: spec.data, setData: (d: unknown) => { source.data = d } }
|
|
66
|
+
this.sources.set(id, source)
|
|
67
|
+
}
|
|
68
|
+
getSource(id: string) {
|
|
69
|
+
if (this.removed) throw new TypeError("can't access property \"getSource\", this.style is undefined")
|
|
70
|
+
return this.sources.get(id)
|
|
71
|
+
}
|
|
72
|
+
removeSource(id: string) { this.sources.delete(id) }
|
|
73
|
+
addLayer(layer: { id: string }, before?: string) { this.layers.push({ ...layer, before }) }
|
|
74
|
+
getLayer(id: string) {
|
|
75
|
+
// Like MapLibre: a removed map has no style left
|
|
76
|
+
if (this.removed) throw new TypeError("can't access property \"getLayer\", this.style is undefined")
|
|
77
|
+
return this.layers.find((l) => l.id === id) ?? this.styleLayer(id)
|
|
78
|
+
}
|
|
79
|
+
removeLayer(id: string) { this.layers = this.layers.filter((l) => l.id !== id) }
|
|
80
|
+
getCanvas() { return this.canvas }
|
|
81
|
+
/** The style getStyle() returns (with `layers` and `metadata`), set by tests */
|
|
82
|
+
style: { name?: string; metadata?: unknown; layers: { id: string; type: string; metadata?: unknown; layout?: Record<string, unknown>; paint?: Record<string, unknown> }[] } = { layers: [] }
|
|
83
|
+
getStyle() { return this.style }
|
|
84
|
+
private styleLayer(id: string) { return this.style.layers.find((l) => l.id === id) }
|
|
85
|
+
getLayoutProperty(id: string, prop: string) { return this.styleLayer(id)?.layout?.[prop] }
|
|
86
|
+
setLayoutProperty(id: string, prop: string, value: unknown) { const l = this.styleLayer(id); if (l) l.layout = { ...l.layout, [prop]: value } }
|
|
87
|
+
getPaintProperty(id: string, prop: string) { return this.styleLayer(id)?.paint?.[prop] }
|
|
88
|
+
setPaintProperty(id: string, prop: string, value: unknown) { const l = this.styleLayer(id); if (l) l.paint = { ...l.paint, [prop]: value } }
|
|
89
|
+
// Screen position: 1px per 0.001° (enough to tell near from far)
|
|
90
|
+
project([lng, lat]: number[]) { return { x: lng! * 1000, y: -lat! * 1000 } }
|
|
91
|
+
doubleClickZoom = { enabled: true, enable() { this.enabled = true }, disable() { this.enabled = false } }
|
|
92
|
+
dragPan = { enabled: true, enable() { this.enabled = true }, disable() { this.enabled = false } }
|
|
93
|
+
filters = new Map<string, unknown>()
|
|
94
|
+
getFilter(id: string) { return this.filters.get(id) ?? (this.styleLayer(id) as { filter?: unknown } | undefined)?.filter }
|
|
95
|
+
setFilter(id: string, filter: unknown) { this.filters.set(id, filter) }
|
|
96
|
+
/** A source's loaded features, set by tests */
|
|
97
|
+
sourceFeatures: { properties: Record<string, unknown> }[] = []
|
|
98
|
+
querySourceFeatures() { return this.sourceFeatures }
|
|
99
|
+
flights: unknown[] = []
|
|
100
|
+
flyTo(o: unknown) { this.flights.push(['flyTo', o]) }
|
|
101
|
+
fitBounds(b: unknown, o?: unknown) { this.flights.push(['fitBounds', b, o]) }
|
|
102
|
+
/** Features "under the pointer", set by tests */
|
|
103
|
+
rendered: { layer: { id: string }; properties: Record<string, unknown> }[] = []
|
|
104
|
+
queryRenderedFeatures(_point: unknown, options?: { layers?: string[] }) { return this.rendered.filter((f) => !options?.layers || options.layers.includes(f.layer.id)) }
|
|
105
|
+
remove() { this.removed = true }
|
|
106
|
+
/** Test helper: the style loaded */
|
|
107
|
+
load() { this.fire('style.load'); this.fire('load') }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export class FakeMarker extends Evented {
|
|
111
|
+
options: Record<string, unknown>
|
|
112
|
+
element: HTMLElement
|
|
113
|
+
lngLat = { lng: 0, lat: 0 }
|
|
114
|
+
popup?: FakePopup
|
|
115
|
+
map?: FakeMap
|
|
116
|
+
constructor(options: Record<string, unknown>) {
|
|
117
|
+
super()
|
|
118
|
+
this.options = options
|
|
119
|
+
this.element = (options.element as HTMLElement) ?? document.createElement('div')
|
|
120
|
+
created.markers.push(this)
|
|
121
|
+
}
|
|
122
|
+
setLngLat([lng, lat]: number[]) { this.lngLat = { lng: lng!, lat: lat! }; return this }
|
|
123
|
+
getLngLat() { return this.lngLat }
|
|
124
|
+
addTo(map: FakeMap) { this.map = map; return this }
|
|
125
|
+
getElement() { return this.element }
|
|
126
|
+
setPopup(p: FakePopup) { this.popup = p; return this }
|
|
127
|
+
getPopup() { return this.popup }
|
|
128
|
+
setDraggable() { return this }
|
|
129
|
+
setRotation() { return this }
|
|
130
|
+
setOffset() { return this }
|
|
131
|
+
remove() { this.map = undefined; return this }
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export class FakePopup extends Evented {
|
|
135
|
+
options: Record<string, unknown>
|
|
136
|
+
content?: HTMLElement
|
|
137
|
+
lngLat?: { lng: number; lat: number }
|
|
138
|
+
opened = false
|
|
139
|
+
constructor(options: Record<string, unknown>) { super(); this.options = options; created.popups.push(this) }
|
|
140
|
+
setDOMContent(el: HTMLElement) { this.content = el; return this }
|
|
141
|
+
setLngLat([lng, lat]: number[]) { this.lngLat = { lng: lng!, lat: lat! }; return this }
|
|
142
|
+
setMaxWidth() { return this }
|
|
143
|
+
addTo() { this.opened = true; return this }
|
|
144
|
+
isOpen() { return this.opened }
|
|
145
|
+
remove() { if (this.opened) { this.opened = false; this.fire('close') } return this }
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
class Control { constructor(public options?: unknown) {} }
|
|
149
|
+
export const fakeModule = {
|
|
150
|
+
Map: FakeMap,
|
|
151
|
+
Marker: FakeMarker,
|
|
152
|
+
Popup: FakePopup,
|
|
153
|
+
NavigationControl: class NavigationControl extends Control {},
|
|
154
|
+
GeolocateControl: class GeolocateControl extends Control {},
|
|
155
|
+
FullscreenControl: class FullscreenControl extends Control {},
|
|
156
|
+
ScaleControl: class ScaleControl extends Control {},
|
|
157
|
+
AttributionControl: class AttributionControl extends Control {},
|
|
158
|
+
}
|