Skip to content

Commit 7a279f6

Browse files
committed
docs: update upload doc
1 parent cfe0021 commit 7a279f6

File tree

4 files changed

+112
-5
lines changed

4 files changed

+112
-5
lines changed

components/upload/__tests__/upload.test.js

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { mount } from '@vue/test-utils'
22
import Upload from '..'
3-
import { fileToObject } from '../utils'
3+
import { T, fileToObject, genPercentAdd, getFileItem, removeFileItem } from '../utils'
44
import PropsTypes from '../../_util/vue-types'
55
import { UploadListProps } from '../interface'
66

@@ -58,6 +58,47 @@ describe('Upload', () => {
5858
}, 0)
5959
})
6060

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+
61102
it('should not stop upload when return value of beforeUpload is false', (done) => {
62103
const data = jest.fn()
63104
const props = {
@@ -165,13 +206,79 @@ describe('Upload', () => {
165206
})
166207

167208
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+
})
168214
it('should be able to copy file instance', () => {
169215
const file = new File([], 'aaa.zip')
170216
const copiedFile = fileToObject(file);
171217
['uid', 'lastModified', 'lastModifiedDate', 'name', 'size', 'type'].forEach((key) => {
172218
expect(key in copiedFile).toBe(true)
173219
})
174220
})
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+
})
175282
})
176283

177284
it('should support linkProps as object', () => {

components/upload/demo/fileList.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default {
5656
if (file.response) {
5757
return file.response.status === 'success';
5858
}
59-
return true;
59+
return false;
6060
});
6161
6262
this.fileList = fileList

components/upload/index.en-US.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
| Property | Description | Type | Default |
66
| -------- | ----------- | ---- | ------- |
7-
| accept | File types that can be accepted. See [input accept Attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept) | string | - |
7+
| accept | File types that can be accepted. See [input accept Attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#accept) | string | - |
88
| action | Uploading URL | string\|(file) => `Promise` | - |
99
| directory | support upload whole directory ([caniuse](https://caniuse.com/#feat=input-file-directory)) | boolean | false |
1010
| beforeUpload | Hook function which will be executed before uploading. Uploading will be stopped with `false` or a rejected Promise returned. **Warning:this function is not supported in IE9**| (file, fileList) => `boolean | Promise` | - |

components/upload/index.zh-CN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
55
| 参数 | 说明 | 类型 | 默认值 |
66
| --- | --- | --- | --- |
7-
| accept | 接受上传的文件类型, 详见 [input accept Attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept) | string ||
7+
| accept | 接受上传的文件类型, 详见 [input accept Attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#accept) | string ||
88
| action | 上传的地址 | string\|(file) => `Promise` ||
99
| directory | 支持上传文件夹([caniuse](https://caniuse.com/#feat=input-file-directory)| boolean | false |
10-
| beforeUpload | 上传文件之前的钩子,参数为上传的文件,若返回 `false` 则停止上传。支持返回一个 Promise 对象,Promise 对象 reject 时则停止上传,resolve 时开始上传。**注意:IE9 不支持该方法**| (file, fileList) => `boolean | Promise` ||
10+
| beforeUpload | 上传文件之前的钩子,参数为上传的文件,若返回 `false` 则停止上传。支持返回一个 Promise 对象,Promise 对象 reject 时则停止上传,resolve 时开始上传( resolve 传入 `File``Blob` 对象则上传 resolve 传入对象)**注意:IE9 不支持该方法**| (file, fileList) => `boolean | Promise` ||
1111
| customRequest | 通过覆盖默认的上传行为,可以自定义自己的上传实现 | Function ||
1212
| data | 上传所需参数或返回上传参数的方法 | object\|(file) => object ||
1313
| defaultFileList | 默认已经上传的文件列表 | object\[] ||

0 commit comments

Comments
 (0)