Files
admin-web/src/modules/iam/components/PlatformTenantSelector.test.ts
T
2026-08-14 12:50:42 +08:00

99 lines
4.1 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 ElementPlus from 'element-plus'
import { flushPromises, mount } from '@vue/test-utils'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { listPlatformTenants, type PlatformTenantResource } from '../api/platformIam'
import PlatformTenantSelector from './PlatformTenantSelector.vue'
vi.mock('../api/platformIam', () => ({ listPlatformTenants: vi.fn() }))
const customer = {
id: '190000000000000008',
tenantCode: 'CUS-008',
tenantName: '柏悦物流',
status: 'ACTIVE',
} as PlatformTenantResource
function mountSelector(props: { modelValue?: string; label?: string } = {}) {
return mount(PlatformTenantSelector, {
props: { modelValue: props.modelValue ?? '', ...(props.label ? { label: props.label } : {}) },
global: { plugins: [ElementPlus] },
})
}
describe('PlatformTenantSelector customer labels', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.mocked(listPlatformTenants).mockResolvedValue({ records: [customer], page: 1, size: 200, total: 1, pages: 1 })
})
it('shows business identity and status without exposing the internal id', async () => {
const wrapper = mountSelector({ modelValue: String(customer.id) })
await flushPromises()
expect(listPlatformTenants).toHaveBeenCalledWith({ page: 1, size: 200, status: 'ACTIVE' })
expect(wrapper.text()).toContain('目标客户')
expect(document.body.textContent).toContain('柏悦物流 · 启用')
expect(document.body.textContent).not.toContain('CUS-008')
expect(document.body.textContent).not.toContain(String(customer.id))
})
it('keeps the internal id only as the emitted contract value', async () => {
const wrapper = mountSelector()
await flushPromises()
expect(wrapper.emitted('update:modelValue')).toEqual([[String(customer.id)]])
expect(wrapper.text()).not.toContain(String(customer.id))
})
it('keeps non-active customers out of the selectable options', async () => {
vi.mocked(listPlatformTenants).mockResolvedValue({
records: [customer, { ...customer, id: '9', tenantName: '停用客户', status: 'DISABLED' }],
page: 1, size: 200, total: 2, pages: 1,
})
mountSelector({ modelValue: String(customer.id) })
await flushPromises()
expect(document.body.textContent).toContain('柏悦物流 · 启用')
expect(document.body.textContent).not.toContain('停用客户')
})
it('safely degrades labels when customer name or code is missing', async () => {
vi.mocked(listPlatformTenants).mockResolvedValue({
records: [
{ ...customer, id: '1', tenantName: '', tenantCode: 'CUS-001', status: 'ACTIVE' },
{ ...customer, id: '2', tenantName: '远航供应链', tenantCode: '', status: 'ACTIVE' },
{ ...customer, id: '3', tenantName: '', tenantCode: '', status: 'ACTIVE' },
],
page: 1, size: 200, total: 3, pages: 1,
})
mountSelector({ modelValue: '1' })
await flushPromises()
expect(document.body.textContent).toContain('未命名客户 · 启用')
expect(document.body.textContent).toContain('远航供应链 · 启用')
expect(document.body.textContent).not.toContain('CUS-001')
expect(document.body.textContent).not.toMatch(/[((][123] ·/)
})
it('uses a custom business label for both visible and accessible names', async () => {
const wrapper = mountSelector({ modelValue: String(customer.id), label: '客户企业' })
await flushPromises()
expect(wrapper.text()).toContain('客户企业')
expect(wrapper.get('[aria-label="客户企业"]').attributes('aria-label')).toBe('客户企业')
})
it('shows an actionable customer-list error and retries', async () => {
vi.mocked(listPlatformTenants)
.mockRejectedValueOnce('unavailable')
.mockResolvedValueOnce({ records: [customer], page: 1, size: 200, total: 1, pages: 1 })
const wrapper = mountSelector()
await flushPromises()
expect(wrapper.get('[role="alert"]').text()).toContain('客户列表加载失败,请重试')
await wrapper.get('[role="alert"] button').trigger('click')
await flushPromises()
expect(listPlatformTenants).toHaveBeenCalledTimes(2)
})
})