97 lines
3.3 KiB
TypeScript
97 lines
3.3 KiB
TypeScript
import { flushPromises, mount } from '@vue/test-utils'
|
|
import { defineComponent, h } from 'vue'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import WarehouseTransferReviewDialog from './WarehouseTransferReviewDialog.vue'
|
|
import { getWarehouseTransfer, reviewWarehouseTransfer } from '../api/warehouseTransfers'
|
|
|
|
vi.mock('../api/warehouseTransfers', async importOriginal => ({
|
|
...await importOriginal<typeof import('../api/warehouseTransfers')>(),
|
|
getWarehouseTransfer: vi.fn(),
|
|
reviewWarehouseTransfer: vi.fn(),
|
|
}))
|
|
|
|
const AppDialogStub = defineComponent({
|
|
name: 'AppDialog',
|
|
props: { modelValue: Boolean },
|
|
emits: ['confirm', 'update:modelValue'],
|
|
setup(props, { slots }) {
|
|
return () => props.modelValue ? h('section', { class: 'dialog' }, slots.default?.()) : null
|
|
},
|
|
})
|
|
|
|
const DataTableStub = defineComponent({
|
|
name: 'DataTable',
|
|
props: { data: { type: Array, default: () => [] } },
|
|
setup(props) {
|
|
return () => h('div', { class: 'data-table' }, JSON.stringify(props.data))
|
|
},
|
|
})
|
|
|
|
function detail(transferNo: string) {
|
|
return {
|
|
id: `id-${transferNo}`,
|
|
transferNo,
|
|
packageCount: 2,
|
|
needRelabel: false,
|
|
status: 'PENDING_REVIEW',
|
|
items: [{ id: 'item-1', skuCode: 'SKU-1', productNameZh: '测试货物', packageCount: 2 }],
|
|
cartons: [],
|
|
pallets: [],
|
|
relabelBindings: [],
|
|
}
|
|
}
|
|
|
|
function mountDialog(props: { modelValue: boolean; customerId: string; customerName: string; transferNo: string }) {
|
|
return mount(WarehouseTransferReviewDialog, {
|
|
props,
|
|
global: {
|
|
stubs: {
|
|
AppDialog: AppDialogStub,
|
|
DataTable: DataTableStub,
|
|
LoadErrorState: true,
|
|
ApiErrorMeta: true,
|
|
ElForm: { template: '<form><slot /></form>' },
|
|
ElFormItem: { template: '<label><slot /></label>' },
|
|
ElRadioGroup: { template: '<div><slot /></div>' },
|
|
ElRadioButton: { template: '<button type="button"><slot /></button>' },
|
|
ElInput: { template: '<textarea />' },
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
describe('WarehouseTransferReviewDialog', () => {
|
|
beforeEach(() => {
|
|
vi.mocked(getWarehouseTransfer).mockReset()
|
|
vi.mocked(reviewWarehouseTransfer).mockReset()
|
|
})
|
|
|
|
it('loads and renders review details when first mounted already open', async () => {
|
|
vi.mocked(getWarehouseTransfer).mockResolvedValue(detail('WT-1'))
|
|
|
|
const wrapper = mountDialog({ modelValue: true, customerId: '20001', customerName: '测试客户', transferNo: 'WT-1' })
|
|
await flushPromises()
|
|
|
|
expect(getWarehouseTransfer).toHaveBeenCalledTimes(1)
|
|
expect(getWarehouseTransfer).toHaveBeenCalledWith('20001', 'WT-1')
|
|
expect(wrapper.text()).toContain('WT-1')
|
|
expect(wrapper.text()).toContain('测试货物')
|
|
wrapper.unmount()
|
|
})
|
|
|
|
it('reloads details when an open dialog switches to another transfer', async () => {
|
|
vi.mocked(getWarehouseTransfer)
|
|
.mockResolvedValueOnce(detail('WT-1'))
|
|
.mockResolvedValueOnce(detail('WT-2'))
|
|
|
|
const wrapper = mountDialog({ modelValue: true, customerId: '20001', customerName: '测试客户', transferNo: 'WT-1' })
|
|
await flushPromises()
|
|
await wrapper.setProps({ customerId: '20002', transferNo: 'WT-2' })
|
|
await flushPromises()
|
|
|
|
expect(getWarehouseTransfer).toHaveBeenLastCalledWith('20002', 'WT-2')
|
|
expect(wrapper.text()).toContain('WT-2')
|
|
wrapper.unmount()
|
|
})
|
|
})
|