test(common-tools): 定价换算与五国ASIN页挂载冒烟测试
补两页的行为覆盖:换算四值与站点切换清空、资料下载/常用链接出口形态、 ASIN 规范化与域名随国家切换、位数不足报错并禁用打开按钮。
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* 常用工具两页的挂载冒烟测试(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('资料下载四个锚点落在 /materials/ 且 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('/materials/')).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()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user