From 1c8c0c45e79324b2fe970ac33491e4f9278a99f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Viricel?= Date: Thu, 21 Nov 2024 11:35:33 +0100 Subject: [PATCH] chore(lint): apply lint rules #30 --- src/teritorio-cluster.ts | 28 +++++++------ src/utils/get-feature-id.ts | 9 +++-- tests/get-feature-id.test.ts | 64 +++++++++++++++--------------- tests/set-selected-feature.test.ts | 36 ++++++++--------- 4 files changed, 70 insertions(+), 67 deletions(-) diff --git a/src/teritorio-cluster.ts b/src/teritorio-cluster.ts index d2997cc..149bbe2 100644 --- a/src/teritorio-cluster.ts +++ b/src/teritorio-cluster.ts @@ -2,13 +2,13 @@ import type { FitBoundsOptions, GeoJSONSource, LngLatLike, MapSourceDataEvent } import bbox from '@turf/bbox' import { featureCollection } from '@turf/helpers' import { Marker, Point } from 'maplibre-gl' +import { getFeatureId } from './utils/get-feature-id' import { clusterRenderDefault, markerRenderDefault, pinMarkerRenderDefault, unfoldedClusterRenderSmart, } from './utils/helpers' -import { getFeatureId } from './utils/get-feature-id' type UnfoldedCluster = ( ( @@ -68,12 +68,12 @@ export class TeritorioCluster extends EventTarget { map: maplibregl.Map, sourceId: string, options?: { - clusterMaxZoom?: number, - clusterMinZoom?: number, - clusterRenderFn?: ClusterRender, - fitBoundsOptions?: FitBoundsOptions, - initialFeature?: GeoJSON.Feature, - markerRenderFn?: MarkerRender, + clusterMaxZoom?: number + clusterMinZoom?: number + clusterRenderFn?: ClusterRender + fitBoundsOptions?: FitBoundsOptions + initialFeature?: GeoJSON.Feature + markerRenderFn?: MarkerRender markerSize?: number unfoldedClusterRenderFn?: UnfoldedCluster unfoldedClusterMaxLeaves?: number @@ -127,7 +127,7 @@ export class TeritorioCluster extends EventTarget { this.fitBoundsOptions = options } - setSelectedFeature = (feature: GeoJSON.Feature) => { + setSelectedFeature = (feature: GeoJSON.Feature): void => { const id = getFeatureId(feature) const match = this.#findFeature(id) @@ -153,7 +153,7 @@ export class TeritorioCluster extends EventTarget { return } - + if ('feature' in match && match.feature.geometry.type === 'Point') { const cluster = this.markersOnScreen.get(match.clusterId) @@ -229,17 +229,19 @@ export class TeritorioCluster extends EventTarget { return false } - const featureIndex = cluster.findIndex(feature => { + const featureIndex = cluster.findIndex((feature) => { try { return getFeatureId(feature) === featureId - } catch (error) { + } + catch (error) { console.error(`Error getting feature ID for feature:`, feature, error) return false } }) return featureIndex > -1 - } catch (error) { + } + catch (error) { console.error(`Error checking if feature ${featureId} is in cluster ${clusterId}:`, error) return false } @@ -352,7 +354,7 @@ export class TeritorioCluster extends EventTarget { } } - this.featuresMap.forEach(feature => { + this.featuresMap.forEach((feature) => { const coords = feature.geometry.type === 'Point' ? feature.geometry.coordinates as LngLatLike : undefined const id = getFeatureId(feature) diff --git a/src/utils/get-feature-id.ts b/src/utils/get-feature-id.ts index 7a158e8..a62b549 100644 --- a/src/utils/get-feature-id.ts +++ b/src/utils/get-feature-id.ts @@ -1,4 +1,4 @@ -export const getFeatureId = (feature: GeoJSON.Feature): string => { +export function getFeatureId(feature: GeoJSON.Feature): string { try { if (!feature.properties) { throw new Error('Feature properties are null or undefined') @@ -25,9 +25,10 @@ export const getFeatureId = (feature: GeoJSON.Feature): string => { } return metadata.id.toString() - } catch (error) { - console.error("Error in getFeatureId: ", error); + } + catch (error) { + console.error('Error in getFeatureId: ', error) return feature.id?.toString() || 'unknown' } -} \ No newline at end of file +} diff --git a/tests/get-feature-id.test.ts b/tests/get-feature-id.test.ts index f863806..b2055cc 100644 --- a/tests/get-feature-id.test.ts +++ b/tests/get-feature-id.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, vi } from 'vitest'; +import { describe, expect, it, vi } from 'vitest' import { getFeatureId } from '../src/utils/get-feature-id' describe('getFeatureId', () => { @@ -8,72 +8,72 @@ describe('getFeatureId', () => { properties: { cluster: true }, id: 123, geometry: { type: 'Point', coordinates: [0, 0] }, - }; + } - const result = getFeatureId(feature); - expect(result).toBe('123'); - }); + const result = getFeatureId(feature) + expect(result).toBe('123') + }) it('should return the feature id if no metadata and but property id exists', () => { const feature: GeoJSON.Feature = { type: 'Feature', properties: { id: 456 }, geometry: { type: 'Point', coordinates: [1, 1] }, - }; + } - const result = getFeatureId(feature); - expect(result).toBe('456'); - }); + const result = getFeatureId(feature) + expect(result).toBe('456') + }) it('should return the metadata id if metadata exists', () => { const feature: GeoJSON.Feature = { type: 'Feature', properties: { metadata: '{"id": "789"}' }, geometry: { type: 'Point', coordinates: [2, 2] }, - }; + } - const result = getFeatureId(feature); - expect(result).toBe('789'); - }); + const result = getFeatureId(feature) + expect(result).toBe('789') + }) it('should return "unknown" when no id or metadata is available', () => { const feature: GeoJSON.Feature = { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [3, 3] }, - }; + } - const result = getFeatureId(feature); - expect(result).toBe('unknown'); - }); + const result = getFeatureId(feature) + expect(result).toBe('unknown') + }) it('should handle malformed metadata and return "unknown"', () => { const feature: GeoJSON.Feature = { type: 'Feature', properties: { metadata: 'invalid-json' }, geometry: { type: 'Point', coordinates: [4, 4] }, - }; + } - const result = getFeatureId(feature); - expect(result).toBe('unknown'); - }); + const result = getFeatureId(feature) + expect(result).toBe('unknown') + }) it('should log an error when an exception occurs', () => { - const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => { }); + const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => { }) const feature: GeoJSON.Feature = { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [3, 3] }, - }; + } - getFeatureId(feature); + getFeatureId(feature) expect(consoleErrorSpy).toHaveBeenCalled() - expect(consoleErrorSpy.mock.calls[0][0]).toContain('Error in getFeatureId:'); + expect(consoleErrorSpy.mock.calls[0][0]).toContain('Error in getFeatureId:') - consoleErrorSpy.mockRestore(); - }); + consoleErrorSpy.mockRestore() + }) it('should return feature id if metadata is malformed but feature has id', () => { const feature: GeoJSON.Feature = { @@ -81,9 +81,9 @@ describe('getFeatureId', () => { properties: { metadata: 'invalid-json' }, id: 101, geometry: { type: 'Point', coordinates: [5, 5] }, - }; + } - const result = getFeatureId(feature); - expect(result).toBe('101'); - }); -}); + const result = getFeatureId(feature) + expect(result).toBe('101') + }) +}) diff --git a/tests/set-selected-feature.test.ts b/tests/set-selected-feature.test.ts index 3f3a493..7e3f775 100644 --- a/tests/set-selected-feature.test.ts +++ b/tests/set-selected-feature.test.ts @@ -1,18 +1,18 @@ -import { describe, it, expect, beforeEach, vi } from 'vitest' -import { TeritorioCluster } from '../src/teritorio-cluster' import { Map as MapGL } from 'maplibre-gl' -import { getFeatureId } from '../src/utils/get-feature-id'; +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { TeritorioCluster } from '../src/teritorio-cluster' +import { getFeatureId } from '../src/utils/get-feature-id' vi.mock('../src/utils/get-feature-id', () => ({ getFeatureId: vi.fn(), -})); +})) describe('setSelectedFeature', () => { let map: MapGL let teritorioCluster: TeritorioCluster beforeEach(() => { - map = new MapGL({ container: 'map'}) + map = new MapGL({ container: 'map' }) teritorioCluster = new TeritorioCluster(map, 'sourceId') }) @@ -20,30 +20,30 @@ describe('setSelectedFeature', () => { const feature = { type: 'Feature', geometry: { type: 'Point', coordinates: [10, 20] }, - properties: null + properties: null, } satisfies GeoJSON.Feature vi.mocked(getFeatureId).mockReturnValue('some-unique-id') - teritorioCluster.setSelectedFeature(feature); + teritorioCluster.setSelectedFeature(feature) - expect(teritorioCluster.selectedFeatureId).toBe('some-unique-id'); - expect(teritorioCluster.pinMarker?.setLngLat).toHaveBeenCalledWith([10, 20]); - expect(teritorioCluster.pinMarker?.addTo).toHaveBeenCalledWith(map); - }); + expect(teritorioCluster.selectedFeatureId).toBe('some-unique-id') + expect(teritorioCluster.pinMarker?.setLngLat).toHaveBeenCalledWith([10, 20]) + expect(teritorioCluster.pinMarker?.addTo).toHaveBeenCalledWith(map) + }) it('should log an error if feature is not a Point and not found', () => { const feature = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[[]]] }, - properties: null + properties: null, } satisfies GeoJSON.Feature - const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); - + const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) + vi.mocked(getFeatureId).mockReturnValue('some-unique-id') - teritorioCluster.setSelectedFeature(feature); + teritorioCluster.setSelectedFeature(feature) - expect(consoleErrorSpy).toHaveBeenCalledWith('Feature some-unique-id is not of type \'Point\', and is not supported.'); - }); -}) \ No newline at end of file + expect(consoleErrorSpy).toHaveBeenCalledWith('Feature some-unique-id is not of type \'Point\', and is not supported.') + }) +})