|
1 | 1 | import { mount } from '@vue/test-utils'
|
2 | 2 | import Upload from '..'
|
3 |
| -import { fileToObject } from '../utils' |
| 3 | +import { T, fileToObject, genPercentAdd, getFileItem, removeFileItem } from '../utils' |
4 | 4 | import PropsTypes from '../../_util/vue-types'
|
5 | 5 | import { UploadListProps } from '../interface'
|
6 | 6 |
|
@@ -58,6 +58,47 @@ describe('Upload', () => {
|
58 | 58 | }, 0)
|
59 | 59 | })
|
60 | 60 |
|
| 61 | + it('upload promise return file in beforeUpload', done => { |
| 62 | + const data = jest.fn() |
| 63 | + const props = { |
| 64 | + propsData: { |
| 65 | + action: 'http://upload.com', |
| 66 | + beforeUpload: file => |
| 67 | + new Promise(resolve => |
| 68 | + setTimeout(() => { |
| 69 | + const result = file |
| 70 | + result.name = 'test.png' |
| 71 | + resolve(result) |
| 72 | + }, 100), |
| 73 | + ), |
| 74 | + data, |
| 75 | + }, |
| 76 | + listeners: { |
| 77 | + change: ({ file }) => { |
| 78 | + if (file.status !== 'uploading') { |
| 79 | + expect(data).toBeCalled() |
| 80 | + expect(file.name).toEqual('test.png') |
| 81 | + done() |
| 82 | + } |
| 83 | + }, |
| 84 | + }, |
| 85 | + slots: { |
| 86 | + default: '<button>upload</button>', |
| 87 | + }, |
| 88 | + sync: false, |
| 89 | + } |
| 90 | + |
| 91 | + const wrapper = mount(Upload, props) |
| 92 | + |
| 93 | + setTimeout(() => { |
| 94 | + wrapper.find({ name: 'ajaxUploader' }).vm.onChange({ |
| 95 | + target: { |
| 96 | + files: [{ file: 'foo.png' }], |
| 97 | + }, |
| 98 | + }) |
| 99 | + }, 0) |
| 100 | + }) |
| 101 | + |
61 | 102 | it('should not stop upload when return value of beforeUpload is false', (done) => {
|
62 | 103 | const data = jest.fn()
|
63 | 104 | const props = {
|
@@ -165,13 +206,79 @@ describe('Upload', () => {
|
165 | 206 | })
|
166 | 207 |
|
167 | 208 | describe('util', () => {
|
| 209 | + // https://github.com/react-component/upload/issues/36 |
| 210 | + it('should T() return true', () => { |
| 211 | + const res = T() |
| 212 | + expect(res).toBe(true) |
| 213 | + }) |
168 | 214 | it('should be able to copy file instance', () => {
|
169 | 215 | const file = new File([], 'aaa.zip')
|
170 | 216 | const copiedFile = fileToObject(file);
|
171 | 217 | ['uid', 'lastModified', 'lastModifiedDate', 'name', 'size', 'type'].forEach((key) => {
|
172 | 218 | expect(key in copiedFile).toBe(true)
|
173 | 219 | })
|
174 | 220 | })
|
| 221 | + it('should be able to progress from 0.1 ', () => { |
| 222 | + // 0.1 -> 0.98 |
| 223 | + const getPercent = genPercentAdd() |
| 224 | + let curPercent = 0 |
| 225 | + curPercent = getPercent(curPercent) |
| 226 | + expect(curPercent).toBe(0.1) |
| 227 | + }) |
| 228 | + |
| 229 | + it('should be able to progress to 0.98 ', () => { |
| 230 | + // 0.1 -> 0.98 |
| 231 | + const getPercent = genPercentAdd() |
| 232 | + let curPercent = 0 |
| 233 | + for (let i = 0; i < 500; i += 1) { |
| 234 | + curPercent = getPercent(curPercent) |
| 235 | + } |
| 236 | + expect(parseFloat(curPercent.toFixed(2))).toBe(0.98) |
| 237 | + }) |
| 238 | + |
| 239 | + it('should be able to get fileItem', () => { |
| 240 | + const file = { uid: '-1', name: 'item.jpg' } |
| 241 | + const fileList = [ |
| 242 | + { |
| 243 | + uid: '-1', |
| 244 | + name: 'item.jpg', |
| 245 | + }, |
| 246 | + ] |
| 247 | + const targetItem = getFileItem(file, fileList) |
| 248 | + expect(targetItem).toBe(fileList[0]) |
| 249 | + }) |
| 250 | + |
| 251 | + it('should be able to remove fileItem', () => { |
| 252 | + const file = { uid: '-1', name: 'item.jpg' } |
| 253 | + const fileList = [ |
| 254 | + { |
| 255 | + uid: '-1', |
| 256 | + name: 'item.jpg', |
| 257 | + }, |
| 258 | + { |
| 259 | + uid: '-2', |
| 260 | + name: 'item2.jpg', |
| 261 | + }, |
| 262 | + ] |
| 263 | + const targetItem = removeFileItem(file, fileList) |
| 264 | + expect(targetItem).toEqual(fileList.slice(1)) |
| 265 | + }) |
| 266 | + |
| 267 | + it('should not be able to remove fileItem', () => { |
| 268 | + const file = { uid: '-3', name: 'item.jpg' } |
| 269 | + const fileList = [ |
| 270 | + { |
| 271 | + uid: '-1', |
| 272 | + name: 'item.jpg', |
| 273 | + }, |
| 274 | + { |
| 275 | + uid: '-2', |
| 276 | + name: 'item2.jpg', |
| 277 | + }, |
| 278 | + ] |
| 279 | + const targetItem = removeFileItem(file, fileList) |
| 280 | + expect(targetItem).toBe(null) |
| 281 | + }) |
175 | 282 | })
|
176 | 283 |
|
177 | 284 | it('should support linkProps as object', () => {
|
|
0 commit comments