Files
crawler-plugin/frontend-vue/tests/mount/keyword-share-page.mount.test.ts
T
huangzd1997 208583e14c feat(common-tools): 工具台新增常用工具分类,三个桌面工具网页化
- tool-catalog 最前插入「常用工具」组(brand_common_tools,23 工具·4 大分类),
  三工具各自权限键:pricing-calc / five-asin / keyword-share
- /pricing-calc:站点(英 GBP÷9、德法 EUR÷8)+ 买货价/运费 → 营业额/利润/
  利润率/换算汇率,公式与精度照抄桌面版,纯函数 node --test 钉住
- /five-asin:五国常量 + ASIN 10 位校验 → 亚马逊商品链接,window.open 与
  :href 出口全部过 safeHttpUrl(url-outlet 棘轮),邮编一键复制
- /keyword-share:筛选/四级类目级联/统计卡/表格分页 200/标记与取消使用/
  添加/删除/随机抽词/共享记录,对接 keywordshare 后端九端点
- 顶栏与 BrandTopBar 导航同步;router 注册三路由;补 test:mount 脚本
  (vitest mount 套件入口,与后管同口径)
2026-10-23 21:56:56 +08:00

161 lines
5.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.
/**
* 五国关键词共享页挂载用例(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:纯函数(normalizeKeywordStatsGrouped)保留真实现,
// 网络函数按用例给桩。页面经 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()
})
})