@@ -5,19 +5,36 @@ import nock from 'nock'
55import { autocomplete , bulk , reverse , search } from '../index'
66
77const mockBulkResult = require ( './mock-bulk-result.json' )
8+ const mockBulkResultWithBadAddress = require ( './mock-bulk-result-with-bad-address.json' )
89const mockReverseResult = require ( './mock-reverse-result.json' )
910const mockSearchResult = require ( './mock-search-result.json' )
1011const mockSuggestResult = require ( './mock-suggest-result.json' )
1112
13+ const ARCGIS_GEOCODE_URL = 'https://geocode.arcgis.com/'
14+ const BULK_URL = / a r c g i s \/ r e s t \/ s e r v i c e s \/ W o r l d \/ G e o c o d e S e r v e r \/ g e o c o d e A d d r e s s e s /
15+ const REVERSE_URL = / a r c g i s \/ r e s t \/ s e r v i c e s \/ W o r l d \/ G e o c o d e S e r v e r \/ r e v e r s e G e o c o d e /
16+ const SEARCH_URL = / a r c g i s \/ r e s t \/ s e r v i c e s \/ W o r l d \/ G e o c o d e S e r v e r \/ f i n d A d d r e s s C a n d i d a t e s /
17+ const SUGGEST_URL = / a r c g i s \/ r e s t \/ s e r v i c e s \/ W o r l d \/ G e o c o d e S e r v e r \/ s u g g e s t /
18+
19+ function nockArcGet ( url : RegExp ) {
20+ return nock ( ARCGIS_GEOCODE_URL ) . get ( url )
21+ }
22+
23+ function nockArcPost ( url : RegExp ) {
24+ return nock ( ARCGIS_GEOCODE_URL ) . post ( url )
25+ }
26+
27+ function snapshotUri ( type : string , response : Object ) {
28+ return ( uri , requestBody ) => {
29+ expect ( uri ) . toMatchSnapshot ( `basic ${ type } request uri` )
30+ return response
31+ }
32+ }
33+
1234describe ( 'geocoder-arcgis-geojson' , ( ) => {
1335 describe ( 'autocomplete' , ( ) => {
1436 it ( 'should make basic autocomplete query' , async ( ) => {
15- nock ( 'https://geocode.arcgis.com/' )
16- . get ( / a r c g i s \/ r e s t \/ s e r v i c e s \/ W o r l d \/ G e o c o d e S e r v e r \/ s u g g e s t / )
17- . reply ( 200 , ( uri , requestBody ) => {
18- expect ( uri ) . toMatchSnapshot ( 'basic autocomplete request uri' )
19- return mockSuggestResult
20- } )
37+ nockArcGet ( SUGGEST_URL ) . reply ( 200 , snapshotUri ( 'autocomplete' , mockSuggestResult ) )
2138
2239 const result = await autocomplete ( {
2340 text : '123 main st'
@@ -27,19 +44,15 @@ describe('geocoder-arcgis-geojson', () => {
2744 } )
2845
2946 describe ( 'bulk' , ( ) => {
30- it ( 'should make basic bulk query' , async ( ) => {
47+ beforeEach ( ( ) => {
3148 // nock for auth token
3249 nock ( 'https://www.arcgis.com/' )
3350 . post ( / s h a r i n g \/ o a u t h 2 \/ t o k e n / )
3451 . reply ( 200 , { access_token : 'test' , expires_in : 86400 } )
52+ } )
3553
36- // nock for bulk geocode reqeust
37- nock ( 'https://geocode.arcgis.com/' )
38- . post ( / a r c g i s \/ r e s t \/ s e r v i c e s \/ W o r l d \/ G e o c o d e S e r v e r \/ g e o c o d e A d d r e s s e s / )
39- . reply ( 200 , ( uri , requestBody ) => {
40- expect ( uri ) . toMatchSnapshot ( 'basic bulk request uri' )
41- return mockBulkResult
42- } )
54+ it ( 'should make basic bulk query' , async ( ) => {
55+ nockArcPost ( BULK_URL ) . reply ( 200 , mockBulkResult )
4356
4457 const result = await bulk ( {
4558 addresses : [ '123 main st' ] ,
@@ -48,16 +61,22 @@ describe('geocoder-arcgis-geojson', () => {
4861 } )
4962 expect ( result ) . toMatchSnapshot ( 'basic bulk g-a-g response' )
5063 } )
64+
65+ it ( 'should handle bulk query resulting in no address found' , async ( ) => {
66+ nockArcPost ( BULK_URL ) . reply ( 200 , mockBulkResultWithBadAddress )
67+
68+ const result = await bulk ( {
69+ addresses : [ 'aefgjil' ] ,
70+ clientId : 'test' ,
71+ clientSecret : 'test'
72+ } )
73+ expect ( result ) . toMatchSnapshot ( 'bulk g-a-g response with no address found' )
74+ } )
5175 } )
5276
5377 describe ( 'reverse' , ( ) => {
5478 it ( 'should make basic reverse query' , async ( ) => {
55- nock ( 'https://geocode.arcgis.com/' )
56- . get ( / a r c g i s \/ r e s t \/ s e r v i c e s \/ W o r l d \/ G e o c o d e S e r v e r \/ r e v e r s e G e o c o d e / )
57- . reply ( 200 , ( uri , requestBody ) => {
58- expect ( uri ) . toMatchSnapshot ( 'basic reverse request uri' )
59- return mockReverseResult
60- } )
79+ nockArcGet ( REVERSE_URL ) . reply ( 200 , snapshotUri ( 'reverse' , mockReverseResult ) )
6180
6281 const result = await reverse ( {
6382 point : {
@@ -71,12 +90,7 @@ describe('geocoder-arcgis-geojson', () => {
7190
7291 describe ( 'search' , ( ) => {
7392 it ( 'should make basic search query' , async ( ) => {
74- nock ( 'https://geocode.arcgis.com/' )
75- . get ( / a r c g i s \/ r e s t \/ s e r v i c e s \/ W o r l d \/ G e o c o d e S e r v e r \/ f i n d A d d r e s s C a n d i d a t e s / )
76- . reply ( 200 , ( uri , requestBody ) => {
77- expect ( uri ) . toMatchSnapshot ( 'basic search request uri' )
78- return mockSearchResult
79- } )
93+ nockArcGet ( SEARCH_URL ) . reply ( 200 , snapshotUri ( 'search' , mockSearchResult ) )
8094
8195 const result = await search ( {
8296 text : '123 main st'
0 commit comments