From bfad9774063c1d5875468136e95f803b4d5dbd65 Mon Sep 17 00:00:00 2001 From: Ken Doman Date: Wed, 31 Oct 2018 01:15:40 -0500 Subject: [PATCH] fix(Fixes geocoder issue where it tries to assign a spatial reference 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 --- packages/arcgis-rest-geocoder/src/geocode.ts | 9 ++-- .../arcgis-rest-geocoder/test/geocode.test.ts | 25 ++++++++++- .../test/mocks/responses.ts | 43 ++++++++++++++++++- 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/packages/arcgis-rest-geocoder/src/geocode.ts b/packages/arcgis-rest-geocoder/src/geocode.ts index d68e071884..6cd75e8e0e 100644 --- a/packages/arcgis-rest-geocoder/src/geocode.ts +++ b/packages/arcgis-rest-geocoder/src/geocode.ts @@ -57,7 +57,8 @@ export interface IGeocodeResponse { candidates: Array<{ address: string; location: IPoint; - extent: IExtent; + extent?: IExtent; + score: number; attributes: object; }>; } @@ -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; } diff --git a/packages/arcgis-rest-geocoder/test/geocode.test.ts b/packages/arcgis-rest-geocoder/test/geocode.test.ts index 9964ad1fc3..6a193f5213 100644 --- a/packages/arcgis-rest-geocoder/test/geocode.test.ts +++ b/packages/arcgis-rest-geocoder/test/geocode.test.ts @@ -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/"; @@ -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); + }); + }); }); diff --git a/packages/arcgis-rest-geocoder/test/mocks/responses.ts b/packages/arcgis-rest-geocoder/test/mocks/responses.ts index e05aad2e4e..982e835dc4 100644 --- a/packages/arcgis-rest-geocoder/test/mocks/responses.ts +++ b/packages/arcgis-rest-geocoder/test/mocks/responses.ts @@ -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 @@ -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: [ {