56 lines
1.8 KiB
JavaScript
56 lines
1.8 KiB
JavaScript
const assert = require('assert')
|
|
|
|
let definition
|
|
let toast
|
|
let savedPayload
|
|
|
|
global.Page = value => { definition = value }
|
|
global.wx = {
|
|
showToast: options => { toast = options },
|
|
navigateBack: () => {}
|
|
}
|
|
global.getApp = () => ({ globalData: { userInfo: { id: 7 } }, setSession: () => {} })
|
|
global.setTimeout = callback => { callback(); return 1 }
|
|
|
|
const auth = require('../../utils/auth')
|
|
auth.getSession = () => ({ userInfo: { id: 7 } })
|
|
const profileCache = require('../../utils/profile-cache')
|
|
profileCache.invalidate = () => {}
|
|
profileCache.getProfile = async () => ({ id: 7, nickname: '员工', avatar: 'profile/7/avatar.png' })
|
|
const userApi = require('../../api/user')
|
|
userApi.updateInfo = async payload => { savedPayload = payload }
|
|
require('./index')
|
|
|
|
function createPage(form) {
|
|
return {
|
|
...definition,
|
|
data: { ...definition.data, form: { ...definition.data.form, ...form } },
|
|
setData(values) {
|
|
Object.entries(values).forEach(([key, value]) => {
|
|
const parts = key.split('.')
|
|
if (parts.length === 1) this.data[key] = value
|
|
else this.data[parts[0]] = { ...this.data[parts[0]], [parts[1]]: value }
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
async function run() {
|
|
const missingFilePage = createPage({ nickname: '員工', avatarChanged: true, avatarFileId: '' })
|
|
await missingFilePage.save()
|
|
assert.strictEqual(toast.title, '头像上传未完成,请重新选择')
|
|
assert.strictEqual(toast.duration, 3000)
|
|
assert.strictEqual(savedPayload, undefined)
|
|
|
|
const validPage = createPage({ nickname: '员工', avatarChanged: true, avatarFileId: '90071992547409931' })
|
|
await validPage.save()
|
|
assert.strictEqual(savedPayload.avatarFileId, '90071992547409931')
|
|
assert.strictEqual(toast.title, '已保存')
|
|
assert.strictEqual(toast.duration, 3000)
|
|
}
|
|
|
|
run().catch(error => {
|
|
console.error(error)
|
|
process.exitCode = 1
|
|
})
|