@@ -3,6 +3,7 @@ import type { Position } from 'geojson'
33
44import type { ReverseGeocodingFeature , ReverseGeocodingResponse } from './api-types'
55import { getFullAddress , getPlaceName , reverseGeocode } from './geocode'
6+ import { MAPBOX_TOKEN } from './config'
67
78const fetchMock = vi . fn ( )
89
@@ -78,6 +79,24 @@ describe('reverseGeocode', () => {
7879 expect ( fetchMock ) . toHaveBeenCalledOnce ( )
7980 } )
8081
82+ test ( 'request includes expected Mapbox URL, params, and cache mode' , async ( ) => {
83+ const position : Position = [ - 0.10664 , 51.514209 ]
84+ const feature = createFeature ( '133 Fleet Street, City of London, London, EC4A 2BB, United Kingdom' )
85+ mockReverseGeocode ( new Map ( [ [ coordinateKey ( position ) , feature ] ] ) )
86+
87+ await reverseGeocode ( position )
88+
89+ expect ( fetchMock ) . toHaveBeenCalledOnce ( )
90+ const [ input , init ] = fetchMock . mock . calls [ 0 ] as [ string | URL | Request , RequestInit | undefined ]
91+ const rawUrl = typeof input === 'string' ? input : input instanceof URL ? input . toString ( ) : input . url
92+ const url = new URL ( rawUrl )
93+ expect ( `${ url . origin } ${ url . pathname } ` ) . toBe ( 'https://api.mapbox.com/search/geocode/v6/reverse' )
94+ expect ( url . searchParams . get ( 'longitude' ) ) . toBe ( '-0.106640' )
95+ expect ( url . searchParams . get ( 'latitude' ) ) . toBe ( '51.514209' )
96+ expect ( url . searchParams . get ( 'access_token' ) ) . toBe ( MAPBOX_TOKEN )
97+ expect ( init ) . toMatchObject ( { cache : 'force-cache' } )
98+ } )
99+
81100 test ( 'return null when fetch rejects' , async ( ) => {
82101 vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
83102 fetchMock . mockRejectedValueOnce ( new Error ( 'network down' ) )
0 commit comments