Fix uncaught TypeError in "Show on map" operation. (#2453)
This commit is contained in:
parent
0d2af8ce00
commit
a0a369a7ef
@ -71,6 +71,16 @@ class ShowOnMap extends Operation {
|
|||||||
}
|
}
|
||||||
latLong = latLong.replace(/[,]$/, "");
|
latLong = latLong.replace(/[,]$/, "");
|
||||||
latLong = latLong.replace(/°/g, "");
|
latLong = latLong.replace(/°/g, "");
|
||||||
|
|
||||||
|
// The map requires a latitude and longitude pair. If the conversion only produced a
|
||||||
|
// single value (e.g. because the chosen input delimiter didn't match the input), bail
|
||||||
|
// out with a helpful message rather than passing it on to the map, which would throw an
|
||||||
|
// uncaught TypeError in the browser.
|
||||||
|
const coords = latLong.split(",").map(v => v.trim());
|
||||||
|
if (coords.length !== 2 || coords.some(v => v === "" || isNaN(Number(v)))) {
|
||||||
|
throw new OperationError(`Could not show coordinates '${latLong}' on the map. Expected a latitude and longitude pair - check that the input format and delimiter are correct.`);
|
||||||
|
}
|
||||||
|
|
||||||
return latLong;
|
return latLong;
|
||||||
}
|
}
|
||||||
return input;
|
return input;
|
||||||
|
|||||||
39
tests/operations/tests/ShowOnMap.mjs
Normal file
39
tests/operations/tests/ShowOnMap.mjs
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* Show on map tests
|
||||||
|
*
|
||||||
|
* @author Leon Zandman [leon@wirwar.com]
|
||||||
|
*
|
||||||
|
* @copyright Crown Copyright 2026
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
import TestRegister from "../../lib/TestRegister.mjs";
|
||||||
|
|
||||||
|
TestRegister.addTests([
|
||||||
|
{
|
||||||
|
name: "Show on map: valid coordinate pair",
|
||||||
|
input: "51.5007, -0.1246",
|
||||||
|
// The presented output is the Leaflet map HTML; just check the coordinates made it through.
|
||||||
|
expectedMatch: /51\.5007,-0\.1246/,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Show on map",
|
||||||
|
args: [13, "Auto", "Auto"]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// Regression test: a comma-separated input with the delimiter set to "\n" used to be
|
||||||
|
// mis-detected as a single Degrees Decimal Minutes value (1° 24' = 1.4°), producing a single
|
||||||
|
// coordinate. That single value was then passed to Leaflet's setView([1.4], ...), throwing
|
||||||
|
// an uncaught "Cannot read properties of null (reading 'lat')" TypeError in the browser.
|
||||||
|
name: "Show on map: single value is rejected with a helpful error",
|
||||||
|
input: "1, 24",
|
||||||
|
expectedOutput: "Could not show coordinates '1.4' on the map. Expected a latitude and longitude pair - check that the input format and delimiter are correct.",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Show on map",
|
||||||
|
args: [13, "Auto", "\\n"]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]);
|
||||||
Loading…
x
Reference in New Issue
Block a user