Skip to content

Commit b0c009d

Browse files
committed
test: adapt test to new layer format #38
1 parent ae9f8b9 commit b0c009d

File tree

3 files changed

+86
-47
lines changed

3 files changed

+86
-47
lines changed

src/teritorio-cluster.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ export class TeritorioCluster extends EventTarget implements CustomLayerInterfac
302302
this.markersOnScreen = newMarkers
303303
}
304304

305+
public getOptionsForTesting(): Required<TeritorioClusterOptions> {
306+
return this.opts
307+
}
308+
305309
private renderUnfoldedCluster = (id: string, leaves: MapGeoJSONFeature[]): HTMLDivElement => {
306310
const element = document.createElement('div')
307311
element.id = id

tests/mocks/maplibre-gl.mock.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import { vi } from 'vitest'
22

33
vi.mock('maplibre-gl', () => {
44
return {
5-
Map: vi.fn().mockImplementation(() => ({
6-
on: vi.fn(),
7-
querySourceFeatures: vi.fn().mockReturnValue([]),
8-
getSource: vi.fn().mockReturnValue({
9-
getClusterLeaves: vi.fn(),
10-
}),
11-
fitBounds: vi.fn(),
12-
})),
5+
default: {
6+
Map: vi.fn().mockImplementation(() => ({
7+
addLayer: vi.fn(),
8+
addSource: vi.fn(),
9+
on: vi.fn(),
10+
})),
11+
},
1312
}
1413
})

tests/teritorio-cluster.test.ts

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,88 @@
1-
import { Map as MapGL } from 'maplibre-gl'
2-
import { beforeEach, describe, expect, it } from 'vitest'
1+
import maplibre from 'maplibre-gl'
2+
import { describe, expect, it, vi } from 'vitest'
33
import { TeritorioCluster } from '../src/index'
44

55
describe('teritorio cluster class implementation', () => {
6-
let map: MapGL
7-
let teritorioCluster: TeritorioCluster
6+
it('should call onAdd when map.addLayer is used', () => {
7+
const map = new maplibre.Map({ container: 'map' })
88

9-
beforeEach(() => {
10-
// Create a mock map
11-
map = new MapGL({ container: 'map' })
9+
map.on('load', () => {
10+
map.addSource('earthquakes', {
11+
type: 'geojson',
12+
data: {
13+
type: 'FeatureCollection',
14+
features: [],
15+
},
16+
cluster: true,
17+
clusterRadius: 80,
18+
clusterMaxZoom: 22,
19+
maxzoom: 24,
20+
})
1221

13-
// Initialize the cluster
14-
teritorioCluster = new TeritorioCluster(map, 'sourceId')
15-
})
16-
17-
it('should initialize with default values', () => {
18-
expect(teritorioCluster.map).toMatchObject(map)
19-
expect(teritorioCluster.clusterLeaves).toBeInstanceOf(Map)
20-
expect(teritorioCluster.clusterLeaves.size).toBe(0)
21-
expect(teritorioCluster.clusterMaxZoom).toBe(17)
22-
expect(teritorioCluster.clusterMinZoom).toBe(0)
23-
24-
// Should have the default render function
25-
expect(teritorioCluster.clusterRender).toBeUndefined()
22+
const teritorioLayer = new TeritorioCluster(
23+
'layerId',
24+
'sourceId',
25+
{
26+
clusterRender: vi.fn(),
27+
markerRender: vi.fn(),
28+
unfoldedClusterRender: vi.fn(),
29+
pinMarkerRender: vi.fn(),
30+
},
31+
)
32+
const onAddSpy = vi.spyOn(teritorioLayer, 'onAdd')
2633

27-
expect(teritorioCluster.featuresMap).toBeInstanceOf(Map)
28-
expect(teritorioCluster.featuresMap.size).toBe(0)
29-
expect(teritorioCluster.fitBoundsOptions).toMatchObject({ padding: 20 })
30-
expect(teritorioCluster.initialFeature).toBeUndefined()
34+
map.addLayer(teritorioLayer)
3135

32-
// Should have the default render function
33-
expect(teritorioCluster.markerRender).toBeUndefined()
34-
35-
expect(teritorioCluster.markerSize).toBe(24)
36-
expect(teritorioCluster.markersOnScreen).toBeInstanceOf(Map)
37-
expect(teritorioCluster.markersOnScreen.size).toBe(0)
38-
expect(teritorioCluster.pinMarker).toBeNull()
36+
expect(onAddSpy).toHaveBeenCalledOnce()
37+
expect(onAddSpy).toHaveBeenCalledWith(map)
38+
})
39+
})
3940

40-
// Should have the default render function
41-
expect(teritorioCluster.pinMarkerRender).toBeUndefined()
41+
it('should initialize with default values', () => {
42+
const teritorioCluster = new TeritorioCluster('layerId', 'sourceId')
43+
const opts = teritorioCluster.getOptionsForTesting()
4244

43-
expect(teritorioCluster.selectedClusterId).toBeNull()
44-
expect(teritorioCluster.selectedFeatureId).toBeNull()
45-
expect(teritorioCluster.sourceId).toBe('sourceId')
45+
expect(teritorioCluster.id).toBe('layerId')
46+
expect(opts.clusterMaxZoom).toBe(17)
47+
expect(opts.clusterMinZoom).toBe(0)
48+
expect(opts.markerSize).toBe(24)
49+
expect(opts.unfoldedClusterMaxLeaves).toBe(7)
50+
expect(opts.fitBoundsOptions).toEqual({ padding: 20 })
51+
expect(opts.initialFeature).toBeUndefined()
52+
expect(typeof opts.clusterRender).toBe('function')
53+
expect(typeof opts.markerRender).toBe('function')
54+
expect(typeof opts.pinMarkerRender).toBe('function')
55+
expect(typeof opts.unfoldedClusterRender).toBe('function')
56+
})
4657

47-
// Should have the default render function
48-
expect(teritorioCluster.unfoldedClusterRender).toBeUndefined()
58+
it('should initialize with user values', () => {
59+
const teritorioCluster = new TeritorioCluster(
60+
'layerId',
61+
'sourceId',
62+
{
63+
clusterMaxZoom: 22,
64+
clusterMinZoom: 2,
65+
clusterRender: vi.fn(),
66+
fitBoundsOptions: { padding: { top: 57, bottom: 20, left: 20, right: 20 } },
67+
markerRender: vi.fn(),
68+
markerSize: 28,
69+
unfoldedClusterRender: vi.fn(),
70+
unfoldedClusterMaxLeaves: 8,
71+
pinMarkerRender: vi.fn(),
72+
},
73+
)
74+
const opts = teritorioCluster.getOptionsForTesting()
4975

50-
expect(teritorioCluster.unfoldedClusterMaxLeaves).toBe(7)
76+
expect(teritorioCluster.id).toBe('layerId')
77+
expect(opts.clusterMaxZoom).toBe(22)
78+
expect(opts.clusterMinZoom).toBe(2)
79+
expect(opts.markerSize).toBe(28)
80+
expect(opts.unfoldedClusterMaxLeaves).toBe(8)
81+
expect(opts.fitBoundsOptions).toEqual({ padding: { top: 57, bottom: 20, left: 20, right: 20 } })
82+
expect(opts.initialFeature).toBeUndefined()
83+
expect(typeof opts.clusterRender).toBe('function')
84+
expect(typeof opts.markerRender).toBe('function')
85+
expect(typeof opts.pinMarkerRender).toBe('function')
86+
expect(typeof opts.unfoldedClusterRender).toBe('function')
5187
})
5288
})

0 commit comments

Comments
 (0)