Files
frontend-miniapp/tests/unit/ExplainGuideStopListNavigation.spec.ts
lyf cf6ad02318
Some checks failed
CI / verify (push) Has been cancelled
讲解对象列表接入展厅分页接口
2026-07-17 18:27:24 +08:00

123 lines
3.8 KiB
TypeScript

// @vitest-environment happy-dom
import { defineComponent } from 'vue'
import { flushPromises, mount } from '@vue/test-utils'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import ExplainGuideStopListPage from '@/pages/explain/guide-stop-list.vue'
const testState = vi.hoisted(() => ({
onLoadHandler: null as null | ((options?: Record<string, string>) => void)
}))
vi.mock('@dcloudio/uni-app', () => ({
onLoad: (handler: (options?: Record<string, string>) => void) => {
testState.onLoadHandler = handler
}
}))
vi.mock('@/usecases/explainUseCase', () => ({
explainUseCase: {
listGuideStopsPageByHall: vi.fn().mockResolvedValue({
items: [], total: 0, pageNo: 1, pageSize: 20, hasMore: false
})
}
}))
const GuidePageFrameStub = defineComponent({
name: 'GuidePageFrame',
emits: ['back'],
template: '<main><button data-testid="frame-back" @click="$emit(\'back\')">返回</button><slot /></main>'
})
const ExplainHallSelectStub = defineComponent({
name: 'ExplainHallSelect',
emits: ['back'],
template: '<button data-testid="list-back" @click="$emit(\'back\')">返回</button>'
})
const mountGuideStopList = () => mount(ExplainGuideStopListPage, {
global: {
stubs: {
GuidePageFrame: GuidePageFrameStub,
ExplainHallSelect: ExplainHallSelectStub
}
}
})
beforeEach(() => {
testState.onLoadHandler = null
vi.stubGlobal('uni', {
navigateBack: vi.fn(),
reLaunch: vi.fn(),
navigateTo: vi.fn(),
setNavigationBarTitle: vi.fn()
})
vi.stubGlobal('getCurrentPages', vi.fn(() => []))
})
afterEach(() => {
vi.restoreAllMocks()
vi.unstubAllGlobals()
})
describe('讲解对象列表返回', () => {
it('上一页是独立展厅列表时回到该列表', async () => {
vi.stubGlobal('getCurrentPages', vi.fn(() => [
{ route: 'pages/explain/list', options: {} },
{ route: 'pages/explain/guide-stop-list', options: {} }
]))
const wrapper = mountGuideStopList()
testState.onLoadHandler?.({ hallId: 'hall-1', hallName: '恐龙厅' })
await flushPromises()
await wrapper.get('[data-testid="frame-back"]').trigger('click')
expect(uni.navigateBack).toHaveBeenCalledWith(expect.objectContaining({ delta: 1 }))
expect(uni.reLaunch).not.toHaveBeenCalled()
wrapper.unmount()
})
it('上一页是首页讲解 Tab 时回到首页中的展厅列表', async () => {
vi.stubGlobal('getCurrentPages', vi.fn(() => [
{ route: 'pages/index/index', options: { tab: 'explain' } },
{ route: 'pages/explain/guide-stop-list', options: {} }
]))
const wrapper = mountGuideStopList()
await wrapper.get('[data-testid="list-back"]').trigger('click')
expect(uni.navigateBack).toHaveBeenCalledWith(expect.objectContaining({ delta: 1 }))
expect(uni.reLaunch).not.toHaveBeenCalled()
wrapper.unmount()
})
it('深链或不相关上一页直接回退至独立展厅列表', async () => {
const wrapper = mountGuideStopList()
await wrapper.get('[data-testid="list-back"]').trigger('click')
expect(uni.navigateBack).not.toHaveBeenCalled()
expect(uni.reLaunch).toHaveBeenCalledWith({ url: '/pages/explain/list' })
wrapper.unmount()
})
it('页面栈返回失败时回退至独立展厅列表', async () => {
vi.stubGlobal('getCurrentPages', vi.fn(() => [
{ route: 'pages/explain/list', options: {} },
{ route: 'pages/explain/guide-stop-list', options: {} }
]))
vi.stubGlobal('uni', {
navigateBack: vi.fn((options: { fail?: () => void }) => options.fail?.()),
reLaunch: vi.fn(),
navigateTo: vi.fn(),
setNavigationBarTitle: vi.fn()
})
const wrapper = mountGuideStopList()
await wrapper.get('[data-testid="list-back"]').trigger('click')
expect(uni.reLaunch).toHaveBeenCalledWith({ url: '/pages/explain/list' })
wrapper.unmount()
})
})