import { compressPickedImages } from '../../utils/image-picker' function run(paths, files) { return new Promise(resolve => { compressPickedImages(paths, files, resolve) }) } describe('image-picker 上传前压缩', () => { afterEach(() => { delete global.uni }) it('小于 800KB 的图片不压缩', async () => { global.uni = { compressImage: jest.fn() } const result = await run(['a.jpg'], [{ size: 100 * 1024 }]) expect(result).toEqual(['a.jpg']) expect(global.uni.compressImage).not.toHaveBeenCalled() }) it('大图压缩成功时返回压缩后的临时路径', async () => { global.uni = { compressImage: jest.fn(opts => opts.success({ tempFilePath: 'a-compressed.jpg' })) } const result = await run(['a.jpg'], [{ size: 6 * 1024 * 1024 }]) expect(result).toEqual(['a-compressed.jpg']) expect(global.uni.compressImage).toHaveBeenCalledWith(expect.objectContaining({ src: 'a.jpg', quality: 75 })) }) it('压缩失败时回退原图', async () => { global.uni = { compressImage: jest.fn(opts => opts.fail(new Error('compress failed'))) } const result = await run(['a.jpg'], [{ size: 6 * 1024 * 1024 }]) expect(result).toEqual(['a.jpg']) }) it('未知大小或缺少压缩 API 时不影响选择结果', async () => { global.uni = {} const result = await run(['a.jpg', 'b.png'], [{}, {}]) expect(result).toEqual(['a.jpg', 'b.png']) }) })