@@ -5,19 +5,36 @@ import nock from 'nock'
5
5
import { autocomplete , bulk , reverse , search } from '../index'
6
6
7
7
const mockBulkResult = require ( './mock-bulk-result.json' )
8
+ const mockBulkResultWithBadAddress = require ( './mock-bulk-result-with-bad-address.json' )
8
9
const mockReverseResult = require ( './mock-reverse-result.json' )
9
10
const mockSearchResult = require ( './mock-search-result.json' )
10
11
const mockSuggestResult = require ( './mock-suggest-result.json' )
11
12
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
+
12
34
describe ( 'geocoder-arcgis-geojson' , ( ) => {
13
35
describe ( 'autocomplete' , ( ) => {
14
36
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 ) )
21
38
22
39
const result = await autocomplete ( {
23
40
text : '123 main st'
@@ -27,19 +44,15 @@ describe('geocoder-arcgis-geojson', () => {
27
44
} )
28
45
29
46
describe ( 'bulk' , ( ) => {
30
- it ( 'should make basic bulk query' , async ( ) => {
47
+ beforeEach ( ( ) => {
31
48
// nock for auth token
32
49
nock ( 'https://www.arcgis.com/' )
33
50
. post ( / s h a r i n g \/ o a u t h 2 \/ t o k e n / )
34
51
. reply ( 200 , { access_token : 'test' , expires_in : 86400 } )
52
+ } )
35
53
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 )
43
56
44
57
const result = await bulk ( {
45
58
addresses : [ '123 main st' ] ,
@@ -48,16 +61,22 @@ describe('geocoder-arcgis-geojson', () => {
48
61
} )
49
62
expect ( result ) . toMatchSnapshot ( 'basic bulk g-a-g response' )
50
63
} )
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
+ } )
51
75
} )
52
76
53
77
describe ( 'reverse' , ( ) => {
54
78
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 ) )
61
80
62
81
const result = await reverse ( {
63
82
point : {
@@ -71,12 +90,7 @@ describe('geocoder-arcgis-geojson', () => {
71
90
72
91
describe ( 'search' , ( ) => {
73
92
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 ) )
80
94
81
95
const result = await search ( {
82
96
text : '123 main st'
0 commit comments