2026-08-31 21:05:03 +08:00
|
|
|
import ElementPlus from 'element-plus'
|
|
|
|
|
import { flushPromises, mount } from '@vue/test-utils'
|
|
|
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
|
import { ApiResponseError } from '@/shared/api/http'
|
|
|
|
|
import TruckAppointmentForm from './TruckAppointmentForm.vue'
|
2026-09-01 15:46:46 +08:00
|
|
|
import type { SaveTruckAppointmentRequest, TruckAppointmentResource } from '../types'
|
|
|
|
|
import { getOceanBooking } from '@/modules/ocean-bookings'
|
|
|
|
|
import { listTruckAppointmentCandidates } from '../api/platformTrucking'
|
2026-08-31 21:05:03 +08:00
|
|
|
|
|
|
|
|
vi.mock('@/modules/suppliers', () => ({
|
|
|
|
|
listSuppliers: vi.fn().mockResolvedValue({ records: [{ id: 'carrier-1', name: '平台拖车公司', category: 'TRUCKING', status: 'ACTIVE' }] }),
|
|
|
|
|
}))
|
|
|
|
|
vi.mock('@/modules/ocean-bookings', () => ({
|
|
|
|
|
getOceanBooking: vi.fn(),
|
|
|
|
|
}))
|
2026-09-01 15:46:46 +08:00
|
|
|
vi.mock('../api/platformTrucking', () => ({ listTruckAppointmentCandidates: vi.fn() }))
|
2026-08-31 21:05:03 +08:00
|
|
|
|
|
|
|
|
const model: SaveTruckAppointmentRequest = {
|
|
|
|
|
expectedVersion: 0,
|
|
|
|
|
oceanBookingId: null,
|
|
|
|
|
containerBookingId: null,
|
|
|
|
|
loadingPlanId: null,
|
|
|
|
|
carrierId: null,
|
|
|
|
|
plannedLoadingAt: null,
|
|
|
|
|
}
|
2026-09-01 15:46:46 +08:00
|
|
|
const candidates = [
|
|
|
|
|
{ oceanBookingId: 'booking-1', oceanBookingNo: 'OB-1', billOfLadingNo: 'BL-1', warehouseId: 'WH-1', warehouseName: '义乌仓', channelCode: 'SEA', channelName: '海运', requirementId: 'requirement-1', containerType: '40HC', requirementQuantity: 2, containerBookingId: 'container-1', cabinetNo: 'CAB-1', containerNo: 'CBHU8888883', loadingPlanId: 'plan-1', loadingPlanNo: 'LP-1', loadingPlanStatus: 'EDITING', bookingDocumentReady: true },
|
|
|
|
|
{ oceanBookingId: 'booking-1', oceanBookingNo: 'OB-1', billOfLadingNo: 'BL-1', warehouseId: 'WH-1', warehouseName: '义乌仓', channelCode: 'SEA', channelName: '海运', requirementId: 'requirement-1', containerType: '40HC', requirementQuantity: 2, containerBookingId: 'container-pending', cabinetNo: 'CAB-X', containerNo: null, loadingPlanId: null, loadingPlanNo: null, loadingPlanStatus: null, bookingDocumentReady: true },
|
|
|
|
|
{ oceanBookingId: 'booking-2', oceanBookingNo: 'OB-2', billOfLadingNo: 'BL-2', warehouseId: 'WH-2', warehouseName: '东莞仓', channelCode: 'SEA', channelName: '海运', requirementId: 'requirement-2', containerType: '40GP', requirementQuantity: 1, containerBookingId: 'container-2', cabinetNo: 'CAB-2', containerNo: 'MSCU6639870', loadingPlanId: null, loadingPlanNo: null, loadingPlanStatus: null, bookingDocumentReady: true },
|
2026-08-31 21:05:03 +08:00
|
|
|
]
|
|
|
|
|
function detail(id: string) {
|
2026-09-01 15:46:46 +08:00
|
|
|
const candidate = candidates.find((item) => item.oceanBookingId === id)!
|
2026-08-31 21:05:03 +08:00
|
|
|
return {
|
2026-09-01 15:46:46 +08:00
|
|
|
id, bookingNo: candidate.oceanBookingNo, status: 'RELEASED', billOfLadingNo: candidate.billOfLadingNo, warehouseId: candidate.warehouseId, warehouseName: candidate.warehouseName,
|
2026-08-31 21:05:03 +08:00
|
|
|
documents: [{ id: `doc-${id}`, documentType: 'BILL_OF_LADING', valid: true }],
|
2026-09-01 15:46:46 +08:00
|
|
|
containers: [],
|
2026-08-31 21:05:03 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-09-01 15:46:46 +08:00
|
|
|
function resource(id: string, bookingId: string, containerId: string): TruckAppointmentResource {
|
|
|
|
|
const candidate = candidates.find((item) => item.oceanBookingId === bookingId && item.containerBookingId === containerId)!
|
|
|
|
|
return { id, status: 'DRAFT', oceanBookingId: bookingId, oceanBookingNo: candidate.oceanBookingNo, containerBookingId: containerId, cabinetNo: candidate.cabinetNo, containerNo: candidate.containerNo, relatedContainerType: candidate.containerType }
|
|
|
|
|
}
|
2026-08-31 21:05:03 +08:00
|
|
|
|
|
|
|
|
describe('TruckAppointmentForm booking-only behavior', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks()
|
2026-09-01 15:46:46 +08:00
|
|
|
vi.mocked(listTruckAppointmentCandidates).mockResolvedValue({ records: candidates, page: 1, size: 200, total: candidates.length, pages: 1 })
|
2026-08-31 21:05:03 +08:00
|
|
|
vi.mocked(getOceanBooking).mockImplementation(async (id) => detail(id) as never)
|
|
|
|
|
})
|
|
|
|
|
|
2026-09-01 15:24:37 +08:00
|
|
|
it('selects a released booking, exposes pending-pickup internal containers, and never emits customer fields', async () => {
|
2026-08-31 21:05:03 +08:00
|
|
|
const wrapper = mount(TruckAppointmentForm, { props: { modelValue: { ...model } }, global: { plugins: [ElementPlus] } })
|
|
|
|
|
await flushPromises()
|
|
|
|
|
const selects = wrapper.findAllComponents({ name: 'ElSelect' })
|
|
|
|
|
selects[0]!.vm.$emit('update:modelValue', 'booking-1')
|
|
|
|
|
await flushPromises()
|
|
|
|
|
|
2026-09-01 15:46:46 +08:00
|
|
|
expect(listTruckAppointmentCandidates).toHaveBeenCalledWith({ page: 1, size: 200 })
|
|
|
|
|
expect(getOceanBooking).not.toHaveBeenCalled()
|
2026-08-31 21:05:03 +08:00
|
|
|
const labels = wrapper.findAllComponents({ name: 'ElOption' }).map((option) => String(option.props('label') || ''))
|
|
|
|
|
expect(labels.some((label) => label.includes('CAB-1') && label.includes('CBHU8888883'))).toBe(true)
|
2026-09-01 15:24:37 +08:00
|
|
|
expect(labels.some((label) => label.includes('CAB-X') && label.includes('40HC') && label.includes('待补录'))).toBe(true)
|
2026-08-31 21:05:03 +08:00
|
|
|
const emitted = wrapper.emitted('update:modelValue')?.at(-1)?.[0] as SaveTruckAppointmentRequest
|
|
|
|
|
expect(emitted.oceanBookingId).toBe('booking-1')
|
|
|
|
|
expect(emitted.billOfLadingNo).toBe('BL-1')
|
|
|
|
|
expect(emitted).not.toHaveProperty('customerId')
|
|
|
|
|
expect(emitted).not.toHaveProperty('customerName')
|
|
|
|
|
wrapper.unmount()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('clears a selected container when switching to another booking', async () => {
|
|
|
|
|
const wrapper = mount(TruckAppointmentForm, { props: { modelValue: { ...model } }, global: { plugins: [ElementPlus] } })
|
|
|
|
|
await flushPromises()
|
|
|
|
|
const selects = wrapper.findAllComponents({ name: 'ElSelect' })
|
|
|
|
|
selects[0]!.vm.$emit('update:modelValue', 'booking-1')
|
|
|
|
|
await flushPromises()
|
|
|
|
|
selects[1]!.vm.$emit('update:modelValue', 'container-1')
|
|
|
|
|
await flushPromises()
|
|
|
|
|
expect((wrapper.emitted('update:modelValue')?.at(-1)?.[0] as SaveTruckAppointmentRequest).containerBookingId).toBe('container-1')
|
|
|
|
|
|
|
|
|
|
selects[0]!.vm.$emit('update:modelValue', 'booking-2')
|
|
|
|
|
await flushPromises()
|
|
|
|
|
const emitted = wrapper.emitted('update:modelValue')?.at(-1)?.[0] as SaveTruckAppointmentRequest
|
|
|
|
|
expect(emitted.oceanBookingId).toBe('booking-2')
|
|
|
|
|
expect(emitted.containerBookingId).toBeNull()
|
|
|
|
|
const labels = wrapper.findAllComponents({ name: 'ElOption' }).map((option) => String(option.props('label') || ''))
|
|
|
|
|
expect(labels.some((label) => label.includes('CAB-2'))).toBe(true)
|
|
|
|
|
wrapper.unmount()
|
|
|
|
|
})
|
|
|
|
|
|
2026-09-01 15:46:46 +08:00
|
|
|
it('ignores stale current-booking details when a cached edit route changes', async () => {
|
2026-09-01 15:43:19 +08:00
|
|
|
let resolveFirst!: (value: ReturnType<typeof detail>) => void
|
|
|
|
|
let resolveSecond!: (value: ReturnType<typeof detail>) => void
|
|
|
|
|
vi.mocked(getOceanBooking).mockImplementation((id) => new Promise((resolve) => {
|
|
|
|
|
if (id === 'booking-1') resolveFirst = resolve as typeof resolveFirst
|
|
|
|
|
else resolveSecond = resolve as typeof resolveSecond
|
|
|
|
|
}) as never)
|
2026-09-01 15:46:46 +08:00
|
|
|
const firstResource = resource('appointment-1', 'booking-1', 'container-1')
|
|
|
|
|
const secondResource = resource('appointment-2', 'booking-2', 'container-2')
|
|
|
|
|
const wrapper = mount(TruckAppointmentForm, { props: { modelValue: { ...model, oceanBookingId: 'booking-1', containerBookingId: 'container-1' }, resource: firstResource }, global: { plugins: [ElementPlus] } })
|
2026-09-01 15:43:19 +08:00
|
|
|
await flushPromises()
|
2026-09-01 15:46:46 +08:00
|
|
|
await wrapper.setProps({ modelValue: { ...model, oceanBookingId: 'booking-2', containerBookingId: 'container-2' }, resource: secondResource })
|
2026-09-01 15:43:19 +08:00
|
|
|
await flushPromises()
|
|
|
|
|
|
|
|
|
|
resolveSecond(detail('booking-2'))
|
|
|
|
|
await flushPromises()
|
|
|
|
|
resolveFirst({ ...detail('booking-1'), documents: [] })
|
|
|
|
|
await flushPromises()
|
|
|
|
|
|
|
|
|
|
const labels = wrapper.findAllComponents({ name: 'ElOption' }).map((option) => String(option.props('label') || ''))
|
2026-09-01 15:46:46 +08:00
|
|
|
expect(labels.some((label) => label.includes('CAB-2') && label.includes('不可重选'))).toBe(true)
|
2026-09-01 15:43:19 +08:00
|
|
|
expect(labels.some((label) => label.includes('CAB-1'))).toBe(false)
|
|
|
|
|
expect(wrapper.text()).not.toContain('所选订舱缺少有效的订舱确认文件或提单文件')
|
2026-09-01 15:46:46 +08:00
|
|
|
expect(listTruckAppointmentCandidates).not.toHaveBeenCalled()
|
2026-09-01 15:43:19 +08:00
|
|
|
wrapper.unmount()
|
|
|
|
|
})
|
|
|
|
|
|
2026-08-31 21:05:03 +08:00
|
|
|
it('preserves the business code and request trace when booking readiness fails', async () => {
|
|
|
|
|
vi.mocked(getOceanBooking).mockRejectedValueOnce(new ApiResponseError('订舱版本冲突', 'A0409', 409, undefined, 'req-booking', 'trace-booking'))
|
2026-09-01 15:46:46 +08:00
|
|
|
const current = resource('appointment-1', 'booking-1', 'container-1')
|
|
|
|
|
const wrapper = mount(TruckAppointmentForm, { props: { modelValue: { ...model, oceanBookingId: 'booking-1', containerBookingId: 'container-1' }, resource: current }, global: { plugins: [ElementPlus] } })
|
2026-08-31 21:05:03 +08:00
|
|
|
await flushPromises()
|
|
|
|
|
expect(wrapper.text()).toContain('订舱版本冲突')
|
|
|
|
|
expect(wrapper.text()).toContain('A0409')
|
|
|
|
|
expect(wrapper.text()).toContain('req-booking')
|
|
|
|
|
expect(wrapper.text()).toContain('trace-booking')
|
|
|
|
|
wrapper.unmount()
|
|
|
|
|
})
|
|
|
|
|
|
2026-09-01 15:46:46 +08:00
|
|
|
it('flags an edit binding whose booking has become unreleased without adding it to create candidates', async () => {
|
2026-08-31 21:05:03 +08:00
|
|
|
vi.mocked(getOceanBooking).mockResolvedValueOnce({ ...detail('booking-1'), status: 'DRAFT' } as never)
|
2026-09-01 15:46:46 +08:00
|
|
|
const current = resource('appointment-1', 'booking-1', 'container-1')
|
|
|
|
|
const wrapper = mount(TruckAppointmentForm, { props: { modelValue: { ...model, oceanBookingId: 'booking-1', containerBookingId: 'container-1' }, resource: current }, global: { plugins: [ElementPlus] } })
|
2026-08-31 21:05:03 +08:00
|
|
|
await flushPromises()
|
2026-09-01 15:46:46 +08:00
|
|
|
expect(wrapper.text()).toContain('当前绑定订舱已不是放舱状态')
|
|
|
|
|
expect(listTruckAppointmentCandidates).not.toHaveBeenCalled()
|
2026-08-31 21:05:03 +08:00
|
|
|
wrapper.unmount()
|
|
|
|
|
})
|
2026-09-01 10:36:49 +08:00
|
|
|
|
|
|
|
|
it('switches the payable currency between RMB and USD and normalizes an illegal legacy value', async () => {
|
|
|
|
|
const wrapper = mount(TruckAppointmentForm, {
|
|
|
|
|
props: { modelValue: { ...model, payableAmount: '88.00', currency: 'EUR' as never } },
|
|
|
|
|
global: { plugins: [ElementPlus] },
|
|
|
|
|
})
|
|
|
|
|
await flushPromises()
|
|
|
|
|
const currencySelect = wrapper.findAllComponents({ name: 'ElSelect' }).find((select) => select.props('placeholder') === '请选择币种')!
|
|
|
|
|
expect(currencySelect.props('modelValue')).toBe('RMB')
|
|
|
|
|
currencySelect.vm.$emit('update:modelValue', 'USD')
|
|
|
|
|
await flushPromises()
|
|
|
|
|
const emitted = wrapper.emitted('update:modelValue')?.at(-1)?.[0] as SaveTruckAppointmentRequest
|
|
|
|
|
expect(emitted.currency).toBe('USD')
|
|
|
|
|
expect(wrapper.text()).toContain('USD')
|
|
|
|
|
expect(emitted).not.toHaveProperty('receivableAmount')
|
|
|
|
|
wrapper.unmount()
|
|
|
|
|
})
|
2026-08-31 21:05:03 +08:00
|
|
|
})
|