231 lines
8.1 KiB
TypeScript
231 lines
8.1 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||
import { flushPromises, mount } from '@vue/test-utils'
|
||
import ElementPlus from 'element-plus'
|
||
import { createMemoryHistory, createRouter } from 'vue-router'
|
||
import Review from '@/components/Review.vue'
|
||
import { getReviewSummary, getReviewTaskStats } from '@/api/review'
|
||
import { getPendingOwnershipFragments } from '@/api/reportFragments'
|
||
import { listTagMindMaps } from '@/api/tagMindMap'
|
||
import { listTaskOptions } from '@/api/tasks'
|
||
import { pageOf } from '../support/page'
|
||
import { getMarkersBatch, listDictionaryTags, createDictionaryTag, deleteDictionaryTag, renameDictionaryTag } from '@/api/markers'
|
||
|
||
vi.mock('@/api/review', () => ({
|
||
getReviewTaskStats: vi.fn(),
|
||
getReviewSummary: vi.fn(),
|
||
getSortPreference: vi.fn(),
|
||
saveSortPreference: vi.fn(),
|
||
}))
|
||
|
||
vi.mock('@/api/reportFragments', () => ({
|
||
getPendingOwnershipFragments: vi.fn(),
|
||
updateFragmentOwnership: vi.fn(),
|
||
}))
|
||
|
||
vi.mock('@/api/markers', () => ({
|
||
addUserTag: vi.fn(),
|
||
createDictionaryTag: vi.fn(),
|
||
deleteDictionaryTag: vi.fn(),
|
||
deleteMarker: vi.fn(),
|
||
filterByMarker: vi.fn(),
|
||
getMarkersBatch: vi.fn(),
|
||
getUserTagUsage: vi.fn(),
|
||
listDictionaryTags: vi.fn(),
|
||
listTagCandidates: vi.fn(),
|
||
renameDictionaryTag: vi.fn(),
|
||
renameUserTag: vi.fn(),
|
||
}))
|
||
|
||
vi.mock('@/api/export', () => ({
|
||
exportRange: vi.fn(),
|
||
}))
|
||
|
||
vi.mock('@/api/tagMindMap', () => ({
|
||
listTagMindMaps: vi.fn(),
|
||
}))
|
||
|
||
vi.mock('@/api/tasks', () => ({
|
||
listTaskOptions: vi.fn(),
|
||
}))
|
||
|
||
const task = (taskNum: string, taskName: string) => ({
|
||
taskNum,
|
||
taskName,
|
||
reportCount: 1,
|
||
fragmentCount: 2,
|
||
effectiveTime: 0,
|
||
reviewDurationSeconds: 0,
|
||
})
|
||
|
||
const mountReview = async (query: Record<string, string> = {}) => {
|
||
const testRouter = createRouter({
|
||
history: createMemoryHistory(),
|
||
routes: [
|
||
{ path: '/review', component: Review },
|
||
{ path: '/review/by-tag', component: { template: '<div />' } },
|
||
],
|
||
})
|
||
await testRouter.push({ path: '/review', query })
|
||
await testRouter.isReady()
|
||
const wrapper = mount(Review, {
|
||
global: { plugins: [ElementPlus, testRouter], stubs: { ElTag: true } },
|
||
})
|
||
await flushPromises()
|
||
return { wrapper, testRouter }
|
||
}
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
vi.mocked(getReviewTaskStats).mockResolvedValue({
|
||
code: 200,
|
||
data: pageOf([task('T1', '英语任务'), task('T2', '数学任务')]),
|
||
} as never)
|
||
// 汇总走独立接口:全量口径,不由分页后的列表相加
|
||
vi.mocked(getReviewSummary).mockResolvedValue({
|
||
code: 200,
|
||
data: { taskTotal: 2, reportTotal: 2, fragmentTotal: 4 },
|
||
} as never)
|
||
vi.mocked(getPendingOwnershipFragments).mockResolvedValue({ code: 200, data: [] } as never)
|
||
vi.mocked(getMarkersBatch).mockResolvedValue({ code: 200, data: {} } as never)
|
||
vi.mocked(listDictionaryTags).mockResolvedValue({ code: 200, data: [] } as never)
|
||
vi.mocked(listTagMindMaps).mockResolvedValue({ code: 200, data: pageOf([]) } as never)
|
||
vi.mocked(listTaskOptions).mockResolvedValue({ code: 200, data: [] } as never)
|
||
})
|
||
|
||
/**
|
||
* 「更多」是 el-dropdown:菜单渲染在 Teleport 的 popper 里,jsdom 下 VTU 拿不到
|
||
* (与 el-tag 同类的 EP 2.8 兼容问题),因此直接触发组件的 command 事件,
|
||
* 这仍然走 handleMoreCommand 的真实分支。
|
||
*/
|
||
const clickMoreItem = async (wrapper: any, command: string) => {
|
||
const dropdown = wrapper.findComponent({ name: 'ElDropdown' })
|
||
expect(dropdown.exists()).toBe(true)
|
||
dropdown.vm.$emit('command', command)
|
||
await flushPromises()
|
||
}
|
||
|
||
describe('Review 页面结构:按任务 / 按标签两个 Tab', () => {
|
||
it('两个模块同位置分 Tab,低频操作收进「更多」,不再有刷新按钮', async () => {
|
||
const { wrapper } = await mountReview()
|
||
|
||
const tabs = wrapper.findAll('[role="tab"]')
|
||
expect(tabs).toHaveLength(2)
|
||
expect(tabs[0].text()).toContain('按任务复习')
|
||
expect(tabs[1].text()).toContain('按标签复习')
|
||
// 刷新已移除:进入页面即加载,重载由空/错态的重试承担
|
||
expect(wrapper.findAll('button').some((b) => b.text().trim() === '刷新')).toBe(false)
|
||
// 导出与标签管理不再平铺在工具栏
|
||
expect(wrapper.findAll('button').some((b) => b.text().trim() === '导出数据')).toBe(false)
|
||
expect(wrapper.findAll('button').some((b) => b.text().includes('更多'))).toBe(true)
|
||
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('按标签复习 Tab 列出已生成的导图,点行进入该范围的复习(URL 即状态)', async () => {
|
||
vi.mocked(listTagMindMaps).mockResolvedValue({
|
||
code: 200,
|
||
data: pageOf([
|
||
{
|
||
id: 1,
|
||
scopeKey: 'S1',
|
||
tags: ['英语', '时态'],
|
||
mode: 'INTERSECT',
|
||
title: '英语时态',
|
||
generator: 'AI',
|
||
sourceFragmentCount: 4,
|
||
needsUpdate: true,
|
||
generatedTime: '2026-10-10T10:00:00',
|
||
},
|
||
]),
|
||
} as never)
|
||
const { wrapper, testRouter } = await mountReview()
|
||
|
||
// 切到标签 Tab 才会加载它的列表
|
||
const tagTab = wrapper.findAll('[role="tab"]').find((t) => t.text().includes('按标签复习'))
|
||
await tagTab!.trigger('click')
|
||
await flushPromises()
|
||
|
||
expect(wrapper.text()).toContain('英语时态')
|
||
expect(wrapper.text()).toContain('有新碎片待整理')
|
||
|
||
await wrapper.find('.map-row').trigger('click')
|
||
await flushPromises()
|
||
|
||
expect(testRouter.currentRoute.value.path).toBe('/review/by-tag')
|
||
expect(testRouter.currentRoute.value.query).toMatchObject({ tag: ['英语', '时态'], mode: 'INTERSECT' })
|
||
|
||
wrapper.unmount()
|
||
})
|
||
})
|
||
|
||
describe('Review 按标签筛选(闭环-01 跳转入口)', () => {
|
||
it('带 markerName 进入时把筛选下推给服务端', async () => {
|
||
vi.mocked(getReviewTaskStats).mockResolvedValue({ code: 200, data: pageOf([task('T1', '英语任务')]) } as never)
|
||
const { wrapper } = await mountReview({ markerName: '英语' })
|
||
|
||
// 筛选在服务端做(否则只能筛当前页、total 也不对)
|
||
expect(getReviewTaskStats).toHaveBeenCalledWith(1, 20, '英语')
|
||
expect(wrapper.text()).toContain('正在按标签「英语」筛选任务')
|
||
expect(wrapper.text()).toContain('英语任务')
|
||
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('不带 markerName 进入时不筛选', async () => {
|
||
const { wrapper } = await mountReview()
|
||
|
||
expect(getReviewTaskStats).toHaveBeenCalledWith(1, 20, undefined)
|
||
expect(wrapper.text()).toContain('数学任务')
|
||
|
||
wrapper.unmount()
|
||
})
|
||
})
|
||
|
||
describe('Review 标签管理:字典(闭环-03)', () => {
|
||
const openTagDialog = async (wrapper: any) => {
|
||
await clickMoreItem(wrapper, 'tags')
|
||
}
|
||
|
||
it('可提前新建空标签,并展示字典项', async () => {
|
||
vi.mocked(listDictionaryTags).mockResolvedValue({
|
||
code: 200,
|
||
data: [{ id: 1, tagName: '英语', normalizedName: '英语' }],
|
||
} as never)
|
||
vi.mocked(createDictionaryTag).mockResolvedValue({
|
||
code: 200,
|
||
data: { id: 2, tagName: '数学', normalizedName: '数学' },
|
||
} as never)
|
||
const { wrapper } = await mountReview()
|
||
|
||
await openTagDialog(wrapper)
|
||
expect(wrapper.text()).toContain('英语')
|
||
|
||
await wrapper.find('input[aria-label="新建标签名"]').setValue('数学')
|
||
const create = wrapper.findAll('button').find((b) => b.text().trim() === '新建')
|
||
await create!.trigger('click')
|
||
await flushPromises()
|
||
|
||
expect(createDictionaryTag).toHaveBeenCalledWith('数学')
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('删除字典标签只影响候选,不动已打标记', async () => {
|
||
vi.mocked(listDictionaryTags).mockResolvedValue({
|
||
code: 200,
|
||
data: [{ id: 9, tagName: '临时', normalizedName: '临时' }],
|
||
} as never)
|
||
vi.mocked(deleteDictionaryTag).mockResolvedValue({ code: 200 } as never)
|
||
const { wrapper } = await mountReview()
|
||
|
||
await openTagDialog(wrapper)
|
||
const remove = wrapper.findAll('button').find((b) => b.text().trim() === '删除')
|
||
await remove!.trigger('click')
|
||
await flushPromises()
|
||
|
||
expect(deleteDictionaryTag).toHaveBeenCalledWith(9)
|
||
// 字典删除不会去改动内容上的标记
|
||
expect(renameDictionaryTag).not.toHaveBeenCalled()
|
||
wrapper.unmount()
|
||
})
|
||
})
|