提交 ac4c2aa3 编写于 作者: A Anne_LXM

add ext-storage-qiniu.test.js

上级 1e25fab8
describe('pages/storage/ext-storage-qiniu.vue', () => {
let page
beforeAll(async () => {
// 重新reLaunch至首页,并获取首页page对象(其中 program 是uni-automator自动注入的全局对象)
page = await program.navigateTo('/pages/storage/ext-storage-qiniu')
await page.waitFor('view')
await page.setData({'isTest':true})
})
it('qiniu-storage-上传文件', async () => {
expect.assertions(2);
const res = await page.callMethod('uploadFile', {
filePath: '../../static/logo.png',
cloudPath: Date.now() + 'test-qiniu.png',
isPrivate: false
})
console.log('res: ---1', res);
expectText(res.fileID,'qiniu://')
expectText(res.fileURL,'https://')
})
it('qiniu-私有文件-上传', async () => {
expect.assertions(2);
const res = await page.callMethod('uploadFile', {
filePath: '../../static/play.png',
// cloudPath: Date.now() + 'test-qiniu.png',
isPrivate: true
})
console.log('res: ----2', res);
expectText(res.fileID,'qiniu://')
expectText(res.fileURL,'https://')
})
it('获取私有文件临时下载链接', async () => {
expect.assertions(3);
expectText(await page.data('privateFileID'),'qiniu://jest')
const res = await page.callMethod('getTempFileURL')
console.log('res: ----3', res);
expectText(res,'&token')
expectText(res,'https://')
})
})
function expectText(value,expectValue){
expect(value).toEqual(expect.stringContaining(expectValue));
}
\ No newline at end of file
...@@ -21,7 +21,8 @@ ...@@ -21,7 +21,8 @@
export default { export default {
data() { data() {
return { return {
privateFileID: "" privateFileID: "",
isTest:false
} }
}, },
mounted() {}, mounted() {},
...@@ -76,6 +77,7 @@ ...@@ -76,6 +77,7 @@
}) })
}, },
async uploadFile(options) { async uploadFile(options) {
console.log('options: ',options);
uni.showLoading({ uni.showLoading({
title: '文件上传中...' title: '文件上传中...'
}) })
...@@ -83,53 +85,60 @@ ...@@ -83,53 +85,60 @@
customUI: true customUI: true
}); });
const uploadFileOptionsRes = await uniCloudStorageExtCo.getUploadFileOptions({ const uploadFileOptionsRes = await uniCloudStorageExtCo.getUploadFileOptions({
cloudPath: `test/${Date.now()}.jpg`, // 支持自定义目录 cloudPath: `jest/${Date.now()}.jpg`, // 支持自定义目录
}); });
console.log('uploadFileOptionsRes: ', uploadFileOptionsRes) console.log('uploadFileOptionsRes: ', uploadFileOptionsRes)
const uploadTask = uni.uploadFile({
...uploadFileOptionsRes.uploadFileOptions, // 上传文件所需参数 let testRes = new Promise((resolve,reject)=>{
filePath: options.filePath, // 本地文件路径 const uploadTask = uni.uploadFile({
success: async () => { ...uploadFileOptionsRes.uploadFileOptions, // 上传文件所需参数
const res = { filePath: options.filePath, // 本地文件路径
cloudPath: uploadFileOptionsRes.cloudPath, // 文件云端路径 success: async () => {
fileID: uploadFileOptionsRes.fileID, // 文件ID const res = {
fileURL: uploadFileOptionsRes.fileURL, // 文件URL(如果是私有权限,则此URL是无法直接访问的) cloudPath: uploadFileOptionsRes.cloudPath, // 文件云端路径
}; fileID: uploadFileOptionsRes.fileID, // 文件ID
// 上传成功后的逻辑 fileURL: uploadFileOptionsRes.fileURL, // 文件URL(如果是私有权限,则此URL是无法直接访问的)
console.log(res); };
if (options.isPrivate) { // 上传成功后的逻辑
// 设为私有文件 console.log(res);
const uniCloudStorageExtCo = uniCloud.importObject("ext-storage-co", { if (options.isPrivate) {
customUI: true // 设为私有文件
}); const uniCloudStorageExtCo = uniCloud.importObject("ext-storage-co", {
await uniCloudStorageExtCo.setFilePrivate({ customUI: true
fileID: res.fileID });
}); await uniCloudStorageExtCo.setFilePrivate({
this.privateFileID = res.fileID; fileID: res.fileID
} });
uni.showModal({ this.privateFileID = res.fileID;
content: '图片上传成功,fileID为:' + res.fileID, }
showCancel: false
})
},
fail: (err) => {
// 上传失败后的逻辑
console.log(err);
if (err.message !== 'Fail_Cancel') {
uni.showModal({ uni.showModal({
content: `图片上传失败,错误信息为:${err.message}`, content: '图片上传成功,fileID为:' + res.fileID,
showCancel: false showCancel: false
}) })
resolve(res)
},
fail: (err) => {
// 上传失败后的逻辑
console.log(err);
if (err.message !== 'Fail_Cancel') {
uni.showModal({
content: `图片上传失败,错误信息为:${err.message}`,
showCancel: false
})
}
reject(err)
},
complete: () => {
uni.hideLoading()
} }
}, });
complete: () => { // 监听上传进度
uni.hideLoading() uploadTask.onProgressUpdate((res) => {
} console.log("监听上传进度", res);
}); });
// 监听上传进度 })
uploadTask.onProgressUpdate((res) => { console.log("testRes",testRes)
console.log("监听上传进度", res); return testRes
});
}, },
async getTempFileURL() { async getTempFileURL() {
const uniCloudStorageExtCo = uniCloud.importObject("ext-storage-co"); const uniCloudStorageExtCo = uniCloud.importObject("ext-storage-co");
...@@ -141,6 +150,9 @@ ...@@ -141,6 +150,9 @@
content: '图片临时下载链接为:' + tempFileURL, content: '图片临时下载链接为:' + tempFileURL,
showCancel: false showCancel: false
}) })
if(this.isTest){
return tempFileURL
}
} }
} }
} }
......
// 扩展存储自定义域名 // 扩展存储自定义域名
const domain = "qiniu01.dcloud.net.cn"; const domain = "jest-ext-storage-aliyun.dcloud.net.cn";
module.exports = { module.exports = {
_before() {}, _before() {},
getUploadFileOptions(data = {}) { getUploadFileOptions(data = {}) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册