Skip to content

Commit

Permalink
feat: add vitest suite + first test #29
Browse files Browse the repository at this point in the history
  • Loading branch information
wazolab committed Nov 19, 2024
1 parent 22f5706 commit 89070e0
Show file tree
Hide file tree
Showing 7 changed files with 672 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules
dist
coverage/

# Yarn v2
.pnp.*
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
}
},
"scripts": {
"dev": "vite",
"build": "vite build && tsc",
"build:demo": "vite build --mode development",
"coverage": "vitest run --coverage",
"dev": "vite",
"preversion": "yarn build",
"preview": "vite preview",
"preversion": "yarn build"
"test": "vitest"
},
"repository": {
"type": "git",
Expand All @@ -33,8 +35,10 @@
},
"homepage": "https://github.com/teritorio/maplibre-gl-teritorio-cluster#readme",
"devDependencies": {
"@vitest/coverage-v8": "2.1.5",
"typescript": "^5.6.2",
"vite": "^5.4.4"
"vite": "^5.4.4",
"vitest": "^2.1.5"
},
"dependencies": {
"@turf/bbox": "^7.1.0",
Expand Down
6 changes: 3 additions & 3 deletions src/teritorio-cluster.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FitBoundsOptions, GeoJSONSource, LngLatLike, MapGeoJSONFeature, MapSourceDataEvent } from 'maplibre-gl'
import { LngLat, Marker, Point } from 'maplibre-gl'
import { LngLat, Marker, Point, type Map as MapGL } from 'maplibre-gl'
import {
clusterRenderDefault,
markerRenderDefault,
Expand Down Expand Up @@ -43,7 +43,7 @@ type FeatureMatch = FeatureInClusterMatch | MapGeoJSONFeature
const UnfoldedClusterClass = 'teritorio-unfolded-cluster'

export class TeritorioCluster extends EventTarget {
map: maplibregl.Map
map: MapGL
clusterLeaves: Map<string, MapGeoJSONFeature[]>
clusterMaxZoom: number
clusterMinZoom: number
Expand All @@ -64,7 +64,7 @@ export class TeritorioCluster extends EventTarget {
unfoldedClusterMaxLeaves: number

constructor(
map: maplibregl.Map,
map: MapGL,
sourceId: string,
options?: {
clusterMaxZoom?: number,
Expand Down
53 changes: 53 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { TeritorioCluster } from '../src/teritorio-cluster'
import { Map as MapGL } from 'maplibre-gl'

describe('TeritorioCluster', () => {
let map: MapGL
let cluster: TeritorioCluster

beforeEach(() => {
// Create a mock map
map = new MapGL({ container: 'map'})

// Initialize the cluster
cluster = new TeritorioCluster(map, 'sourceId')
})

it('should initialize with default values', () => {
expect(cluster.map).toMatchObject(map)
expect(cluster.clusterLeaves).toBeInstanceOf(Map)
expect(cluster.clusterLeaves.size).toBe(0)
expect(cluster.clusterMaxZoom).toBe(17)
expect(cluster.clusterMinZoom).toBe(0)

// Should have the default render function
expect(cluster.clusterRender).toBeUndefined()

expect(cluster.featuresMap).toBeInstanceOf(Map)
expect(cluster.featuresMap.size).toBe(0)
expect(cluster.fitBoundsOptions).toMatchObject({ padding: 20 })
expect(cluster.initialFeature).toBeUndefined()

// Should have the default render function
expect(cluster.markerRender).toBeUndefined()

expect(cluster.markerSize).toBe(24)
expect(cluster.markersOnScreen).toBeInstanceOf(Map)
expect(cluster.markersOnScreen.size).toBe(0)
expect(cluster.pinMarker).toBeNull()

// Should have the default render function
expect(cluster.pinMarkerRender).toBeUndefined()

expect(cluster.selectedClusterId).toBeNull()
expect(cluster.selectedFeatureId).toBeNull()
expect(cluster.sourceId).toBe('sourceId')
expect(cluster.ticking).toBeFalsy()

// Should have the default render function
expect(cluster.unfoldedClusterRender).toBeUndefined()

expect(cluster.unfoldedClusterMaxLeaves).toBe(7)
})
})
14 changes: 14 additions & 0 deletions tests/mocks/maplibre-gl.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { vi } from 'vitest'

vi.mock('maplibre-gl', () => {
return {
Map: vi.fn().mockImplementation(() => ({
on: vi.fn(),
querySourceFeatures: vi.fn().mockReturnValue([]),
getSource: vi.fn().mockReturnValue({
getClusterLeaves: vi.fn(),
}),
fitBounds: vi.fn(),
})),
}
})
10 changes: 10 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
coverage: {
reporter: ['text', 'json', 'html'],
},
setupFiles: './tests/mocks/maplibre-gl.mock.ts',
},
})
Loading

0 comments on commit 89070e0

Please sign in to comment.