import { flushPromises, mount } from '@vue/test-utils'
import ElementPlus from 'element-plus'
import AppDialog from '@/shared/components/AppDialog.vue'
import { nextTick } from 'vue'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import PalletLabelPrintDialog from './PalletLabelPrintDialog.vue'
const preparePalletLabels = vi.fn()
const printPalletLabels = vi.fn()
vi.mock('../api/platformLoadingLists', () => ({
preparePalletLabels: (...args: unknown[]) => preparePalletLabels(...args),
printPalletLabels: (...args: unknown[]) => printPalletLabels(...args),
}))
describe('PalletLabelPrintDialog', () => {
beforeEach(() => {
vi.stubGlobal('ResizeObserver', class { observe() {} unobserve() {} disconnect() {} })
preparePalletLabels.mockReset().mockResolvedValue({
planId: '10', planNo: 'LP-1', containerNo: 'HLCU1723867', totalCount: 2,
groups: [{
destinationWarehouseCode: 'ACY2', destinationWarehouseInternalCode: '101',
destinationWarehouseName: 'ACY2', totalCount: 2,
labels: [
{ palletCode: 'HLCU1723867-101-001' },
{ palletCode: 'HLCU1723867-101-002' },
],
}],
})
})
it.each([false, true])('保留按仓补打的PDF范围和文件名,dense=%s', async (dense) => {
URL.createObjectURL = vi.fn(() => 'blob:pallet-labels')
URL.revokeObjectURL = vi.fn()
const downloads: string[] = []
const click = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function (this: HTMLAnchorElement) { downloads.push(this.download) })
printPalletLabels.mockReset().mockResolvedValue(new Blob(['pdf'], { type: 'application/pdf' }))
const wrapper = mount(PalletLabelPrintDialog, { props: { planId: '10', warehouseId: '20', dense }, global: { plugins: [ElementPlus] } })
await wrapper.get('button').trigger('click')
await flushPromises()
const dialog = wrapper.getComponent(AppDialog)
expect(dialog.props('title')).toBe(dense ? '散货托盘码 · HLCU1723867' : '散货托盘码')
wrapper.getComponent({ name: 'ElRadioGroup' }).vm.$emit('update:modelValue', 'WAREHOUSE')
await flushPromises()
wrapper.getComponent({ name: 'ElInputNumber' }).vm.$emit('update:modelValue', 1)
if (dense) {
await dialog.get('[aria-label="展开全屏"]').trigger('click')
expect(dialog.find('.el-dialog.is-fullscreen').exists()).toBe(true)
expect(wrapper.getComponent({ name: 'ElInputNumber' }).props('modelValue')).toBe(1)
}
expect(dialog.props('confirmDisabled')).toBe(false)
dialog.vm.$emit('confirm')
await flushPromises()
expect(printPalletLabels).toHaveBeenCalledWith('10', '20', 'ACY2', 1)
expect(downloads).toEqual(['HLCU1723867-托盘码.pdf'])
expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:pallet-labels')
wrapper.unmount()
click.mockRestore()
})
it('shows the server-generated internal code rule and both warehouse codes', async () => {
const wrapper = mount(PalletLabelPrintDialog, {
props: { planId: '10', warehouseId: '20' },
global: {
stubs: {
AppDialog: { template: '
{{ title }}
' }, 'el-radio-group': { template: '