Files
admin-web/src/modules/overseas-warehouse/pages/LastMileOrdersView.behavior.test.ts
T

118 lines
6.1 KiB
TypeScript
Raw Normal View History

import ElementPlus from 'element-plus'
import { createPinia } from 'pinia'
import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils'
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2026-08-27 17:16:38 +08:00
import { ApiResponseError } from '@/shared/api/http'
import { convertLastMileContainerToInboundPlans, convertLastMileOrderToInboundPlan, listLastMileOrders, listPendingContainerSummaries } from '../api/lastMileOrders'
import LastMileOrdersView from './LastMileOrdersView.vue'
vi.mock('@/core/access/accessPreview', () => ({ useAccessPreviewStore: () => ({ can: () => true }) }))
vi.mock('vue-router', () => ({ useRouter: () => ({ push: vi.fn() }) }))
vi.mock('../api/lastMileOrders', () => ({
2026-08-27 17:16:38 +08:00
listLastMileOrders: vi.fn(), listPendingContainerSummaries: vi.fn(),
convertLastMileContainerToInboundPlans: vi.fn(), convertLastMileOrderToInboundPlan: vi.fn(), transferLastMileOrders: vi.fn(),
}))
class ResizeObserverStub implements ResizeObserver { observe() {} unobserve() {} disconnect() {} }
function memoryStorage(): Storage {
const values = new Map<string, string>()
return {
2026-08-27 16:42:32 +08:00
get length() { return values.size }, clear: () => values.clear(), getItem: (key) => values.get(key) ?? null,
key: (index) => [...values.keys()][index] ?? null, removeItem: (key) => values.delete(key), setItem: (key, value) => values.set(key, value),
}
}
2026-08-27 17:16:38 +08:00
const summary = {
sourceContainerBookingId: '2092873463568597001', containerNo: 'CONT-15', orderCount: 15,
inboundCodeCount: 15, customerCount: 3, totalPackageCount: 150, totalPalletCount: 30,
totalVolumeCbm: 45.5, eligible: true, ineligibleReason: null,
}
function plans(count = 15) {
return Array.from({ length: count }, (_, index) => ({
id: String(index + 1), planNo: `LIP-${index + 1}`, customerId: String(index % 3 + 1), sourceOrderId: String(index + 1),
containerNo: 'CONT-15', status: 'PENDING_ASSIGNMENT', lineCount: 1, palletCount: 1, totalVolumeCbm: 1,
lines: [], pallets: [], allocations: [],
}))
}
enableAutoUnmount(afterEach)
afterAll(() => vi.unstubAllGlobals())
2026-08-27 17:16:38 +08:00
async function openPending() {
const wrapper = mount(LastMileOrdersView, { attachTo: document.body, global: { plugins: [createPinia(), ElementPlus] } })
await flushPromises()
const pendingTab = wrapper.findAll('.el-radio-button').find((item) => item.text().includes('待分配'))
await pendingTab!.trigger('click')
await flushPromises()
return wrapper
}
describe('LastMileOrdersView server-side container summaries', () => {
beforeEach(() => {
vi.stubGlobal('ResizeObserver', ResizeObserverStub)
vi.stubGlobal('localStorage', memoryStorage())
vi.mocked(listLastMileOrders).mockReset().mockResolvedValue({ records: [], page: 1, size: 10, total: 0, pages: 0 })
2026-08-27 17:16:38 +08:00
vi.mocked(listPendingContainerSummaries).mockReset().mockResolvedValue({ records: [summary], page: 1, size: 10, total: 21, pages: 3 })
vi.mocked(convertLastMileContainerToInboundPlans).mockReset().mockResolvedValue(plans())
2026-08-27 16:42:32 +08:00
vi.mocked(convertLastMileOrderToInboundPlan).mockReset()
})
2026-08-27 17:16:38 +08:00
it('uses one server summary for a 15-order container that could span order pages', async () => {
const wrapper = await openPending()
expect(listPendingContainerSummaries).toHaveBeenCalledWith({ page: 1, size: 10, containerNo: undefined, createdFrom: undefined, createdTo: undefined })
expect(wrapper.text()).toContain('CONT-15')
expect(wrapper.text()).toContain('15')
expect(wrapper.text()).toContain('将为 15 个尾程订单生成 15 个独立入库计划')
2026-08-27 16:42:32 +08:00
expect(listLastMileOrders).toHaveBeenCalledTimes(1)
2026-08-27 17:16:38 +08:00
})
it('requests the next server summary page instead of deriving containers from order rows', async () => {
const wrapper = await openPending()
const nextPage = wrapper.find('button[aria-label="第 2 页"]')
expect(nextPage.exists()).toBe(true)
await nextPage.trigger('click')
await flushPromises()
expect(listPendingContainerSummaries).toHaveBeenLastCalledWith({
page: 2, size: 10, containerNo: undefined, createdFrom: undefined, createdTo: undefined,
})
expect(listLastMileOrders).toHaveBeenCalledTimes(1)
})
it('calls convert-container once, validates N plans and refreshes the server summary', async () => {
const wrapper = await openPending()
const button = wrapper.findAll('button').find((item) => item.text().includes('生成整柜入库计划'))
await button!.trigger('click')
await flushPromises()
expect(convertLastMileContainerToInboundPlans).toHaveBeenCalledTimes(1)
expect(convertLastMileContainerToInboundPlans).toHaveBeenCalledWith({ sourceContainerBookingId: '2092873463568597001' })
2026-08-27 16:34:32 +08:00
expect(convertLastMileOrderToInboundPlan).not.toHaveBeenCalled()
2026-08-27 17:16:38 +08:00
expect(listPendingContainerSummaries).toHaveBeenCalledTimes(2)
expect(document.body.textContent).toContain('已为15个尾程订单生成15个入库计划')
})
it('does not report success or refresh when N returned plans do not match N orders', async () => {
vi.mocked(convertLastMileContainerToInboundPlans).mockResolvedValueOnce(plans(14))
const wrapper = await openPending()
const button = wrapper.findAll('button').find((item) => item.text().includes('生成整柜入库计划'))
await button!.trigger('click')
await flushPromises()
expect(convertLastMileContainerToInboundPlans).toHaveBeenCalledTimes(1)
expect(listPendingContainerSummaries).toHaveBeenCalledTimes(1)
expect(wrapper.text()).toContain('返回 14 个计划')
expect(document.body.textContent).not.toContain('已为15个尾程订单生成14个入库计划')
})
it.each(['A0404', 'A0409'])('keeps %s request evidence visible', async (code) => {
vi.mocked(convertLastMileContainerToInboundPlans).mockRejectedValueOnce(new ApiResponseError(
'柜级生成失败', code, 200, undefined, `request-${code}`, `trace-${code}`,
))
const wrapper = await openPending()
const button = wrapper.findAll('button').find((item) => item.text().includes('生成整柜入库计划'))
await button!.trigger('click')
await flushPromises()
expect(wrapper.text()).toContain(code)
expect(wrapper.text()).toContain(`request-${code}`)
expect(wrapper.text()).toContain(`trace-${code}`)
})
})