2026-10-09 22:05:51 +08:00
|
|
|
import { createPinia } from 'pinia'
|
2026-10-19 17:56:31 +08:00
|
|
|
import ElementPlus from 'element-plus'
|
|
|
|
|
import { flushPromises, mount } from '@vue/test-utils'
|
|
|
|
|
import { beforeEach, expect, it, vi } from 'vitest'
|
2026-08-21 11:20:09 +08:00
|
|
|
import DispatchesView from './DispatchesView.vue'
|
2026-10-19 17:56:31 +08:00
|
|
|
import QueryToolbar from '@/shared/components/QueryToolbar.vue'
|
|
|
|
|
import RowActions from '@/shared/components/RowActions.vue'
|
|
|
|
|
import { getDispatch, getDispatchBolPdf, listDispatches } from '../api/dispatches'
|
|
|
|
|
import { detail, listRow, page } from '../dispatchTestFixtures'
|
|
|
|
|
import type { DispatchPage } from '../dispatchContracts'
|
|
|
|
|
import { useDispatchCommandRecovery } from '../dispatchCommandRecovery'
|
|
|
|
|
const identity = vi.hoisted(() => ({ id: '' }))
|
2026-10-08 14:11:54 +08:00
|
|
|
const push = vi.hoisted(() => vi.fn())
|
2026-10-19 17:56:31 +08:00
|
|
|
const can = vi.hoisted(() => vi.fn(() => true))
|
|
|
|
|
vi.mock('vue-router', () => ({ useRoute: () => ({ query: {} }), useRouter: () => ({ push }) }))
|
|
|
|
|
vi.mock('@/core/access/accessPreview', () => ({ useAccessPreviewStore: () => ({ can }) }))
|
|
|
|
|
vi.mock('@/core/auth/auth', () => ({ useAuthStore: () => ({ user: identity }) }))
|
|
|
|
|
vi.mock('@/modules/appointment-calendar', () => ({ appointmentReturnTarget: () => null }))
|
|
|
|
|
vi.mock('@/modules/overseas-master-data', () => ({ TransportRouteDialog: { template: '<div />' }, routeLocation: (label: string) => ({ label }) }))
|
|
|
|
|
vi.mock('../api/dispatches', () => ({ listDispatches: vi.fn(), getDispatch: vi.fn(), getDispatchBolPdf: vi.fn() }))
|
|
|
|
|
const options = { global: { plugins: [createPinia(), ElementPlus], stubs: { SmartDispatchDialog: true, DispatchWorkflowDialog: true } } }
|
|
|
|
|
beforeEach(() => { vi.clearAllMocks(); identity.id = crypto.randomUUID(); sessionStorage.clear(); can.mockReturnValue(true); vi.stubGlobal('ResizeObserver', class { observe() {} unobserve() {} disconnect() {} }); localStorage.clear(); vi.mocked(listDispatches).mockResolvedValue(page()); vi.mocked(getDispatch).mockResolvedValue(detail()) })
|
|
|
|
|
it('offers recovery of the original pending command independently of the filtered list rows', async () => {
|
|
|
|
|
const pending = useDispatchCommandRecovery(() => identity.id); pending.begin('PC-OTHER', 'DEPART', { recordVersion: 0, idempotencyKey: 'original-key' })
|
|
|
|
|
const wrapper = mount(DispatchesView, options); await flushPromises()
|
|
|
|
|
expect(wrapper.text()).toContain('PC-OTHER 的确认发车结果尚待核对')
|
|
|
|
|
await wrapper.findAll('button').find(button => button.text() === '核对原请求')!.trigger('click')
|
|
|
|
|
const dialog = wrapper.get('dispatch-workflow-dialog-stub')
|
|
|
|
|
expect(dialog.attributes('dispatchno')).toBe('PC-OTHER'); expect(dialog.attributes('modelvalue')).toBe('true'); wrapper.unmount()
|
|
|
|
|
})
|
|
|
|
|
it('shows exactly five phases and uses server stage counts instead of current page length', async () => {
|
|
|
|
|
const wrapper = mount(DispatchesView, options); await flushPromises()
|
|
|
|
|
const tabs = wrapper.get('[aria-label="排车执行阶段"]').findAll('button')
|
|
|
|
|
expect(tabs.map(tab => tab.text())).toEqual(['装车前 3', '装车中 1', '装车后 1', '派送中 1', '派送完成 1'])
|
|
|
|
|
expect(listDispatches).toHaveBeenCalledWith(expect.objectContaining({ stage: 'BEFORE_LOADING', status: 'ACTIVE', beforeLoadingSection: 'VEHICLE_INFO' }))
|
|
|
|
|
await tabs[3]!.trigger('click'); await flushPromises()
|
|
|
|
|
expect(listDispatches).toHaveBeenLastCalledWith(expect.objectContaining({ stage: 'DELIVERING' })); expect(wrapper.find('[aria-label="装车前审核分区"]').exists()).toBe(false); wrapper.unmount()
|
|
|
|
|
})
|
|
|
|
|
it('queries each review section on the server and includes only its legal review filter', async () => {
|
|
|
|
|
const wrapper = mount(DispatchesView, options); await flushPromises()
|
|
|
|
|
await wrapper.get('[aria-label="装车前审核分区"]').findAll('button')[1]!.trigger('click'); await flushPromises()
|
|
|
|
|
expect(listDispatches).toHaveBeenLastCalledWith(expect.objectContaining({ beforeLoadingSection: 'DISPATCH_REVIEW' }))
|
|
|
|
|
wrapper.getComponent(QueryToolbar).vm.$emit('search', { status: 'ACTIVE', reviewStatus: 'PENDING', dispatchNo: '', vehiclePlate: '' }); await flushPromises()
|
|
|
|
|
expect(listDispatches).toHaveBeenLastCalledWith(expect.objectContaining({ reviewStatus: 'PENDING' })); wrapper.unmount()
|
|
|
|
|
})
|
|
|
|
|
it('discards a late page response after switching phase', async () => {
|
|
|
|
|
let old!: (value: DispatchPage) => void
|
|
|
|
|
vi.mocked(listDispatches).mockImplementationOnce(() => new Promise(resolve => { old = resolve }))
|
|
|
|
|
const wrapper = mount(DispatchesView, options)
|
|
|
|
|
await wrapper.get('[aria-label="排车执行阶段"]').findAll('button')[1]!.trigger('click'); await flushPromises()
|
|
|
|
|
old(page([])); await flushPromises(); expect(wrapper.text()).toContain('PC/1'); wrapper.unmount()
|
|
|
|
|
})
|
|
|
|
|
it('gates every mutation with both its literal service capability and independent permission', async () => {
|
|
|
|
|
vi.mocked(listDispatches).mockResolvedValue(page([listRow({ actions: { DEPART: { allowed: false, blockReason: '未安排', blockCode: 'NO' }, EDIT: { allowed: true, blockCode: null, blockReason: null } } })]))
|
|
|
|
|
const wrapper = mount(DispatchesView, options); await flushPromises()
|
|
|
|
|
const actions = wrapper.findAllComponents(RowActions).find(entry => entry.props('moreLabel') === '更多操作 · PC/1')!.props('actions') as Array<{ key: string }>
|
|
|
|
|
expect(actions.map(row => row.key)).toContain('EDIT'); expect(actions.map(row => row.key)).not.toContain('DEPART')
|
|
|
|
|
can.mockReturnValue(false); wrapper.unmount()
|
|
|
|
|
const readonly = mount(DispatchesView, options); await flushPromises(); expect((readonly.findAllComponents(RowActions).find(entry => entry.props('moreLabel') === '更多操作 · PC/1')!.props('actions') as Array<{ key: string }>).map(row => row.key)).toEqual(['detail', 'bol']); readonly.unmount()
|
|
|
|
|
})
|
|
|
|
|
it('keeps server errors visible with the integrated query and retry context', async () => {
|
|
|
|
|
vi.mocked(listDispatches).mockRejectedValueOnce(new Error('service unavailable'))
|
|
|
|
|
const wrapper = mount(DispatchesView, options); await flushPromises(); expect(wrapper.text()).toContain('排车列表加载失败,请重试'); expect(wrapper.findComponent(QueryToolbar).exists()).toBe(true); wrapper.unmount()
|
|
|
|
|
})
|
|
|
|
|
it('navigates by the business number without arranging a separate trip', async () => {
|
|
|
|
|
const wrapper = mount(DispatchesView, options); await flushPromises(); wrapper.findAllComponents(RowActions).find(entry => entry.props('moreLabel') === '更多操作 · PC/1')!.vm.$emit('action', 'detail'); await flushPromises()
|
|
|
|
|
expect(push).toHaveBeenCalledWith({ name: 'overseas-dispatch-detail', params: { dispatchNo: 'PC/1' } }); expect(wrapper.text()).not.toContain('安排车次'); wrapper.unmount()
|
|
|
|
|
})
|
|
|
|
|
it('reads one BOL on demand and preserves popup failure handling', async () => {
|
|
|
|
|
vi.stubGlobal('open', vi.fn().mockReturnValue(null)); const wrapper = mount(DispatchesView, options); await flushPromises(); wrapper.findAllComponents(RowActions).find(entry => entry.props('moreLabel') === '更多操作 · PC/1')!.vm.$emit('action', 'bol'); await flushPromises(); expect(getDispatchBolPdf).not.toHaveBeenCalled(); wrapper.unmount()
|
2026-08-21 11:20:09 +08:00
|
|
|
})
|