Files
crawler-plugin/frontend-vue/tests/mount/common-tools-pages.mount.test.ts
T
huangzd1997 e36d363c57 fix(common-tools): 资料下载改挂站点 /static/ 静态位
/materials/ 是不在发布白名单里的新目录,deploy_app_web.py 只上传
index.html+assets+/static/ 清单 ⇒ 上线即 404。改走既有 /static/(nginx 已配),
发布清单在 ops 仓 deploy_app_web.py 同步登记 4 个文件。
2026-10-23 23:38:03 +08:00

154 lines
5.1 KiB
TypeScript

/**
* 常用工具两页的挂载冒烟测试(tests/mount/** 由 vitest 收)。
*
* 页面壳(AmazonToolPageShell→AmazonTopBar)依赖权限接口与路由,这里整体桩掉,
* 只验页面本体的行为:换算数值、站点切换清空、资料下载/常用链接出口、ASIN 校验与链接拼装。
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import ElementPlus from 'element-plus'
import PricingCalcPage from '@/pages/common/PricingCalcPage.vue'
import FiveAsinPage from '@/pages/common/FiveAsinPage.vue'
vi.mock('@/shared/api/permission', () => ({
getCurrentUserAppColumnKeys: vi.fn().mockResolvedValue([]),
}))
class ResizeObserverStub {
observe() {}
unobserve() {}
disconnect() {}
}
const STUBS = {
RouterLink: true,
'router-link': true,
AmazonTopBar: true,
NotificationBell: true,
BrandApiSecretSettingsButton: true,
DownloadProgressPanel: true,
AmazonFooterBar: true,
}
let wrapper: VueWrapper | null = null
function mountPage(component: object) {
wrapper = mount(component, {
global: { plugins: [ElementPlus], stubs: STUBS },
})
return wrapper
}
function findButtonByText(text: RegExp) {
return (wrapper?.findAll('button') ?? []).find((b) => text.test(b.text()))
}
async function setInput(index: number, value: string) {
const input = wrapper?.findAll('input')[index]
expect(input, `第 ${index} 个输入框不存在`).toBeTruthy()
await input!.setValue(value)
}
describe('定价利润换算页', () => {
beforeEach(() => {
if (!(globalThis as Record<string, unknown>).ResizeObserver) {
;(globalThis as Record<string, unknown>).ResizeObserver = ResizeObserverStub
}
})
afterEach(() => {
wrapper?.unmount()
wrapper = null
})
it('买货价 10 + 运费 5(英国)→ 营业额 30.00 / 利润 9.00 / 利润率 30.0% / 汇率 3.3333', async () => {
const page = mountPage(PricingCalcPage)
await setInput(0, '10')
await setInput(1, '5')
findButtonByText(/^换算利润$/).trigger('click')
await flushPromises()
const values = page.findAll('.result-value').map((n) => n.text())
expect(values).toEqual(['30.00', '9.00', '30.0%', '3.3333'])
})
it('切到德国·法国:除数 8 生效,且切换站点清空已有结果', async () => {
const page = mountPage(PricingCalcPage)
await setInput(0, '10')
await setInput(1, '5')
findButtonByText(/^换算利润$/).trigger('click')
await flushPromises()
expect(page.findAll('.result-value').length).toBe(4)
findButtonByText(/德国·法国/).trigger('click')
await flushPromises()
expect(page.find('.result-empty').exists()).toBe(true)
findButtonByText(/^换算利润$/).trigger('click')
await flushPromises()
expect(page.findAll('.result-value')[3]?.text()).toBe('3.7500')
})
it('资料下载四个锚点落在 /static/ 且 href 无裸空格;常用链接均为 http(s)', async () => {
const page = mountPage(PricingCalcPage)
const downloadHrefs = page
.findAll('a[download]')
.map((a) => a.attributes('href') ?? '')
expect(downloadHrefs).toHaveLength(4)
for (const href of downloadHrefs) {
expect(href.startsWith('/static/')).toBe(true)
expect(href.includes(' ')).toBe(false)
}
const linkHrefs = page
.findAll('.resource-block')
.filter((block) => block.text().includes('常用链接'))
.flatMap((block) => block.findAll('a').map((a) => a.attributes('href') ?? ''))
expect(linkHrefs.length).toBeGreaterThanOrEqual(5)
for (const href of linkHrefs) {
expect(/^https?:\/\//.test(href)).toBe(true)
}
})
})
describe('五国ASIN快速查询页', () => {
beforeEach(() => {
if (!(globalThis as Record<string, unknown>).ResizeObserver) {
;(globalThis as Record<string, unknown>).ResizeObserver = ResizeObserverStub
}
})
afterEach(() => {
wrapper?.unmount()
wrapper = null
})
it('五个国家可选,小写输入规范化后拼出 amazon.co.uk 商品链接', async () => {
const page = mountPage(FiveAsinPage)
expect(page.findAll('.country-btn')).toHaveLength(5)
await setInput(0, ' b0c1234abc ')
await flushPromises()
const link = page.find('.link-url')
expect(link.attributes('href')).toBe('https://www.amazon.co.uk/dp/B0C1234ABC')
expect(link.text()).toBe('https://www.amazon.co.uk/dp/B0C1234ABC')
})
it('切到德国后链接域名随国家走', async () => {
const page = mountPage(FiveAsinPage)
page.findAll('.country-btn')[1].trigger('click')
await setInput(0, 'B012345678')
await flushPromises()
expect(page.find('.link-url').attributes('href')).toBe('https://www.amazon.de/dp/B012345678')
expect(page.find('.postcode-value').text()).toBe('10115')
})
it('不足 10 位给中文报错并禁用「打开商品页」', async () => {
const page = mountPage(FiveAsinPage)
await setInput(0, 'B0123')
await flushPromises()
expect(page.find('.asin-error').text()).toContain('10')
expect(findButtonByText(/^打开商品页$/).attributes('disabled')).toBeDefined()
})
})