37 lines
3.8 KiB
JavaScript
37 lines
3.8 KiB
JavaScript
const fs=require('fs'),path=require('path'),vm=require('vm'),assert=require('assert')
|
|
const root=path.resolve(__dirname,'../..')
|
|
function script(name){return fs.readFileSync(path.join(root,'src',name),'utf8').match(/<script>([\s\S]*?)<\/script>/)[1].replace(/^import .*$/gm,'')}
|
|
const invitation=fs.readFileSync(path.join(root,'src/utils/invitation.js'),'utf8').replace(/export /g,'')
|
|
const s={};vm.createContext(s);vm.runInContext(invitation,s)
|
|
assert.equal(s.invitationFromOptions({query:{scene:'from_user_id%3D42'}}),'42')
|
|
assert.equal(s.invitationFromOptions({query:{from_user_id:43}}),'43')
|
|
for(const query of [{scene:'%'},{scene:'id=42'},{scene:'from_user_id=-1'},{from_user_id:'invalid'},{}])assert.equal(s.invitationFromOptions({query}),'')
|
|
const storage=new Map(),nav=[],previews=[]
|
|
let appDef,pageDef
|
|
const wx={setStorageSync(k,v){storage.set(k,v)},getStorageSync(k){return storage.get(k)},removeStorageSync(k){storage.delete(k)},switchTab(x){nav.push(x)},getSystemInfo({success}){success({screenHeight:800,safeArea:{bottom:780}})},getSystemInfoSync(){return {screenWidth:375,windowHeight:700,statusBarHeight:20}},getMenuButtonBoundingClientRect(){return {left:280,top:24,height:32,bottom:56}},navigateTo(x){nav.push(x)},previewImage(x){previews.push(x)},showToast(){}}
|
|
const context={...s,wx,console:{log(){}},wepy:{use(){},app(x){appDef=x},page(x){pageDef=x}},vuex:{},promisify:{},initEid(){},https:{},service:{host:'synthetic-host'},base:{},getApp(){return {$wepy:{$options:{globalData:{systemInfo:{system:'synthetic OS'}}}}}}}
|
|
vm.createContext(context);vm.runInContext(script('app.wpy'),context)
|
|
const app={$options:{globalData:{}},appLogin(){assert.equal(storage.get('from_user_id'),'42')}}
|
|
appDef.onLaunch.call(app,{query:{scene:'from_user_id%3D42'}})
|
|
appDef.onShow.call(app,{path:'pages/tabBar/welcome',query:{scene:'from_user_id%3D42'}});assert.equal(storage.get('from_user_id'),'42')
|
|
appDef.onShow.call(app,{path:'pages/sub_mall/confirmOrder',query:{}});assert.equal(storage.get('from_user_id'),'42')
|
|
appDef.onShow.call(app,{path:'pages/tabBar/home',query:{}});assert.equal(storage.has('from_user_id'),false)
|
|
appDef.onShow.call(app,{path:'pages/tabBar/welcome',query:{scene:'%'}})
|
|
appDef.onShow.call(app,{path:'pages/tabBar/welcome',query:{scene:'userId=88'}});assert.equal(storage.get('agent_user_id'),'88')
|
|
vm.runInContext(script('pages/tabBar/user.wpy'),context)
|
|
assert.deepEqual(Array.from(pageDef.data.list,x=>x.title),['会员中心','我的发布','我的邀请','我的卡片','更多','设置'])
|
|
assert.equal(pageDef.data.list[0].path,'/pages/users/upgradeVIP');assert.equal(pageDef.data.list[1].path,'/pages/users/myDynamic');assert.equal(pageDef.data.list[5].path,'/pages/users/setTing')
|
|
vm.runInContext(script('pages/users/more.wpy'),context)
|
|
const more={...pageDef.data,...pageDef.methods,$get:async()=>({data:{qrcode:'synthetic-assigned-code'}})}
|
|
;(async()=>{
|
|
await more.openService();assert.equal(previews[0].current,'synthetic-assigned-code')
|
|
more.$get=async()=>({data:{}});await more.openService();assert.equal(nav.pop().url,'/pages/users/exclusiveService')
|
|
more.$get=async()=>{throw Error('synthetic')};await more.openService();assert.equal(more.serviceLoading,false)
|
|
more.guide();assert(nav.pop().url.includes(encodeURIComponent('https://love.ufutx.com/h5/#/guide')))
|
|
const configText=fs.readFileSync(path.join(root,'src/app.wpy'),'utf8').match(/<config>([\s\S]*?)<\/config>/)[1]
|
|
const config=vm.runInNewContext('('+configText+')')
|
|
const users=config.subPackages.find(x=>x.root==='pages/users')
|
|
for(const name of ['more','myShare','shareCard','myDynamic','exclusiveService','setTing'])assert.equal(users.pages.filter(x=>x===name).length,1)
|
|
console.log('PASS: cold-start attribution, malformed QR, old agent/shopping paths, menu order, preserved member/publish/settings routes, assigned customer service, route registration.')
|
|
})().catch(e=>{console.error(e.stack);process.exitCode=1})
|