优化:小程序记住登录与启动跳转
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
const assert = require('assert')
|
||||
|
||||
const LOGIN_PREFERENCE_KEY = 'jayucloud_salon_login_preference'
|
||||
const storage = {}
|
||||
const navigations = []
|
||||
let codeSequence = 0
|
||||
let appDefinition
|
||||
|
||||
global.wx = {
|
||||
getAccountInfoSync: () => ({ miniProgram: { envVersion: 'develop' } }),
|
||||
getDeviceInfo: () => ({ platform: 'devtools' }),
|
||||
getStorageSync: key => storage[key],
|
||||
setStorageSync: (key, value) => { storage[key] = value },
|
||||
removeStorageSync: key => { delete storage[key] },
|
||||
login: ({ success }) => success({ code: `code-${++codeSequence}` }),
|
||||
reLaunch: ({ url }) => { navigations.push(url) }
|
||||
}
|
||||
global.getCurrentPages = () => [{ route: 'pages/launch/index' }]
|
||||
global.App = definition => { appDefinition = definition }
|
||||
|
||||
require('./app')
|
||||
const authApi = require('./api/auth')
|
||||
|
||||
function reset(preference) {
|
||||
Object.keys(storage).forEach(key => delete storage[key])
|
||||
if (preference) storage[LOGIN_PREFERENCE_KEY] = preference
|
||||
navigations.length = 0
|
||||
appDefinition.globalData.userInfo = null
|
||||
appDefinition.globalData.wechatLoginState = null
|
||||
}
|
||||
|
||||
async function run() {
|
||||
reset({ rememberMe: true, agreementVersions: { USER_TERMS: '1', PRIVACY_POLICY: '1' } })
|
||||
authApi.wechatStatus = async () => ({ status: 'READY' })
|
||||
authApi.wechatQuickLogin = async () => ({ status: 'SUCCESS', accessToken: 'token', userInfo: { id: 1 } })
|
||||
await appDefinition.runWechatBootstrap()
|
||||
assert.deepStrictEqual(navigations, ['/pages/planning/index'])
|
||||
assert.strictEqual(appDefinition.globalData.userInfo.id, 1)
|
||||
|
||||
reset({ rememberMe: false })
|
||||
let quickLoginCalled = false
|
||||
authApi.wechatStatus = async () => ({ status: 'READY' })
|
||||
authApi.wechatQuickLogin = async () => { quickLoginCalled = true }
|
||||
await appDefinition.runWechatBootstrap()
|
||||
assert.deepStrictEqual(navigations, ['/pages/login/index'])
|
||||
assert.strictEqual(quickLoginCalled, false)
|
||||
|
||||
reset({ rememberMe: true, agreementVersions: { USER_TERMS: '1', PRIVACY_POLICY: '1' } })
|
||||
authApi.wechatStatus = async () => ({ status: 'READY' })
|
||||
authApi.wechatQuickLogin = async () => { throw new Error('网络异常') }
|
||||
await appDefinition.runWechatBootstrap()
|
||||
assert.deepStrictEqual(navigations, ['/pages/login/index'])
|
||||
assert.strictEqual(storage[LOGIN_PREFERENCE_KEY].rememberMe, true)
|
||||
assert.strictEqual(appDefinition.globalData.wechatLoginState.status, 'ERROR')
|
||||
|
||||
reset({ rememberMe: true, agreementVersions: { USER_TERMS: '1', PRIVACY_POLICY: '1' } })
|
||||
authApi.wechatStatus = async () => ({ status: 'READY' })
|
||||
authApi.wechatQuickLogin = async () => ({ status: 'SELECT_SHOP', ticket: 'ticket', shops: [{ id: 2 }] })
|
||||
await appDefinition.runWechatBootstrap()
|
||||
assert.deepStrictEqual(navigations, ['/pages/login/index'])
|
||||
assert.strictEqual(appDefinition.globalData.wechatLoginState.status, 'SELECT_SHOP')
|
||||
|
||||
appDefinition.logoutDevice()
|
||||
assert.strictEqual(storage[LOGIN_PREFERENCE_KEY], undefined)
|
||||
assert.strictEqual(appDefinition.globalData.userInfo, null)
|
||||
}
|
||||
|
||||
run().catch(error => {
|
||||
console.error(error)
|
||||
process.exitCode = 1
|
||||
})
|
||||
Reference in New Issue
Block a user