import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { downloadWeightThumbnail } from '../api/weightOcr' import WeightImageThumbnail from './WeightImageThumbnail.vue' vi.mock('../api/weightOcr', () => ({ downloadWeightThumbnail: vi.fn(), })) enableAutoUnmount(afterEach) describe('WeightImageThumbnail', () => { const createObjectURL = vi.fn(() => 'blob:thumbnail') const revokeObjectURL = vi.fn() beforeEach(() => { vi.clearAllMocks() vi.stubGlobal('URL', { createObjectURL, revokeObjectURL }) }) it('loads an authenticated thumbnail and releases its object URL', async () => { vi.mocked(downloadWeightThumbnail).mockResolvedValue(new Blob(['image'], { type: 'image/jpeg' })) const wrapper = mount(WeightImageThumbnail, { props: { customerId: '10001', inboundId: '20001', fileId: '30001', alt: '称重图片 1' }, }) await flushPromises() expect(downloadWeightThumbnail).toHaveBeenCalledWith('10001', '20001', '30001') expect(wrapper.get('img').attributes('src')).toBe('blob:thumbnail') await wrapper.unmount() expect(revokeObjectURL).toHaveBeenCalledWith('blob:thumbnail') }) it('shows a stable failure state when the thumbnail cannot be read', async () => { vi.mocked(downloadWeightThumbnail).mockRejectedValue(new Error('network')) const wrapper = mount(WeightImageThumbnail, { props: { customerId: '10001', inboundId: '20001', fileId: '30001', alt: '称重图片 1' }, }) await flushPromises() expect(wrapper.text()).toContain('图片加载失败') expect(wrapper.find('img').exists()).toBe(false) }) })