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

85 lines
3.2 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'
import {
getCustomerPricingRule,
listCustomerPricingRules,
type CustomerPricingRule,
} from '@/modules/fba-pricing'
import { useAccessPreviewStore } from '@/core/access/accessPreview'
import { useAuthStore } from '@/core/auth/auth'
import CustomerBillingRulesPanel from './CustomerBillingRulesPanel.vue'
vi.mock('@/modules/fba-pricing', () => ({
createCustomerPricingRuleVersion: vi.fn(),
disableCustomerPricingRule: vi.fn(),
getCustomerPricingRule: vi.fn(),
listCustomerPricingRules: vi.fn(),
}))
class ResizeObserverStub implements ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
}
const rule: CustomerPricingRule = {
id: '91', tenantId: '8', ruleCode: 'FBA-SEA-KG', customerId: '8', customerName: '柏悦物流',
businessType: 'FBA_SEA', chargeMode: 'KG', taxMode: 'TAX_INCLUDED', conversionFactor: 167,
roundingScale: 3, roundingMode: 'CEILING', currency: 'CNY', effectiveFrom: '2026-08-01T00:00:00',
effectiveTo: null, ruleVersion: 2, status: 'ACTIVE', remark: '标准规则', createdAt: null, updatedAt: null,
}
enableAutoUnmount(afterEach)
describe('CustomerBillingRulesPanel', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.stubGlobal('ResizeObserver', ResizeObserverStub)
document.body.innerHTML = ''
const pinia = createPinia()
setActivePinia(pinia)
const auth = useAuthStore()
auth.token = 'token'
auth.permissions = ['firstleg:fba-pricing:rule:view', 'firstleg:fba-pricing:rule:edit']
useAccessPreviewStore().initializeAfterLogin()
vi.mocked(listCustomerPricingRules).mockResolvedValue([rule])
vi.mocked(getCustomerPricingRule).mockResolvedValue(rule)
})
it('loads only the current customer rules and exposes version actions', async () => {
mount(CustomerBillingRulesPanel, {
attachTo: document.body,
props: { customerId: '8', customerName: '柏悦物流', customerCode: 'TEN8' },
global: { plugins: [ElementPlus] },
})
await flushPromises()
expect(listCustomerPricingRules).toHaveBeenCalledWith({ tenantId: '8' })
expect(document.body.textContent).toContain('FBA-SEA-KG')
expect(document.body.textContent).toContain('FBA 海运')
expect(document.body.textContent).toContain('新建规则')
expect(document.body.textContent).toContain('新版本')
expect(document.body.textContent).toContain('停用')
})
it('loads rule details with the customer tenant scope', async () => {
mount(CustomerBillingRulesPanel, {
attachTo: document.body,
props: { customerId: '8', customerName: '柏悦物流' },
global: { plugins: [ElementPlus] },
})
await flushPromises()
const detailButtons = [...document.querySelectorAll<HTMLButtonElement>('button')]
.filter((button) => button.textContent?.trim() === '详情')
const detailButton = detailButtons.at(-1)
detailButton?.click()
await flushPromises()
expect(getCustomerPricingRule).toHaveBeenCalledWith('8', '91')
expect(document.body.textContent).toContain('标准规则')
})
})