Skip to content

Commit 2dcddc5

Browse files
committed
Add hybrid geocode test coverage
1 parent f91fb93 commit 2dcddc5

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/map/geocode.live.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { describe, expect, test } from 'vitest'
2+
3+
import { getFullAddress, getPlaceName, reverseGeocode } from './geocode'
4+
5+
const describeLive = process.env.RUN_LIVE_MAP_TESTS === '1' ? describe : describe.skip
6+
7+
describeLive('live geocode smoke tests', () => {
8+
test('reverseGeocode returns a feature for a known coordinate', async () => {
9+
expect(await reverseGeocode([-0.10664, 51.514209])).not.toBeNull()
10+
}, 15000)
11+
12+
test('getFullAddress returns a non-empty string', async () => {
13+
const fullAddress = await getFullAddress([-0.10664, 51.514209])
14+
expect(fullAddress).toBeTruthy()
15+
expect(typeof fullAddress).toBe('string')
16+
}, 15000)
17+
18+
test('getPlaceName returns a non-empty string', async () => {
19+
const placeName = await getPlaceName([-117.168638, 32.723695])
20+
expect(placeName).toBeTruthy()
21+
expect(typeof placeName).toBe('string')
22+
}, 15000)
23+
})

src/map/geocode.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Position } from 'geojson'
33

44
import type { ReverseGeocodingFeature, ReverseGeocodingResponse } from './api-types'
55
import { getFullAddress, getPlaceName, reverseGeocode } from './geocode'
6+
import { MAPBOX_TOKEN } from './config'
67

78
const 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

Comments
 (0)