|
| 1 | +/* eslint no-console:0 */ |
| 2 | +import React from 'react'; |
| 3 | +import { format } from 'util'; |
| 4 | +import { mount, ReactWrapper } from 'enzyme'; |
| 5 | +import sinon from 'sinon'; |
| 6 | +import Upload from '../src'; |
| 7 | +import { UploadProps, RcFile } from '../src/interface'; |
| 8 | + |
| 9 | +describe('Upload.Batch', () => { |
| 10 | + function getFile(name: string): RcFile { |
| 11 | + return { |
| 12 | + name, |
| 13 | + toString: () => name, |
| 14 | + } as RcFile; |
| 15 | + } |
| 16 | + |
| 17 | + function genProps(props?: { |
| 18 | + onStart: UploadProps['onStart']; |
| 19 | + onProgress: UploadProps['onProgress']; |
| 20 | + onSuccess: UploadProps['onSuccess']; |
| 21 | + onError: UploadProps['onError']; |
| 22 | + }) { |
| 23 | + return { |
| 24 | + action: '/test', |
| 25 | + data: { a: 1, b: 2 }, |
| 26 | + multiple: true, |
| 27 | + accept: '.png', |
| 28 | + onStart(file) { |
| 29 | + props?.onStart?.(file); |
| 30 | + }, |
| 31 | + onSuccess(res, file, xhr) { |
| 32 | + props?.onSuccess?.(res, file, xhr); |
| 33 | + }, |
| 34 | + onProgress(step, file) { |
| 35 | + props?.onProgress?.(step, file); |
| 36 | + }, |
| 37 | + onError(err, ret, file) { |
| 38 | + props?.onError?.(err, ret, file); |
| 39 | + }, |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + describe('onBatchUpload', () => { |
| 44 | + const firstFile = getFile('first.png'); |
| 45 | + const secondFile = getFile('second.png'); |
| 46 | + |
| 47 | + function triggerUpload(wrapper: ReactWrapper) { |
| 48 | + const files: RcFile[] = [firstFile, secondFile]; |
| 49 | + wrapper.find('input').first().simulate('change', { target: { files } }); |
| 50 | + } |
| 51 | + |
| 52 | + it('should trigger', done => { |
| 53 | + const onBatchUpload = jest.fn(); |
| 54 | + |
| 55 | + const wrapper = mount(<Upload onBatchUpload={onBatchUpload} {...genProps()} />); |
| 56 | + triggerUpload(wrapper); |
| 57 | + |
| 58 | + setTimeout(() => { |
| 59 | + expect(onBatchUpload).toHaveBeenCalledWith([ |
| 60 | + expect.objectContaining(firstFile), |
| 61 | + expect.objectContaining(secondFile), |
| 62 | + ]); |
| 63 | + done(); |
| 64 | + }, 10); |
| 65 | + }); |
| 66 | + |
| 67 | + it('beforeUpload return false', done => { |
| 68 | + const onBatchUpload = jest.fn(); |
| 69 | + |
| 70 | + const wrapper = mount( |
| 71 | + <Upload onBatchUpload={onBatchUpload} beforeUpload={() => false} {...genProps()} />, |
| 72 | + ); |
| 73 | + triggerUpload(wrapper); |
| 74 | + |
| 75 | + setTimeout(() => { |
| 76 | + expect(onBatchUpload).toHaveBeenCalledWith([ |
| 77 | + expect.objectContaining(firstFile), |
| 78 | + expect.objectContaining(secondFile), |
| 79 | + ]); |
| 80 | + done(); |
| 81 | + }, 10); |
| 82 | + }); |
| 83 | + |
| 84 | + it('beforeUpload return promise file', done => { |
| 85 | + const onBatchUpload = jest.fn(); |
| 86 | + |
| 87 | + const wrapper = mount( |
| 88 | + <Upload |
| 89 | + onBatchUpload={onBatchUpload} |
| 90 | + beforeUpload={file => Promise.resolve(file)} |
| 91 | + {...genProps()} |
| 92 | + />, |
| 93 | + ); |
| 94 | + triggerUpload(wrapper); |
| 95 | + |
| 96 | + setTimeout(() => { |
| 97 | + expect(onBatchUpload).toHaveBeenCalledWith([ |
| 98 | + expect.objectContaining(firstFile), |
| 99 | + expect.objectContaining(secondFile), |
| 100 | + ]); |
| 101 | + done(); |
| 102 | + }, 10); |
| 103 | + }); |
| 104 | + |
| 105 | + it('beforeUpload return promise rejection', done => { |
| 106 | + const onBatchUpload = jest.fn(); |
| 107 | + |
| 108 | + const wrapper = mount( |
| 109 | + <Upload |
| 110 | + onBatchUpload={onBatchUpload} |
| 111 | + beforeUpload={() => Promise.reject()} |
| 112 | + {...genProps()} |
| 113 | + />, |
| 114 | + ); |
| 115 | + triggerUpload(wrapper); |
| 116 | + |
| 117 | + setTimeout(() => { |
| 118 | + expect(onBatchUpload).toHaveBeenCalledWith([]); |
| 119 | + done(); |
| 120 | + }, 10); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments