Files
JayuCloud/JC-Salon-WeChat/utils/upload.test.js
T

60 lines
1.7 KiB
JavaScript
Raw Normal View History

const assert = require('assert')
global.API_DOMAIN = 'http://127.0.0.1/api'
global.REQUEST_TIMEOUT = 1000
global.ENABLE_REQUEST_SIGNING = false
let uploadOptions
2026-10-31 21:02:37 +08:00
const navigations = []
let clearedSessions = 0
global.wx = {
2026-10-31 21:02:37 +08:00
getAccountInfoSync: () => ({ miniProgram: { envVersion: 'develop' } }),
getDeviceInfo: () => ({ platform: 'devtools' }),
getStorageSync: () => ({}),
2026-10-31 21:02:37 +08:00
setStorageSync: () => {},
removeStorageSync: () => {},
showToast: () => {},
reLaunch: options => navigations.push(options.url),
uploadFile(options) {
uploadOptions = options
2026-10-31 21:02:37 +08:00
if (options.filePath === '/tmp/expired.png') {
options.success({ statusCode: 401, data: '{"message":"登录已过期"}' })
return
}
options.success({
statusCode: 200,
data: '{"code":200,"data":{"fileId":9223372036854775001,"id":9223372036854775002}}'
})
}
}
2026-10-31 21:02:37 +08:00
global.getApp = () => ({ clearSession() { clearedSessions += 1 } })
global.setTimeout = callback => { callback(); return 1 }
const { uploadFile } = require('./upload')
async function run() {
const result = await uploadFile({
url: '/user/workbench/register/avatar',
filePath: '/tmp/avatar.png',
name: 'file',
formData: { registrationTicket: 'ticket' }
})
assert.strictEqual(uploadOptions.url, 'http://127.0.0.1:80/api/user/workbench/register/avatar')
assert.strictEqual(result.fileId, '9223372036854775001')
assert.strictEqual(result.id, '9223372036854775002')
2026-10-31 21:02:37 +08:00
await assert.rejects(uploadFile({
url: '/object-storage-file/upload',
filePath: '/tmp/expired.png',
name: 'file'
}), /登录已过期/)
assert.strictEqual(clearedSessions, 1)
assert.deepStrictEqual(navigations, ['/pages/login/index'])
}
run().catch(error => {
console.error(error)
process.exitCode = 1
})