de4465730f
/stats-grouped 是四段各自 GROUP BY(对齐桌面版弹窗),前端原先按 「国家×一级类目」交叉表建模 ⇒ 弹窗恒为暂无数据;/logs 返回 userName/time 而表格读 operator/createdAt ⇒ 两列恒为占位符。改为 API 层映射 + 四段渲染, 日志去掉后端不产出的「说明」列。
161 lines
5.5 KiB
TypeScript
161 lines
5.5 KiB
TypeScript
/**
|
||
* 五国关键词共享页挂载用例(2026-10-21 常用工具网页化)。
|
||
*
|
||
* 两件事都要判,缺一条就是假绿:
|
||
* ① 首屏真实走 API:列表行、统计卡都来自 /keywords 返回,页面不得自带假数据;
|
||
* ② 行内「标记已使用」真实打到 POST /usage/mark,且载荷是 country+keyword、
|
||
* 成功后重拉列表(防"点了没反应")。
|
||
*
|
||
* 跑法:cd frontend-vue && npx vitest run tests/mount/keyword-share-page.mount.test.ts
|
||
* (tests/mount/** 由 vitest.config.ts 收,node --test 的 glob 抓不到这里。)
|
||
*/
|
||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
|
||
import ElementPlus from 'element-plus'
|
||
|
||
import KeywordSharePage from '@/pages/common/KeywordSharePage.vue'
|
||
|
||
// API 层整模块 mock:纯函数(buildKeywordStatsSections / mapKeywordShareLogs)保留真实现,
|
||
// 网络函数按用例给桩。页面经 shared/api/types/modules/keyword-share 调用。
|
||
const api = vi.hoisted(() => ({
|
||
fetchKeywordShareFilters: vi.fn(),
|
||
fetchKeywordShareKeywords: vi.fn(),
|
||
fetchKeywordShareStatsGrouped: vi.fn(),
|
||
addKeywordShareKeywords: vi.fn(),
|
||
deleteKeywordShareKeywords: vi.fn(),
|
||
markKeywordShareUsage: vi.fn(),
|
||
unmarkKeywordShareUsage: vi.fn(),
|
||
fetchKeywordShareLogs: vi.fn(),
|
||
fetchKeywordShareRandom: vi.fn(),
|
||
}))
|
||
|
||
vi.mock('@/shared/api/types/modules/keyword-share', async (importOriginal) => ({
|
||
...(await importOriginal<typeof import('@/shared/api/types/modules/keyword-share')>()),
|
||
...api,
|
||
}))
|
||
|
||
vi.mock('@/shared/api/permission', () => ({
|
||
getCurrentUserAppColumnKeys: vi.fn().mockResolvedValue([]),
|
||
}))
|
||
|
||
const ROWS = [
|
||
{
|
||
id: 1,
|
||
country: '英国',
|
||
keyword: 'wireless charger',
|
||
chinese: '无线充电器',
|
||
category: '电子',
|
||
subCategory: '配件',
|
||
categoryL3: '充电',
|
||
categoryL4: '无线充',
|
||
used: false,
|
||
usedTime: null,
|
||
usedUser: null,
|
||
},
|
||
{
|
||
id: 2,
|
||
country: '德国',
|
||
keyword: 'handtuch wärmer',
|
||
chinese: '毛巾架',
|
||
category: '家居',
|
||
subCategory: '浴室',
|
||
categoryL3: '毛巾架',
|
||
categoryL4: '加热',
|
||
used: true,
|
||
usedTime: '2026-10-20 10:00:00',
|
||
usedUser: '张三',
|
||
},
|
||
]
|
||
|
||
const LIST_VO = {
|
||
total: 2,
|
||
rows: ROWS,
|
||
stats: { hit: 2, used: 1, unused: 1, dup: 0 },
|
||
}
|
||
|
||
// happy-dom 没有ResizeObserver 时 el-table 会挂不上,补一个最小桩
|
||
class ResizeObserverStub {
|
||
observe() {}
|
||
unobserve() {}
|
||
disconnect() {}
|
||
}
|
||
|
||
let wrapper: VueWrapper | null = null
|
||
|
||
async function mountPage() {
|
||
wrapper = mount(KeywordSharePage, {
|
||
global: {
|
||
plugins: [ElementPlus],
|
||
stubs: {
|
||
RouterLink: true,
|
||
'router-link': true,
|
||
NotificationBell: true,
|
||
BrandApiSecretSettingsButton: true,
|
||
DownloadProgressPanel: true,
|
||
AmazonFooterBar: true,
|
||
},
|
||
},
|
||
})
|
||
await flushPromises()
|
||
await flushPromises()
|
||
return wrapper
|
||
}
|
||
|
||
function findButtonByText(text: string) {
|
||
const buttons = wrapper?.findAll('button') ?? []
|
||
return buttons.find((button) => button.text().trim() === text)
|
||
}
|
||
|
||
describe('五国关键词共享页', () => {
|
||
beforeEach(() => {
|
||
if (!(globalThis as Record<string, unknown>).ResizeObserver) {
|
||
;(globalThis as Record<string, unknown>).ResizeObserver = ResizeObserverStub
|
||
}
|
||
api.fetchKeywordShareFilters.mockResolvedValue({ countries: ['英国', '德国'], categories: [] })
|
||
api.fetchKeywordShareKeywords.mockResolvedValue(LIST_VO)
|
||
api.markKeywordShareUsage.mockResolvedValue({ marked: 1 })
|
||
api.unmarkKeywordShareUsage.mockResolvedValue({ unmarked: 1 })
|
||
})
|
||
|
||
afterEach(() => {
|
||
wrapper?.unmount()
|
||
wrapper = null
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
it('首屏渲染 API 返回的关键词行与统计卡(无假数据)', async () => {
|
||
const w = await mountPage()
|
||
expect(api.fetchKeywordShareKeywords).toHaveBeenCalledTimes(1)
|
||
const text = w.text()
|
||
expect(text, '行数据来自 /keywords 返回').toContain('wireless charger')
|
||
expect(text).toContain('handtuch wärmer')
|
||
expect(text).toContain('✓已使用·2026-10-20 10:00:00·张三')
|
||
expect(text).toContain('未使用')
|
||
// 统计卡四格来自返回的 stats
|
||
expect(text).toContain('命中总数')
|
||
expect(w.findAll('.stat-card .stat-value').map((node) => node.text())).toEqual(['2', '1', '1', '0'])
|
||
})
|
||
|
||
it('行内「标记已使用」打到 POST /usage/mark(country+keyword 载荷)并重拉列表', async () => {
|
||
await mountPage()
|
||
expect(api.markKeywordShareUsage).not.toHaveBeenCalled()
|
||
const markBtn = findButtonByText('标记已使用')
|
||
expect(markBtn, '未使用行应显示「标记已使用」').toBeTruthy()
|
||
await markBtn!.trigger('click')
|
||
await flushPromises()
|
||
expect(api.markKeywordShareUsage).toHaveBeenCalledTimes(1)
|
||
expect(api.markKeywordShareUsage).toHaveBeenCalledWith([{ country: '英国', keyword: 'wireless charger' }])
|
||
expect(api.fetchKeywordShareKeywords, '标记成功后应重拉列表').toHaveBeenCalledTimes(2)
|
||
})
|
||
|
||
it('已使用行的行内按钮是「标记未使用」,点击走 POST /usage/unmark', async () => {
|
||
await mountPage()
|
||
const unmarkBtn = findButtonByText('标记未使用')
|
||
expect(unmarkBtn, '已使用行应显示「标记未使用」').toBeTruthy()
|
||
await unmarkBtn!.trigger('click')
|
||
await flushPromises()
|
||
expect(api.unmarkKeywordShareUsage).toHaveBeenCalledWith([{ country: '德国', keyword: 'handtuch wärmer' }])
|
||
expect(api.markKeywordShareUsage).not.toHaveBeenCalled()
|
||
})
|
||
})
|