-
-
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.
fix(amapPolygonPath2GeoJSONCoords): 传空数组时抛异常
- Loading branch information
Showing
4 changed files
with
199 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
isLatLng, | ||
isLatLngArr, | ||
} from '../amapHelper'; | ||
|
||
describe('isLatLng', () => { | ||
test('should return true for valid LngLat object', () => { | ||
const input = { lat: 123, lng: 456 }; | ||
expect(isLatLng(input)).toBe(true); | ||
}); | ||
|
||
test('should return false for invalid LngLat object', () => { | ||
const input = { lat: 123 }; | ||
expect(isLatLng(input)).toBe(false); | ||
}); | ||
|
||
test('should return false for non-LngLat object', () => { | ||
const input = { foo: 'bar' }; | ||
expect(isLatLng(input)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isLatLngArr', () => { | ||
test('returns true for an empty array', () => { | ||
expect(isLatLngArr([])).toBe(true); | ||
}); | ||
|
||
test('returns true for an array with valid LngLat objects', () => { | ||
const validArray = [ | ||
{ lat: 123, lng: 456 }, | ||
]; | ||
expect(isLatLngArr(validArray)).toBe(true); | ||
}); | ||
|
||
test('returns false for an array with invalid objects', () => { | ||
const invalidArray = [ | ||
{ lat: 1, lng: 1 }, | ||
{ foo: 'bar' }, // Invalid object | ||
]; | ||
expect(isLatLngArr(invalidArray)).toBe(false); | ||
}); | ||
|
||
// Add more test cases as needed | ||
}); |
137 changes: 137 additions & 0 deletions
137
src/helpers/__tests__/amapPolygonPath2GeoJSONCoords.test.ts
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,137 @@ | ||
import amapPolygonPath2GeoJSONCoords from '../amapPolygonPath2GeoJSONCoords'; | ||
|
||
// mock LngLat class | ||
class LngLat { | ||
public lat: number; | ||
|
||
public lng: number; | ||
|
||
constructor(lng: number, lat: number) { | ||
this.lat = lat; | ||
this.lng = lng; | ||
} | ||
|
||
public toArray() { | ||
return [this.lng, this.lat]; | ||
} | ||
} | ||
|
||
// mock AMap api | ||
const AMap = { | ||
LngLat, | ||
}; | ||
|
||
describe('amapPolygonPath2GeoJSONCoords', () => { | ||
test('should convert AMap polygon path to GeoJSON coordinates', () => { | ||
const amapPath: ReturnType<AMap.Polygon['getPath']> = [ | ||
new AMap.LngLat(1, 2), | ||
new AMap.LngLat(3, 4), | ||
new AMap.LngLat(5, 6), | ||
]; | ||
const expectedCoords: typeof amapPath = [ | ||
[1, 2], | ||
[3, 4], | ||
[5, 6], | ||
[1, 2], | ||
]; | ||
const result = amapPolygonPath2GeoJSONCoords(amapPath); | ||
expect(result).toEqual(expectedCoords); | ||
}); | ||
|
||
test('should convert correctly if it is a Polygon that has a hole', () => { | ||
const amapPolygonWithHolePath: ReturnType<AMap.Polygon['getPath']>[] = [ | ||
// outer-line | ||
[ | ||
new AMap.LngLat(0, 0), | ||
new AMap.LngLat(0, 10), | ||
new AMap.LngLat(10, 0), | ||
], | ||
// inner-line | ||
[ | ||
new AMap.LngLat(1, 1), | ||
new AMap.LngLat(1, 2), | ||
new AMap.LngLat(2, 1), | ||
], | ||
]; | ||
const expectedCoords: typeof amapPolygonWithHolePath = [ | ||
[ | ||
[0, 0], | ||
[0, 10], | ||
[10, 0], | ||
[0, 0], | ||
], | ||
[ | ||
[1, 1], | ||
[1, 2], | ||
[2, 1], | ||
[1, 1], | ||
], | ||
]; | ||
const result = amapPolygonPath2GeoJSONCoords(amapPolygonWithHolePath); | ||
expect(result).toEqual(expectedCoords); | ||
}); | ||
|
||
test('should convert correctly if it is a MultiPolygon that has a hole', () => { | ||
const amapMultiPolygonPath: ReturnType<AMap.Polygon['getPath']>[] = [ | ||
// polygon 1 | ||
[ | ||
[ | ||
new AMap.LngLat(0, 0), | ||
new AMap.LngLat(0, 10), | ||
new AMap.LngLat(10, 0), | ||
], | ||
[ | ||
new AMap.LngLat(1, 1), | ||
new AMap.LngLat(1, 2), | ||
new AMap.LngLat(2, 1), | ||
], | ||
], | ||
// polygon 2 | ||
[ | ||
[ | ||
new AMap.LngLat(0, 0), | ||
new AMap.LngLat(0, 10), | ||
new AMap.LngLat(10, 0), | ||
], | ||
[ | ||
new AMap.LngLat(1, 1), | ||
new AMap.LngLat(1, 2), | ||
new AMap.LngLat(2, 1), | ||
], | ||
], | ||
|
||
]; | ||
const expectedCoords: typeof amapMultiPolygonPath = [ | ||
// polygon 1 | ||
[ | ||
[ | ||
[0, 0], [0, 10], [10, 0], [0, 0], | ||
], | ||
[ | ||
[1, 1], [1, 2], [2, 1], [1, 1], | ||
], | ||
], | ||
// polygon 2 | ||
[ | ||
[ | ||
[0, 0], [0, 10], [10, 0], [0, 0], | ||
], | ||
[ | ||
[1, 1], [1, 2], [2, 1], [1, 1], | ||
], | ||
], | ||
]; | ||
const result = amapPolygonPath2GeoJSONCoords(amapMultiPolygonPath); | ||
expect(result).toEqual(expectedCoords); | ||
}); | ||
|
||
test('should handle empty polygon path', () => { | ||
const emptyPath: ReturnType<AMap.Polygon['getPath']> = []; | ||
const result = amapPolygonPath2GeoJSONCoords(emptyPath); | ||
expect(result).toEqual(emptyPath); | ||
}); | ||
|
||
test('should throw an error if the input is not an array', () => { | ||
expect(() => amapPolygonPath2GeoJSONCoords(undefined)).toThrowError('invalid path'); | ||
}); | ||
}); |
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,3 @@ | ||
export const isLatLng = (p: any): p is AMap.LngLat => !!('lat' in p && 'lng' in p); | ||
|
||
export const isLatLngArr = (p: any): p is AMap.LngLat[] => p.every(isLatLng); |
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