Files
admin-web/src/modules/trucking/pages/TruckAppointmentsView.test.ts
T

129 lines
5.9 KiB
TypeScript
Raw Normal View History

2026-08-08 13:32:27 +08:00
import ElementPlus from 'element-plus'
import { createPinia, setActivePinia } from 'pinia'
import { flushPromises, mount } from '@vue/test-utils'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import TruckAppointmentsView from './TruckAppointmentsView.vue'
import { listTruckAppointments } from '../api/platformTrucking'
import type { TruckAppointmentResource } from '../types'
2026-08-12 12:35:32 +08:00
import { useAuthStore } from '@/core/auth/auth'
2026-08-10 16:24:54 +08:00
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
const source = readFileSync(resolve(process.cwd(), 'src/modules/trucking/pages/TruckAppointmentsView.vue'), 'utf8')
2026-08-08 13:32:27 +08:00
vi.mock('../api/platformTrucking', () => ({
listTruckAppointments: vi.fn(),
}))
const row: TruckAppointmentResource = {
2026-08-08 13:46:21 +08:00
id: '9001', appointmentNo: 'TA-9001', status: 'DISPATCHED', customerName: '示例企业', orderNo: 'ORD-1', containerBookingId: '77',
containerNo: 'CONT-1', carrierName: '宁波运输', driverName: '王强', vehicleNo: '浙B12345',
plannedLoadingAt: '2026-08-10T10:00:00', payableAmount: '3200.00', receivableAmount: '3800.00', attachments: [],
2026-08-08 13:32:27 +08:00
}
describe('TruckAppointmentsView', () => {
beforeEach(() => {
vi.mocked(listTruckAppointments).mockReset().mockResolvedValue({ records: [row], page: 1, size: 20, total: 1, pages: 1 })
setActivePinia(createPinia())
2026-08-12 12:35:32 +08:00
useAuthStore().permissions = [
'platform:firstleg:truck-appointment:view',
'platform:firstleg:truck-appointment:edit',
]
2026-08-08 13:32:27 +08:00
})
it('loads the real list and renders relationship snapshots', async () => {
const wrapper = mount(TruckAppointmentsView, { global: { plugins: [ElementPlus] } })
await flushPromises()
2026-08-08 15:05:37 +08:00
expect(listTruckAppointments).toHaveBeenCalledWith({ page: 1, size: 20, keyword: '', status: '', containerBookingId: '', containerType: '' })
2026-08-08 13:32:27 +08:00
expect(wrapper.text()).toContain('TA-9001')
2026-08-12 10:59:49 +08:00
expect(wrapper.text()).toContain('CONT-1')
expect(wrapper.text()).not.toContain('77')
2026-08-08 13:32:27 +08:00
expect(wrapper.text()).toContain('宁波运输')
expect(wrapper.find('button[aria-label="查看 TA-9001"]').exists()).toBe(true)
})
2026-08-10 16:24:54 +08:00
2026-08-10 17:42:17 +08:00
it('uses stable accessible labels when appointment identifiers are missing', async () => {
vi.mocked(listTruckAppointments).mockResolvedValue({
records: [
{ ...row, id: '9002', appointmentNo: '' },
{ ...row, id: undefined, appointmentNo: null } as unknown as TruckAppointmentResource,
],
page: 1,
size: 20,
total: 2,
pages: 1,
})
const wrapper = mount(TruckAppointmentsView, { global: { plugins: [ElementPlus] } })
await flushPromises()
expect(wrapper.find('button[aria-label="查看 9002"]').exists()).toBe(true)
expect(wrapper.find('button[aria-label="查看拖车预约详情"]').exists()).toBe(true)
const labels = wrapper.findAll('button[aria-label]').map((button) => button.attributes('aria-label'))
expect(labels.every((label) => Boolean(label) && !/undefined|null/.test(label ?? ''))).toBe(true)
expect(source).toContain("appointmentActionLabel('编辑', row as TruckAppointmentResource)")
})
2026-08-12 12:35:32 +08:00
it('does not expose a technical carrier id when the business name is unavailable', async () => {
vi.mocked(listTruckAppointments).mockResolvedValue({
records: [{ ...row, carrierName: '', carrierId: 'carrier-77' }],
page: 1,
size: 20,
total: 1,
pages: 1,
})
const wrapper = mount(TruckAppointmentsView, { global: { plugins: [ElementPlus] } })
await flushPromises()
expect(wrapper.text()).toContain('已关联承运商')
expect(wrapper.text()).not.toContain('carrier-77')
})
2026-08-10 16:24:54 +08:00
it('uses the table toolbar for create and keeps a single empty state', () => {
expect(source).toContain('<template #toolbar-actions>')
expect(source).not.toContain('<template #toolbar-start>')
expect(source).not.toContain('PageToolbar')
expect(source).not.toContain('empty-hint')
expect(source.match(/empty-text=/g)).toHaveLength(1)
})
2026-08-10 17:42:17 +08:00
it('shows the two frequent row actions directly without a more menu', () => {
2026-08-13 11:57:44 +08:00
expect(source).not.toContain('platform:firstleg:truck-appointment:view')
2026-08-12 14:08:33 +08:00
expect(source).toContain("access.can('platform:firstleg:truck-appointment:edit') && ['DRAFT', 'PENDING_DISPATCH', 'RESCHEDULED'].includes(row.status)")
2026-08-10 17:42:17 +08:00
expect(source).toContain('<Eye :size="15" />详情')
expect(source).toContain('<Pencil :size="15" />编辑')
expect(source).not.toContain('MoreHorizontal')
})
2026-08-12 12:35:32 +08:00
2026-08-12 14:08:33 +08:00
it('hides edit for completed appointments rejected by the backend edit contract', async () => {
vi.mocked(listTruckAppointments).mockResolvedValue({
records: [{ ...row, status: 'COMPLETED' }],
page: 1,
size: 20,
total: 1,
pages: 1,
})
const wrapper = mount(TruckAppointmentsView, { global: { plugins: [ElementPlus] } })
await flushPromises()
expect(wrapper.find('button[aria-label="编辑 TA-9001"]').exists()).toBe(false)
})
2026-08-13 11:57:44 +08:00
it('shows the detail action with the list permission', async () => {
2026-08-12 12:35:32 +08:00
useAuthStore().permissions = ['platform:firstleg:truck-appointment:list']
const wrapper = mount(TruckAppointmentsView, { global: { plugins: [ElementPlus] } })
await flushPromises()
2026-08-13 11:57:44 +08:00
expect(wrapper.find('button[aria-label="查看 TA-9001"]').exists()).toBe(true)
2026-08-12 12:35:32 +08:00
})
2026-08-12 17:34:06 +08:00
it('prioritizes outbound business fields and keeps finance amounts out of the list', () => {
expect(source).toContain("key: 'customerName'")
expect(source).toContain("key: 'updatedAt'")
expect(source).not.toContain("key: 'payableAmount'")
expect(source).not.toContain("key: 'receivableAmount'")
})
2026-08-12 17:45:49 +08:00
it('uses bordered list actions instead of text links', () => {
expect(source).toContain('size="small" type="primary" plain')
expect(source).toContain('size="small" :aria-label="appointmentActionLabel(\'编辑\'')
expect(source).not.toContain("truck-appointment:view')\" link")
2026-08-12 18:16:44 +08:00
expect(source).toContain(':total="total" stripe preference-key="trucking-appointments"')
2026-08-12 17:45:49 +08:00
})
2026-08-08 13:32:27 +08:00
})