Files
frontend-miniapp/tests/unit/WechatOpenLocationService.spec.ts
lyf f9fefa11ef
Some checks failed
CI / verify (push) Has been cancelled
修复小程序内来馆地图导航
2026-07-11 17:24:45 +08:00

54 lines
1.5 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { openWechatHostLocation } from '@/services/WechatOpenLocationService'
const target = {
latitude: 22.604321,
longitude: 114.058765,
name: '深圳自然博物馆',
address: '深圳市龙华区民治街道'
}
beforeEach(() => {
vi.useFakeTimers()
})
afterEach(() => {
vi.useRealTimers()
vi.unstubAllGlobals()
})
describe('宿主微信地图能力', () => {
it('向 uni.openLocation 透传完整位置参数', async () => {
const openLocation = vi.fn((options: { success?: () => void }) => options.success?.())
vi.stubGlobal('uni', { openLocation })
await expect(openWechatHostLocation(target)).resolves.toEqual({ status: 'opened' })
expect(openLocation).toHaveBeenCalledWith(expect.objectContaining({
...target,
scale: 18
}))
})
it('宿主能力无回调时按超时失败并释放调用', async () => {
vi.stubGlobal('uni', { openLocation: vi.fn() })
const result = openWechatHostLocation(target)
await vi.advanceTimersByTimeAsync(5100)
await expect(result).resolves.toEqual({ status: 'failed', reason: 'timeout' })
})
it('宿主能力同步抛错时返回可处理的失败结果', async () => {
vi.stubGlobal('uni', {
openLocation: vi.fn(() => {
throw new Error('openLocation unavailable')
})
})
await expect(openWechatHostLocation(target)).resolves.toEqual({
status: 'failed',
reason: 'api-failed'
})
})
})