Files
hfb_jysh/frontend/src/features/listings/labels.test.ts
T

152 lines
4.9 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 { describe, expect, it } from 'vitest'
import {
listingPriceDetail,
listingRatioText,
listingResources,
resourceBadgeText,
resourceChargeText,
} from '@/features/listings/labels'
type RatioItem = {
price_cent: number
haf_coin_amount: number
asset_summary?: Record<string, unknown>
}
function listing(patch: Partial<RatioItem> = {}): RatioItem {
return { price_cent: 0, haf_coin_amount: 0, asset_summary: {}, ...patch }
}
describe('listingRatioText', () => {
it('优先使用 publish_ratio,并按四舍五入取整到万', () => {
expect(listingRatioText(listing({ asset_summary: { publish_ratio: 33.8256 } }))).toBe('34w')
expect(listingRatioText(listing({ asset_summary: { publish_ratio: 33.4 } }))).toBe('33w')
})
it('缺少 publish_ratio 时按「万币 / 元」兜底计算', () => {
expect(listingRatioText(listing({ haf_coin_amount: 30_000_000, price_cent: 9_369 }))).toBe(
'32w'
)
})
it('价格或币量缺失时返回空串,不渲染占位', () => {
expect(listingRatioText(listing({ haf_coin_amount: 30_000_000 }))).toBe('')
expect(listingRatioText(listing({ price_cent: 10_000 }))).toBe('')
expect(listingRatioText(listing({ asset_summary: { publish_ratio: 0 } }))).toBe('')
})
})
describe('listingPriceDetail', () => {
const item = {
price_cent: 191350,
asset_summary: { pure_coin_price_cent: 176350, extra_item_price_cent: 15000 },
}
it('把额外物资标成收费项', () => {
expect(listingPriceDetail(item)).toBe('(纯币租金 ¥1,763.50 + 额外物资收费 ¥150.00)')
expect(listingPriceDetail(item, { compact: true })).toBe('纯币 ¥1,763.50 + 物资收费 ¥150.00')
})
it('只有纯币租金时省略收费项,无拆分时不渲染', () => {
expect(
listingPriceDetail({
price_cent: 176350,
asset_summary: { pure_coin_price_cent: 176350, extra_item_price_cent: 0 },
})
).toBe('(纯币租金 ¥1,763.50)')
expect(listingPriceDetail({ price_cent: 176350, asset_summary: {} })).toBe('')
})
})
describe('resourceChargeText', () => {
it('区分赠送、收费与未标注', () => {
expect(resourceChargeText('赠送')).toBe('赠送')
expect(resourceChargeText('收费')).toBe('额外收费')
expect(resourceChargeText('收费', true)).toBe('收费')
expect(resourceChargeText('付费')).toBe('未标注')
expect(resourceChargeText('')).toBe('未标注')
expect(resourceChargeText(undefined)).toBe('未标注')
})
})
describe('listingResources', () => {
it('归一化模式、计算收费项小计并过滤空项', () => {
const rows = listingResources({
resources: [
{ key: 'ammo6', label: '6级弹', quantity: 50, mode: '收费', price_cent: 10 },
{ key: 'helmet6', label: '6头', quantity: 5, mode: '收费', price: '3.9元/个' },
{ key: 'med', label: '医疗包', quantity: 3, mode: '赠送', price_cent: 100 },
{ key: 'legacy', label: '旧数据', quantity: 2, mode: '付费', price: '2.5元/个' },
{ key: 'empty', label: '', quantity: 1, mode: '收费' },
{ key: 'zero', label: '零数量', quantity: 0, mode: '收费' },
],
})
expect(rows).toEqual([
{
label: '6级弹',
quantity: 50,
mode: 'paid',
modeText: '额外收费',
amountCent: 500,
amountText: '5.0',
},
{
label: '6头',
quantity: 5,
mode: 'paid',
modeText: '额外收费',
amountCent: 1950,
amountText: '19.5',
},
{
label: '医疗包',
quantity: 3,
mode: 'gift',
modeText: '赠送',
amountCent: 0,
amountText: '',
},
{
label: '旧数据',
quantity: 2,
mode: 'unknown',
modeText: '未标注',
amountCent: 0,
amountText: '',
},
])
})
it('缺少 resources 时返回空数组', () => {
expect(listingResources(undefined)).toEqual([])
expect(listingResources({})).toEqual([])
})
})
describe('resourceBadgeText', () => {
const base = { label: '6级弹', quantity: 50, modeText: '额外收费' } as const
it('收费项带小计金额,赠送与未标注用固定文案', () => {
expect(resourceBadgeText({ ...base, mode: 'paid', amountCent: 500, amountText: '5.0' })).toBe(
'额外收费 ¥5.0'
)
expect(
resourceBadgeText({ ...base, mode: 'paid', amountCent: 500, amountText: '5.0' }, 'compact')
).toBe('收费 ¥5.0')
expect(resourceBadgeText({ ...base, mode: 'paid', amountCent: 0, amountText: '' })).toBe(
'额外收费'
)
expect(resourceBadgeText({ ...base, mode: 'gift', amountCent: 0, amountText: '' })).toBe(
'随号赠送'
)
expect(
resourceBadgeText({ ...base, mode: 'gift', amountCent: 0, amountText: '' }, 'compact')
).toBe('赠送')
expect(resourceBadgeText({ ...base, mode: 'unknown', amountCent: 0, amountText: '' })).toBe(
'未标注'
)
})
})