Skip to content

Commit

Permalink
chore(lint): apply lint rules #30
Browse files Browse the repository at this point in the history
  • Loading branch information
wazolab committed Nov 21, 2024
1 parent 57509ed commit 1c8c0c4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 67 deletions.
28 changes: 15 additions & 13 deletions src/teritorio-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 5 additions & 4 deletions src/utils/get-feature-id.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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'
}
}
}
64 changes: 32 additions & 32 deletions tests/get-feature-id.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -8,82 +8,82 @@ 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 = {
type: 'Feature',
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')
})
})
36 changes: 18 additions & 18 deletions tests/set-selected-feature.test.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
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')
})

it('should render pin marker when feature is not found and is a Point', () => {
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.');
});
})
expect(consoleErrorSpy).toHaveBeenCalledWith('Feature some-unique-id is not of type \'Point\', and is not supported.')
})
})

0 comments on commit 1c8c0c4

Please sign in to comment.