-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
136 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export const getFeatureId = (feature: GeoJSON.Feature): string => { | ||
try { | ||
if (!feature.properties) { | ||
throw new Error('Feature properties are null or undefined') | ||
} | ||
|
||
if (feature.properties.cluster) { | ||
if (!feature.id) { | ||
throw new Error('Feature is a cluster but is missing property id') | ||
} | ||
return feature.id.toString() | ||
} | ||
|
||
// Vido support: shouldn't be part of this plugin | ||
if (!feature.properties.metadata || typeof feature.properties.metadata !== 'string') { | ||
if (!feature.properties.id) { | ||
throw new Error('Feature property id is missing') | ||
} | ||
return feature.properties.id.toString() | ||
} | ||
|
||
const metadata = JSON.parse(feature.properties.metadata) | ||
if (!metadata.id) { | ||
throw new Error('Feature properties metadata id is missing') | ||
} | ||
|
||
return metadata.id.toString() | ||
} catch (error) { | ||
console.error("Error in getFeatureId: ", error); | ||
|
||
return feature.id?.toString() || 'unknown' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { describe, it, expect, vi } from 'vitest'; | ||
import { getFeatureId } from '../src/utils/get-feature-id' | ||
|
||
describe('getFeatureId function', () => { | ||
it('should return the feature id for clusters', () => { | ||
const feature: GeoJSON.Feature = { | ||
type: 'Feature', | ||
properties: { cluster: true }, | ||
id: 123, | ||
geometry: { type: 'Point', coordinates: [0, 0] }, | ||
}; | ||
|
||
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'); | ||
}); | ||
|
||
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'); | ||
}); | ||
|
||
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'); | ||
}); | ||
|
||
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'); | ||
}); | ||
|
||
it('should log an error when an exception occurs', () => { | ||
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => { }); | ||
|
||
const feature: GeoJSON.Feature = { | ||
type: 'Feature', | ||
properties: {}, | ||
geometry: { type: 'Point', coordinates: [3, 3] }, | ||
}; | ||
|
||
getFeatureId(feature); | ||
|
||
expect(consoleErrorSpy).toHaveBeenCalled() | ||
expect(consoleErrorSpy.mock.calls[0][0]).toContain('Error in getFeatureId:'); | ||
|
||
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'); | ||
}); | ||
}); |