53
tests/unit/WechatOpenLocationService.spec.ts
Normal file
53
tests/unit/WechatOpenLocationService.spec.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
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'
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user