You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
3.2 KiB
71 lines
3.2 KiB
import fs from 'node:fs'
|
|
import vm from 'node:vm'
|
|
import assert from 'node:assert/strict'
|
|
|
|
function loadPage(path, uni) {
|
|
const source = fs.readFileSync(new URL(path, import.meta.url), 'utf8')
|
|
.split('<script>')[1].split('</script>')[0]
|
|
.replace(/^\s*import .*$/gm, '').replace('export default', 'page =')
|
|
const context = { uni, clearInterval, CompanySelector: {}, page: null }
|
|
vm.runInNewContext(source, context)
|
|
return context.page
|
|
}
|
|
let modal, redirect
|
|
const uni = { showModal: options => { modal = options }, redirectTo: options => { redirect = options } }
|
|
const page = loadPage('../packages/register/index.vue', uni)
|
|
const state = {
|
|
$set: (target, key, value) => { target[key] = value }, $nextTick: () => Promise.resolve(),
|
|
isBasicInfoIncomplete: () => false, saveUser() { this.registrations = (this.registrations || 0) + 1 },
|
|
myMobile: '13800138000', myCode: '123456', form: {}, showMobile: true,
|
|
sendTimer: null, hasSend: true, count: 35, survey_id: '12', course_id: '34', isDonate: true,
|
|
base: { isNull: value => !value, toast() {} },
|
|
$u: { api: { bindMobile: async () => { throw { data: { errmsg: '手机号已存在' } } } } }
|
|
}
|
|
await page.methods.changeMobile.call(state)
|
|
assert.equal(modal.title, '用户已注册')
|
|
assert.equal(modal.confirmText, '去登陆')
|
|
assert.equal(state.form.mobile, undefined)
|
|
assert.equal(state.myCode, '')
|
|
assert.equal(state.showMobile, false)
|
|
assert.equal(state.hasSend, false)
|
|
modal.success({ confirm: true })
|
|
assert.equal(redirect.url, '/packages/register/login?mobile=13800138000&survey_id=12&id=34&isDonate=1')
|
|
modal = undefined
|
|
state.$u.api.bindMobile = async () => { throw { data: { errmsg: '验证码错误' } } }
|
|
state.myCode = '654321'
|
|
await page.methods.changeMobile.call(state)
|
|
assert.equal(modal, undefined)
|
|
assert.equal(state.registrations || 0, 0)
|
|
state.$u.api.bindMobile = async () => ({ id: 1 })
|
|
await page.methods.changeMobile.call(state)
|
|
assert.equal(state.form.mobile, '13800138000')
|
|
assert.equal(state.registrations, 1)
|
|
const login = loadPage('../packages/register/login.vue', uni)
|
|
const loginState = { form: { mobile: '' } }
|
|
login.onLoad.call(loginState, { mobile: '13800138000', survey_id: '12' })
|
|
assert.equal(loginState.form.mobile, '13800138000')
|
|
assert.equal(loginState.survey_id, '12')
|
|
loginState.form.mobile = ''
|
|
login.onLoad.call(loginState, { mobile: 'invalid' })
|
|
assert.equal(loginState.form.mobile, '')
|
|
console.log('Registration duplicate-phone and login prefill checks passed')
|
|
|
|
for (const response of [false, null, undefined, { errcode: 1, errmsg: '验证码错误' }]) {
|
|
const before = state.registrations
|
|
state.myCode = '123456'
|
|
state.$u.api.bindMobile = async () => response
|
|
await page.methods.changeMobile.call(state)
|
|
assert.equal(state.registrations, before)
|
|
assert.equal(state.bindingMobile, false)
|
|
}
|
|
let completeBinding, requests = 0
|
|
state.myCode = '123456'
|
|
state.$u.api.bindMobile = () => { requests++; return new Promise(resolve => { completeBinding = resolve }) }
|
|
const pending = page.methods.changeMobile.call(state)
|
|
page.methods.changeMobile.call(state)
|
|
assert.equal(requests, 1)
|
|
completeBinding({ id: 1 })
|
|
await pending
|
|
assert.equal(state.registrations, 2)
|
|
console.log('Verified phone binding auto-registers once; errors and duplicate clicks never register')
|