-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathbulk.test.ts
234 lines (218 loc) · 8.19 KB
/
bulk.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import fetchMock from "fetch-mock";
import { bulkGeocode } from "../src/bulk.js";
import { GeocodeAddresses } from "./mocks/responses.js";
const addresses = [
{
OBJECTID: 1,
SingleLine: "380 New York St. Redlands 92373"
},
{
OBJECTID: 2,
SingleLine: "1 World Way Los Angeles 90045"
},
{
OBJECTID: 3,
SingleLine: "foo bar baz"
}
];
const MOCK_AUTH = {
token: "token",
getToken() {
return Promise.resolve("token");
},
portal: "https://mapsdev.arcgis.com"
};
describe("geocode", () => {
afterEach(() => {
fetchMock.restore();
});
it("should make a bulk geocoding request, even with an unmatchable record", (done) => {
fetchMock.once("*", GeocodeAddresses);
bulkGeocode({ addresses, authentication: MOCK_AUTH })
.then((response) => {
expect(fetchMock.called()).toEqual(true);
const [url, options] = 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"}},{"attributes":{"OBJECTID":3,"SingleLine":"foo bar baz"}}]}'
)}`
);
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
);
expect(response.locations[2].score).toEqual(0);
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] = 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"}},{"attributes":{"OBJECTID":3,"SingleLine":"foo bar baz"}}]}'
)}`
);
// 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
);
expect(response.locations[2].address).toEqual("foo bar baz");
done();
});
});
it("should send a bulk geocoding request with params correctly", (done) => {
fetchMock.once("*", GeocodeAddresses);
bulkGeocode({
addresses,
endpoint:
"https://customer.gov/arcgis/rest/services/CompositeGeocoder/GeocodeServer/",
params: {
outSR: 4326,
forStorage: true
}
})
// tslint:disable-next-line
.then((response) => {
expect(fetchMock.called()).toEqual(true);
const [url, options] = 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("outSR=4326");
expect(options.body).toContain("forStorage=true");
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"}},{"attributes":{"OBJECTID":3,"SingleLine":"foo bar baz"}}]}'
)}`
);
// 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
);
expect(response.locations[2].address).toEqual("foo bar baz");
done();
});
});
it("should send a bulk geocoding request with postal params correctly", (done) => {
fetchMock.once("*", GeocodeAddresses);
const addresses = [
{
OBJECTID: 1,
address: "380 New York St. Redlands",
postal: 92373
},
{
OBJECTID: 2,
address: "1205 Williston Rd",
postal: "05403"
}
];
bulkGeocode({
addresses,
endpoint:
"https://customer.gov/arcgis/rest/services/CompositeGeocoder/GeocodeServer/",
params: {
outSR: 4326,
forStorage: true
}
})
// tslint:disable-next-line
.then((response) => {
expect(fetchMock.called()).toEqual(true);
const [url, options] = 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("outSR=4326");
expect(options.body).toContain("forStorage=true");
expect(options.body).toContain(
`addresses=${encodeURIComponent(
'{"records":[{"attributes":{"OBJECTID":1,"address":"380 New York St. Redlands","postal":92373}},{"attributes":{"OBJECTID":2,"address":"1205 Williston Rd","postal":"05403"}}]}'
)}`
);
expect(response.spatialReference.latestWkid).toEqual(4326);
done();
});
});
it("should support rawResponse", (done) => {
fetchMock.once("*", GeocodeAddresses);
bulkGeocode({ addresses, authentication: MOCK_AUTH, rawResponse: true })
.then((response: any) => {
expect(fetchMock.called()).toEqual(true);
const [url, options] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/geocodeAddresses"
);
expect(options.method).toBe("POST");
expect(response.status).toBe(200);
expect(response.ok).toBe(true);
expect(response.body.Readable).not.toBe(null);
response.json().then((raw: any) => {
expect(raw).toEqual(GeocodeAddresses);
done();
});
// this used to work with isomorphic-fetch
// expect(response instanceof Response).toBe(true);
})
.catch((e) => {
fail(e);
});
});
});