Files
frontend-miniapp/tests/unit/ArrivalPanel.spec.ts
lyf d199b4602f
Some checks failed
CI / verify (push) Has been cancelled
完善馆内点位展示与微信地图桥接
2026-07-19 16:01:32 +08:00

85 lines
3.7 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// @vitest-environment happy-dom
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import ArrivalPanel from '@/components/navigation/ArrivalPanel.vue'
import { ARRIVAL_TARGETS } from '@/data/arrivalSearchTargets'
import { openExternalNavigation } from '@/services/MapNavigationService'
import { openWechatMiniProgramLocation } from '@/services/WechatMiniProgramBridgeService'
vi.mock('@/services/MapNavigationService', () => ({ openExternalNavigation: vi.fn() }))
vi.mock('@/services/WechatMiniProgramBridgeService', () => ({ openWechatMiniProgramLocation: vi.fn() }))
const selectedTarget = ARRIVAL_TARGETS[1]
const defaultUserAgent = window.navigator.userAgent
const mountArrivalPanel = () => mount(ArrivalPanel, {
props: {
visible: true,
activeType: selectedTarget.type,
targets: [selectedTarget],
selectedTargetId: selectedTarget.id,
selectedTarget
}
})
beforeEach(() => {
vi.mocked(openExternalNavigation).mockResolvedValue(true)
vi.mocked(openWechatMiniProgramLocation).mockResolvedValue({ status: 'not-embedded' })
vi.stubGlobal('uni', { showToast: vi.fn(), showActionSheet: vi.fn() })
window.history.replaceState({}, '', '/#/pages/index/index?tab=guide')
delete (window as Window & { __wxjs_environment?: string }).__wxjs_environment
Object.defineProperty(window.navigator, 'userAgent', { configurable: true, value: 'Mozilla/5.0 (Linux; Android 14)' })
})
afterEach(() => {
Object.defineProperty(window.navigator, 'userAgent', { configurable: true, value: defaultUserAgent })
delete (window as Window & { __wxjs_environment?: string }).__wxjs_environment
vi.clearAllMocks()
vi.unstubAllGlobals()
})
describe('来馆面板 H5 导航分流', () => {
it('嵌入小程序 web-view 时只向宿主页面导航,且完整传递当前选中目标', async () => {
Object.assign(window, { __wxjs_environment: 'miniprogram' })
Object.defineProperty(window.navigator, 'userAgent', { configurable: true, value: 'Mozilla/5.0 MicroMessenger/8.0.50' })
vi.mocked(openWechatMiniProgramLocation).mockResolvedValue({ status: 'opened' })
const wrapper = mountArrivalPanel()
expect(wrapper.get('.arrival-primary-text').text()).toBe('打开地图导航')
await wrapper.get('.arrival-primary').trigger('tap')
expect(openWechatMiniProgramLocation).toHaveBeenCalledTimes(1)
expect(openWechatMiniProgramLocation).toHaveBeenCalledWith({
latitude: selectedTarget.latitude,
longitude: selectedTarget.longitude,
name: selectedTarget.title,
address: selectedTarget.subtitle
})
expect(openExternalNavigation).not.toHaveBeenCalled()
expect((uni as unknown as { showActionSheet: ReturnType<typeof vi.fn> }).showActionSheet).not.toHaveBeenCalled()
})
it('普通 H5 仅回退至第三方地图,并使用当前选中目标', async () => {
const wrapper = mountArrivalPanel()
expect(wrapper.get('.arrival-primary-text').text()).toBe('第三方导航')
await wrapper.get('.arrival-primary').trigger('tap')
expect(openExternalNavigation).toHaveBeenCalledWith({
latitude: selectedTarget.latitude,
longitude: selectedTarget.longitude,
name: selectedTarget.title,
address: selectedTarget.subtitle
})
})
it('诊断信息默认隐藏,仅在 debug-build=1 时显示构建边界', () => {
const normalWrapper = mountArrivalPanel()
expect(normalWrapper.find('[data-testid="arrival-build-diagnostics"]').exists()).toBe(false)
normalWrapper.unmount()
window.location.hash = '#/pages/index/index?tab=guide&debug-build=1'
const debugWrapper = mountArrivalPanel()
expect(debugWrapper.get('[data-testid="arrival-build-diagnostics"]').text()).toContain('构建平台h5')
})
})