Skip to content

Commit

Permalink
fix(Fixes geocoder issue where it tries to assign a spatial reference…
Browse files Browse the repository at this point in the history
… to a null extent): implemented

Implemented a truthy check for an address candidate extent prior to assigning a spatial reference to
the extent object. Also updated tests to account for this change, as well as updated typescrypt
interfaces to work  with the candidate data.

AFFECTS PACKAGES:
@esri/arcgis-rest-geocoder

ISSUES CLOSED: #376
  • Loading branch information
raykendo committed Oct 31, 2018
1 parent afb8274 commit bfad977
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
9 changes: 6 additions & 3 deletions packages/arcgis-rest-geocoder/src/geocode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export interface IGeocodeResponse {
candidates: Array<{
address: string;
location: IPoint;
extent: IExtent;
extent?: IExtent;
score: number;
attributes: object;
}>;
}
Expand Down Expand Up @@ -112,10 +113,12 @@ export function geocode(
const sr = response.spatialReference;
response.candidates.forEach(function(candidate: {
location: IPoint;
extent: IExtent;
extent?: IExtent;
}) {
candidate.location.spatialReference = sr;
candidate.extent.spatialReference = sr;
if (candidate.extent) {
candidate.extent.spatialReference = sr;
}
});
return response;
}
Expand Down
25 changes: 24 additions & 1 deletion packages/arcgis-rest-geocoder/test/geocode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { geocode } from "../src/geocode";

import * as fetchMock from "fetch-mock";

import { FindAddressCandidates } from "./mocks/responses";
import { FindAddressCandidates, FindAddressCandidatesNullExtent } from "./mocks/responses";

const customGeocoderUrl =
"https://foo.com/arcgis/rest/services/Custom/GeocodeServer/";
Expand Down Expand Up @@ -145,4 +145,27 @@ describe("geocode", () => {
fail(e);
});
});

it("should handle geocoders that return null extents for location candidates", done => {
fetchMock.once("*", FindAddressCandidatesNullExtent);

geocode("LAX")
.then(response => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain("singleLine=LAX");
// the only property this lib tacks on
expect(response.spatialReference.wkid).toEqual(4326);
expect(response.candidates.every( candidate => candidate.extent == null ));
done();
})
.catch(e => {
fail(e);
});
});
});
43 changes: 42 additions & 1 deletion packages/arcgis-rest-geocoder/test/mocks/responses.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

export const FindAddressCandidates = {
import { IGeocodeResponse } from "../../src/geocode";

export const FindAddressCandidates : IGeocodeResponse = {
spatialReference: {
wkid: 4326,
latestWkid: 4326
Expand Down Expand Up @@ -55,6 +57,45 @@ export const FindAddressCandidates = {
]
};

export const FindAddressCandidatesNullExtent : IGeocodeResponse = {
spatialReference: {
wkid: 4326,
latestWkid: 4326
},
candidates: [
{
address: "LAX",
location: {
x: -118.40896999999995,
y: 33.942510000000027
},
score: 100,
attributes: {},
extent: null
},
{
address: "LAX",
location: {
x: -118.39223999999996,
y: 33.945610000000045
},
score: 100,
attributes: {},
extent: null
},
{
address: "Lax, Georgia",
location: {
x: -83.121539999999982,
y: 31.473250000000064
},
score: 100,
attributes: {},
extent: null
}
]
};

export const Suggest = {
suggestions: [
{
Expand Down

0 comments on commit bfad977

Please sign in to comment.