feat(Uploader): use before read promise resolved value (#5813)

This commit is contained in:
Pingren Nie
2020-03-12 20:52:25 +08:00
committed by GitHub
parent df877751b3
commit e96c352680
5 changed files with 49 additions and 165 deletions
+4 -1
View File
@@ -140,7 +140,10 @@ export default {
Toast('Please upload an image in jpg format');
reject();
} else {
resolve();
let img = new File(["foo"], "bar.jpg", {
type: "image/jpeg",
});
resolve(img);
}
});
}
+6 -3
View File
@@ -133,9 +133,9 @@ export default {
</van-uploader>
```
### 上传前校验
### 上传前自定义处理
通过传入`beforeRead`函数可以在上传前进行校验,返回`true`表示校验通过,返回`false`表示校验失败。支持返回`Promise`进行异步校验
通过传入`beforeRead`函数可以在上传前进行校验,返回`true`表示校验通过,返回`false`表示校验失败。支持返回`Promise`对 file 对象进行自定义处理,例如压缩图片。
```html
<van-uploader :before-read="beforeRead" />
@@ -161,7 +161,10 @@ export default {
Toast('请上传 jpg 格式图片');
reject();
} else {
resolve();
let img = new File(["foo"], "bar.jpg", {
type: "image/jpeg",
});
resolve(img);
}
});
}
+6 -2
View File
@@ -113,8 +113,12 @@ export default createComponent({
if (isPromise(response)) {
response
.then(() => {
this.readFile(files);
.then(data => {
if (data) {
this.readFile(data);
} else {
this.readFile(files);
}
})
.catch(this.resetInput);
+21 -2
View File
@@ -118,7 +118,7 @@ it('before read return promise and resolve', async () => {
propsData: {
beforeRead: () =>
new Promise(resolve => {
resolve();
resolve(file);
}),
afterRead,
},
@@ -130,6 +130,25 @@ it('before read return promise and resolve', async () => {
expect(afterRead).toHaveBeenCalledTimes(1);
});
it('before read return promise and resolve no value', async () => {
const afterRead = jest.fn();
const wrapper = mount(Uploader, {
propsData: {
beforeRead: () =>
new Promise(resolve => {
resolve();
}),
afterRead,
},
});
const input = wrapper.find('input');
wrapper.vm.onChange(file);
await later();
expect(afterRead).toHaveBeenCalledTimes(1);
expect(input.element.value).toEqual('');
});
it('before read return promise and reject', async () => {
const afterRead = jest.fn();
const wrapper = mount(Uploader, {
@@ -143,9 +162,9 @@ it('before read return promise and reject', async () => {
});
const input = wrapper.find('input');
wrapper.vm.onChange(file);
await later();
wrapper.vm.onChange(file);
expect(afterRead).toHaveBeenCalledTimes(0);
expect(input.element.value).toEqual('');
});