Files
admin-web/src/modules/customers/components/CustomerBillingRulesPanel.test.ts
T

94 lines
5.4 KiB
TypeScript
Raw Normal View History

2026-08-05 15:54:59 +08:00
import ElementPlus from 'element-plus'
import { createPinia, setActivePinia } from 'pinia'
import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2026-08-07 20:07:37 +08:00
import { getCustomerDimensionConfig, updateCustomerDimensionConfig, type CustomerDimensionConfig } from '@/modules/dimension-config'
2026-08-05 15:54:59 +08:00
import { useAccessPreviewStore } from '@/core/access/accessPreview'
import { useAuthStore } from '@/core/auth/auth'
import CustomerBillingRulesPanel from './CustomerBillingRulesPanel.vue'
2026-08-07 20:07:37 +08:00
vi.mock('@/modules/dimension-config', () => ({ getCustomerDimensionConfig: vi.fn(), updateCustomerDimensionConfig: vi.fn() }))
2026-08-05 15:54:59 +08:00
2026-08-07 20:07:37 +08:00
const boxes = [
['SMALL', '小箱', 30, 20, 20, '标准小箱'],
['MEDIUM', '中箱', 40, 30, 30, '标准中箱'],
['LARGE', '大箱', 50, 40, 40, '标准大箱'],
['EXTRA_LARGE', '特大箱', 60, 50, 50, '特大箱'],
] as const
const config: CustomerDimensionConfig = {
2026-08-07 19:23:43 +08:00
tenantId: '8',
2026-08-07 20:07:37 +08:00
rules: boxes.map(([boxType, boxTypeName, innerLengthCm, innerWidthCm, innerHeightCm, remark], index) => ({
id: String(100 + index),
boxType, boxTypeName, innerLengthCm, innerWidthCm, innerHeightCm, edgeAdditionCm: 2,
outerLengthCm: innerLengthCm + 4, outerWidthCm: innerWidthCm + 4, outerHeightCm: innerHeightCm + 4,
volumeCbm: Number(((innerLengthCm + 4) * (innerWidthCm + 4) * (innerHeightCm + 4) / 1_000_000).toFixed(3)),
remark, version: 3, createdAt: '2026-08-07T10:00:00', updatedAt: '2026-08-07T10:20:00',
})),
2026-08-05 15:54:59 +08:00
}
enableAutoUnmount(afterEach)
describe('CustomerBillingRulesPanel', () => {
beforeEach(() => {
vi.clearAllMocks()
document.body.innerHTML = ''
2026-08-07 20:07:37 +08:00
const pinia = createPinia(); setActivePinia(pinia)
const auth = useAuthStore(); auth.token = 'token'; auth.permissions = ['firstleg:dimension-config:view', 'firstleg:dimension-config:edit']
2026-08-05 15:54:59 +08:00
useAccessPreviewStore().initializeAfterLogin()
2026-08-07 20:07:37 +08:00
vi.mocked(getCustomerDimensionConfig).mockResolvedValue(config)
vi.mocked(updateCustomerDimensionConfig).mockResolvedValue(config)
2026-08-05 15:54:59 +08:00
})
2026-08-07 20:07:37 +08:00
it('renders the four fixed carton dimension rows and calculated values', async () => {
mount(CustomerBillingRulesPanel, { attachTo: document.body, props: { customerId: '8', customerName: '柏悦物流' }, global: { plugins: [ElementPlus] } })
2026-08-05 15:54:59 +08:00
await flushPromises()
2026-08-07 19:23:43 +08:00
expect(getCustomerDimensionConfig).toHaveBeenCalledWith('8')
2026-08-07 20:07:37 +08:00
expect(document.body.textContent).toContain('按箱规尺寸计算')
2026-08-07 20:08:17 +08:00
expect(document.body.textContent).toContain('内径尺寸 + 外边加宽 × 2 = 外径尺寸')
2026-08-07 20:07:37 +08:00
expect(document.body.textContent).toContain('小箱')
expect(document.body.textContent).toContain('特大箱')
expect(document.body.textContent).toContain('34 × 24 × 24')
expect(document.body.textContent).toContain('0.020')
expect(document.body.textContent).toContain('标准中箱')
2026-08-05 15:54:59 +08:00
})
2026-08-12 12:35:32 +08:00
it('hides permission codes in the no-access state', async () => {
const auth = useAuthStore()
auth.permissions = []
useAccessPreviewStore().initializeAfterLogin()
mount(CustomerBillingRulesPanel, { attachTo: document.body, props: { customerId: '8', customerName: '柏悦物流' }, global: { plugins: [ElementPlus] } })
await flushPromises()
expect(document.body.textContent).toContain('暂无尺寸计算规则查看权限')
expect(document.body.textContent).toContain('请联系管理员申请尺寸计算规则查看权限')
expect(document.body.textContent).not.toContain('firstleg:dimension-config:view')
expect(getCustomerDimensionConfig).not.toHaveBeenCalled()
})
2026-08-07 20:07:37 +08:00
it('submits all four rows in one update request', async () => {
mount(CustomerBillingRulesPanel, { attachTo: document.body, props: { customerId: '8', customerName: '柏悦物流' }, global: { plugins: [ElementPlus] } })
2026-08-05 15:54:59 +08:00
await flushPromises()
2026-08-07 20:07:37 +08:00
;[...document.querySelectorAll<HTMLButtonElement>('button')].find((button) => button.textContent?.trim() === '编辑规则')?.click()
2026-08-05 15:54:59 +08:00
await flushPromises()
2026-08-07 20:07:37 +08:00
const saveButton = [...document.querySelectorAll<HTMLButtonElement>('button')].find((button) => button.textContent?.trim() === '保存规则')
saveButton?.click(); await flushPromises()
expect(updateCustomerDimensionConfig).toHaveBeenCalledWith('8', { rules: expect.arrayContaining([expect.objectContaining({ boxType: 'SMALL', version: 3 }), expect.objectContaining({ boxType: 'EXTRA_LARGE', version: 3 })]) })
const submittedRules = vi.mocked(updateCustomerDimensionConfig).mock.calls[0][1].rules
expect(submittedRules).toHaveLength(4)
expect(Object.keys(submittedRules[0]).sort()).toEqual([
'boxType', 'edgeAdditionCm', 'innerHeightCm', 'innerLengthCm', 'innerWidthCm', 'remark', 'version',
])
2026-08-07 19:23:43 +08:00
})
2026-08-07 20:07:37 +08:00
it('keeps the editor open when the whole-set save fails', async () => {
vi.mocked(updateCustomerDimensionConfig).mockRejectedValueOnce(new Error('规则版本冲突'))
mount(CustomerBillingRulesPanel, { attachTo: document.body, props: { customerId: '8', customerName: '柏悦物流' }, global: { plugins: [ElementPlus] } })
2026-08-07 19:23:43 +08:00
await flushPromises()
2026-08-07 20:07:37 +08:00
;[...document.querySelectorAll<HTMLButtonElement>('button')].find((button) => button.textContent?.trim() === '编辑规则')?.click(); await flushPromises()
;[...document.querySelectorAll<HTMLButtonElement>('button')].find((button) => button.textContent?.trim() === '保存规则')?.click()
2026-08-07 19:23:43 +08:00
await flushPromises()
2026-08-07 20:07:37 +08:00
expect(document.body.textContent).toContain('编辑箱规尺寸')
2026-08-05 15:54:59 +08:00
})
})