39 lines
1.5 KiB
TypeScript
39 lines
1.5 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import {
|
|
buildWechatOpenLocationPageUrl,
|
|
parseWechatOpenLocationPageOptions,
|
|
validateWechatOpenLocationTarget,
|
|
WECHAT_OPEN_LOCATION_PAGE_PATH
|
|
} from '@/utils/wechatOpenLocationProtocol'
|
|
|
|
describe('微信宿主地图参数协议', () => {
|
|
it('中文及特殊字符编码后可以完整还原', () => {
|
|
const source = {
|
|
latitude: 22.604321,
|
|
longitude: 114.058765,
|
|
name: '深圳自然博物馆 A&B #1',
|
|
address: '龙华区 100% 路口?入口=东&层=1'
|
|
}
|
|
const url = buildWechatOpenLocationPageUrl(source)
|
|
const [path, query = ''] = url.split('?')
|
|
const params = Object.fromEntries(new URLSearchParams(query))
|
|
|
|
expect(path).toBe(WECHAT_OPEN_LOCATION_PAGE_PATH)
|
|
expect(parseWechatOpenLocationPageOptions(params)).toEqual({
|
|
ok: true,
|
|
value: source
|
|
})
|
|
})
|
|
|
|
it.each([
|
|
[{ latitude: '', longitude: 114, name: '博物馆' }, '地图坐标缺失或格式错误'],
|
|
[{ latitude: Number.NaN, longitude: 114, name: '博物馆' }, '地图坐标缺失或格式错误'],
|
|
[{ latitude: 91, longitude: 114, name: '博物馆' }, '地图坐标超出有效范围'],
|
|
[{ latitude: 22, longitude: 181, name: '博物馆' }, '地图坐标超出有效范围'],
|
|
[{ latitude: 22, longitude: 114, name: ' ' }, '目的地名称不能为空']
|
|
])('拒绝非法参数 %#', (target, message) => {
|
|
expect(validateWechatOpenLocationTarget(target)).toEqual({ ok: false, message })
|
|
expect(() => buildWechatOpenLocationPageUrl(target)).toThrow(message)
|
|
})
|
|
})
|