|
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' |
3 | 3 | import { TeritorioCluster } from '../src/index' |
4 | 4 |
|
5 | 5 | 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' }) |
8 | 8 |
|
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 | + }) |
12 | 21 |
|
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') |
26 | 33 |
|
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) |
31 | 35 |
|
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 | + }) |
39 | 40 |
|
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() |
42 | 44 |
|
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 | + }) |
46 | 57 |
|
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() |
49 | 75 |
|
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') |
51 | 87 | }) |
52 | 88 | }) |
0 commit comments