80 lines
3.3 KiB
JavaScript
80 lines
3.3 KiB
JavaScript
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
|
|
appDefinition.globalData.reviewNoticeShown = true
|
|
appDefinition.globalData.openEmployeeReview = true
|
|
codeSequence = 0
|
|
}
|
|
|
|
async function run() {
|
|
reset({ rememberMe: true, agreementVersions: { USER_TERMS: '1', PRIVACY_POLICY: '1' } })
|
|
let statusCalled = false
|
|
authApi.wechatStatus = async () => { statusCalled = true; return { 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)
|
|
assert.strictEqual(statusCalled, false)
|
|
assert.strictEqual(codeSequence, 1)
|
|
assert.strictEqual(appDefinition.globalData.reviewNoticeShown, false)
|
|
assert.strictEqual(appDefinition.globalData.openEmployeeReview, false)
|
|
|
|
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)
|
|
assert.strictEqual(codeSequence, 1)
|
|
|
|
reset({ rememberMe: true, agreementVersions: { USER_TERMS: '1', PRIVACY_POLICY: '1' } })
|
|
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.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)
|
|
assert.strictEqual(appDefinition.globalData.reviewNoticeShown, false)
|
|
}
|
|
|
|
run().catch(error => {
|
|
console.error(error)
|
|
process.exitCode = 1
|
|
})
|