Files
lpt-fe/src/__tests__/components/Review.spec.ts
T

168 lines
6.5 KiB
TypeScript
Raw 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.
import { afterEach, describe, it, expect, vi, beforeEach } from 'vitest'
import { DOMWrapper, flushPromises, mount } from '@vue/test-utils'
import ElementPlus from 'element-plus'
import { createMemoryHistory, createRouter } from 'vue-router'
import Review from '@/components/Review.vue'
import { getPendingOwnershipFragments } from '@/api/reportFragments'
import { listTaskOptions } from '@/api/tasks'
import { listRecalls } from '@/api/recalls'
import { pageOf } from '../support/page'
import { ok } from '../support/recall'
import { getMarkersBatch, getUserTagUsage, listDictionaryTags, createDictionaryTag, deleteDictionaryTag, renameDictionaryTag } from '@/api/markers'
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/tasks', () => ({
listTaskOptions: vi.fn(),
}))
vi.mock('@/api/recalls', () => ({
listRecalls: vi.fn(),
}))
const mountReview = async (query: Record<string, string> = {}) => {
const testRouter = createRouter({
history: createMemoryHistory(),
routes: [
{ path: '/review', component: Review },
{ path: '/review/by-tag', component: { template: '<div />' } },
{ path: '/review/recalls/:recallNum', component: { template: '<div />' } },
],
})
await testRouter.push({ path: '/review', query })
await testRouter.isReady()
const wrapper = mount(Review, {
attachTo: document.body,
global: { plugins: [ElementPlus, testRouter], stubs: { ElTag: true } },
})
await flushPromises()
return { wrapper, testRouter }
}
beforeEach(() => {
vi.clearAllMocks()
vi.mocked(getUserTagUsage).mockResolvedValue(ok([]))
vi.mocked(getPendingOwnershipFragments).mockResolvedValue(ok([]))
vi.mocked(getMarkersBatch).mockResolvedValue(ok({}))
vi.mocked(listDictionaryTags).mockResolvedValue(ok([]))
vi.mocked(listTaskOptions).mockResolvedValue(ok([]))
vi.mocked(listRecalls).mockResolvedValue(ok(pageOf([])))
})
/** 区块按页面上的实际位置取:范围块 → 待归类块 → 最近回忆 */
const blockOrder = (wrapper: ReturnType<typeof mount>) => Array.from(
wrapper.element.querySelectorAll('.tag-scope, .pending-card, .review-history'),
).map(node => node.classList.contains('tag-scope') ? 'scope'
: node.classList.contains('pending-card') ? 'pending' : 'history')
afterEach(() => { document.body.innerHTML = '' })
const clickMoreItem = async (wrapper: ReturnType<typeof mount>, name: string) => {
await wrapper.find('[aria-label="标签管理与导出"]').trigger('click'); await flushPromises();
const item = Array.from(document.querySelectorAll('[role="menuitem"]')).find(node => node.textContent?.includes(name));
await new DOMWrapper(item!).trigger('click'); await flushPromises();
}
describe('Review 页面结构:范围入口而不是旧仪表盘', () => {
it('不再有已有标签导图、三连统计与排序偏好入口', async () => {
const { wrapper } = await mountReview()
expect(wrapper.text()).not.toContain('已有标签导图')
expect(wrapper.find('.page-stats').exists()).toBe(false)
expect(wrapper.find('.tag-mind-map').exists()).toBe(false)
await wrapper.find('[aria-label="标签管理与导出"]').trigger('click'); await flushPromises();
const items = Array.from(document.querySelectorAll('[role="menuitem"]')).map(node => node.textContent?.trim());
expect(items).toEqual(['标签管理', '导出学习数据'])
wrapper.unmount()
})
it('首块是范围块,待归类块之后才是最近回忆', async () => {
vi.mocked(getPendingOwnershipFragments).mockResolvedValue(ok([{ id: 11, content: '待归类知识', ownerTaskNum: null }]))
const { wrapper } = await mountReview()
expect(blockOrder(wrapper)).toEqual(['scope', 'pending', 'history'])
wrapper.unmount()
})
it('页面上没有未结束草稿时不出现继续上次回忆,范围按钮是唯一实底主操作', async () => {
vi.mocked(getUserTagUsage).mockResolvedValue(ok([{ markerName: '英语', markerCount: 1, objectCount: 1 }]))
const { wrapper } = await mountReview()
expect(wrapper.find('.draft-card').exists()).toBe(false)
const range = wrapper.find('.tag-scope .action-row button')
expect(range.classes()).toContain('el-button--success')
expect(range.classes()).not.toContain('is-plain')
expect(range.attributes('disabled')).toBeDefined()
wrapper.unmount()
})
})
describe('Review 标签管理:字典(闭环-03)', () => {
const openTagDialog = async (wrapper: ReturnType<typeof mount>) => {
await clickMoreItem(wrapper, '标签管理')
}
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()
})
})