Skip to content

Commit

Permalink
Merge pull request #326 from Esri/geocoder-tests
Browse files Browse the repository at this point in the history
chore: break geocoder tests out into individual files
  • Loading branch information
jgravois authored Sep 18, 2018
2 parents 03d3418 + 3fc9eab commit c85a677
Show file tree
Hide file tree
Showing 6 changed files with 550 additions and 513 deletions.
113 changes: 113 additions & 0 deletions packages/arcgis-rest-geocoder/test/bulk.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { bulkGeocode } from "../src/bulk";

import * as fetchMock from "fetch-mock";

import { GeocodeAddresses } from "./mocks/responses";

const addresses = [
{
OBJECTID: 1,
SingleLine: "380 New York St. Redlands 92373"
},
{
OBJECTID: 2,
SingleLine: "1 World Way Los Angeles 90045"
}
];

describe("geocode", () => {
afterEach(fetchMock.restore);

it("should make a bulk geocoding request", done => {
fetchMock.once("*", GeocodeAddresses);

const MOCK_AUTH = {
getToken() {
return Promise.resolve("token");
},
portal: "https://mapsdev.arcgis.com"
};

bulkGeocode({ addresses, authentication: MOCK_AUTH })
.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/geocodeAddresses"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain(
`addresses=${encodeURIComponent(
'{"records":[{"attributes":{"OBJECTID":1,"SingleLine":"380 New York St. Redlands 92373"}},{"attributes":{"OBJECTID":2,"SingleLine":"1 World Way Los Angeles 90045"}}]}'
)}`
);
expect(options.body).toContain("token=token");
expect(response.spatialReference.latestWkid).toEqual(4326);
expect(response.locations[0].address).toEqual(
"380 New York St, Redlands, California, 92373"
);
expect(response.locations[0].location.x).toEqual(-117.19567031799994);
// the only property this lib tacks on
expect(response.locations[0].location.spatialReference.wkid).toEqual(
4326
);
done();
})
.catch(e => {
fail(e);
});
});

it("should throw an error when a bulk geocoding request is made without a token", done => {
fetchMock.once("*", GeocodeAddresses);

bulkGeocode({ addresses })
// tslint:disable-next-line
.catch(e => {
expect(e).toEqual(
"bulk geocoding using the ArcGIS service requires authentication"
);
done();
});
});

it("should send a bulk geocoding request to a custom url without a token", done => {
fetchMock.once("*", GeocodeAddresses);

bulkGeocode({
addresses,
endpoint:
"https://customer.gov/arcgis/rest/services/CompositeGeocoder/GeocodeServer/"
})
// tslint:disable-next-line
.then(response => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://customer.gov/arcgis/rest/services/CompositeGeocoder/GeocodeServer/geocodeAddresses"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain(
`addresses=${encodeURIComponent(
'{"records":[{"attributes":{"OBJECTID":1,"SingleLine":"380 New York St. Redlands 92373"}},{"attributes":{"OBJECTID":2,"SingleLine":"1 World Way Los Angeles 90045"}}]}'
)}`
);
// expect(options.body).toContain("token=token");
expect(response.spatialReference.latestWkid).toEqual(4326);
expect(response.locations[0].address).toEqual(
"380 New York St, Redlands, California, 92373"
);
expect(response.locations[0].location.x).toEqual(-117.19567031799994);
// the only property this lib tacks on
expect(response.locations[0].location.spatialReference.wkid).toEqual(
4326
);
done();
});
});
});
121 changes: 121 additions & 0 deletions packages/arcgis-rest-geocoder/test/geocode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { geocode } from "../src/geocode";

import * as fetchMock from "fetch-mock";

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

const customGeocoderUrl =
"https://foo.com/arcgis/rest/services/Custom/GeocodeServer/";

describe("geocode", () => {
afterEach(fetchMock.restore);

it("should make a simple, single geocoding request", done => {
fetchMock.once("*", FindAddressCandidates);

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);
done();
})
.catch(e => {
fail(e);
});
});

it("should make a simple, single geocoding request with a custom parameter", done => {
fetchMock.once("*", FindAddressCandidates);

geocode({ params: { singleLine: "LAX", countryCode: "USA" } })
.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");
expect(options.body).toContain("countryCode=USA");
// the only property this lib tacks on
expect(response.spatialReference.wkid).toEqual(4326);
done();
})
.catch(e => {
fail(e);
});
});

it("should make a single geocoding request to a custom geocoding service", done => {
fetchMock.once("*", FindAddressCandidates);

geocode({
endpoint: customGeocoderUrl,
params: {
outSr: 3857,
address: "380 New York St",
postal: 92373
}
})
.then(response => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://foo.com/arcgis/rest/services/Custom/GeocodeServer/findAddressCandidates"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain(
`address=${encodeURIComponent("380 New York St")}`
);
expect(options.body).toContain("postal=92373");
expect(options.body).toContain("outSr=3857");
// the only property this lib tacks on
expect(response.spatialReference.wkid).toEqual(4326);
done();
})
.catch(e => {
fail(e);
});
});

it("should pass through all requestOptions when making a geocoding request", done => {
fetchMock.once("*", FindAddressCandidates);

geocode({
endpoint: customGeocoderUrl,
params: {
outSr: 3857,
address: "380 New York St",
postal: 92373
},
httpMethod: "GET"
})
.then(response => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://foo.com/arcgis/rest/services/Custom/GeocodeServer/findAddressCandidates?f=json&outSr=3857&address=380%20New%20York%20St&postal=92373"
);
expect(options.method).toBe("GET");
// the only property this lib tacks on
expect(response.spatialReference.wkid).toEqual(4326);
done();
})
.catch(e => {
fail(e);
});
});
});
Loading

0 comments on commit c85a677

Please sign in to comment.