fix: 复习详情返回按钮改为返回总览

This commit is contained in:
2026-09-13 23:12:23 +08:00
parent 66c3c68394
commit 132cb706d7
4 changed files with 89 additions and 7 deletions
+51 -2
View File
@@ -22,6 +22,13 @@ async function mountAt(path: string) {
})
}
// 真实挂载:shallow 会存根化 el-button 且不渲染插槽,无法断言按钮文案与点击行为
async function mountRealAt(path: string) {
await router.push(path)
await router.isReady()
return mount(MyHead, { global: { plugins: [ElementPlus, router] } })
}
describe('MyHead.vue', () => {
beforeEach(async () => {
vi.clearAllMocks()
@@ -30,6 +37,49 @@ describe('MyHead.vue', () => {
await router.isReady()
})
it('默认返回首页,复习详情按路由 meta 返回总览', async () => {
localStorage.setItem('isLoggedIn', 'true')
const studyWrapper = await mountRealAt('/study')
expect(studyWrapper.find('.back-btn').text()).toBe('← 返回首页')
await studyWrapper.find('.back-btn').trigger('click')
await vi.waitFor(() => expect(router.currentRoute.value.path).toBe('/welcome'))
studyWrapper.unmount()
const taskDetailWrapper = await mountRealAt('/review/detail/task/T1')
expect(taskDetailWrapper.find('.back-btn').text()).toBe('← 返回总览')
await taskDetailWrapper.find('.back-btn').trigger('click')
await vi.waitFor(() => expect(router.currentRoute.value.path).toBe('/review'))
taskDetailWrapper.unmount()
})
it('多入口详情页优先返回来路,无历史时兜底到上级', async () => {
localStorage.setItem('isLoggedIn', 'true')
const backSpy = vi.spyOn(router, 'back').mockImplementation(() => {})
// 从回忆复习页进入:存在历史,回退到实际来路
await mountRealAt('/review/recall/T1')
const fromRecall = await mountRealAt('/review/detail/report/9')
expect(fromRecall.find('.back-btn').text()).toBe('← 返回上一页')
await fromRecall.find('.back-btn').trigger('click')
expect(backSpy).toHaveBeenCalledTimes(1)
fromRecall.unmount()
// 直接深链或刷新进入:没有历史,回退到该页声明的上级
window.history.replaceState(
{ back: null, current: '/review/detail/report/9', forward: null, position: 0 },
'',
)
const deepLink = mount(MyHead, { global: { plugins: [ElementPlus, router] } })
expect(deepLink.find('.back-btn').text()).toBe('← 返回上一页')
await deepLink.find('.back-btn').trigger('click')
expect(backSpy).toHaveBeenCalledTimes(1)
await vi.waitFor(() => expect(router.currentRoute.value.path).toBe('/review'))
deepLink.unmount()
backSpy.mockRestore()
})
it('derives route-based computed properties from real route', async () => {
const loginWrapper = await mountAt('/login')
expect(loginWrapper.vm.isLoginRoute).toBe(true)
@@ -46,8 +96,7 @@ describe('MyHead.vue', () => {
expect(welcomeWrapper.vm.pageTitle).toBeUndefined()
})
it('clears login state and navigates to login on logout', async () => {
vi.mocked(logout).mockResolvedValue({ code: 200 } as any)
it('clears login state and navigates to login on logout', async () => { vi.mocked(logout).mockResolvedValue({ code: 200 } as any)
localStorage.setItem('isLoggedIn', 'true')
const wrapper = await mountAt('/study')