ufutx_love_mp/tools/local-acceptance/membership-plans.test.cjs

13 lines
2.2 KiB
JavaScript

const fs=require('fs'),vm=require('vm'),assert=require('assert');
const helpers={};vm.createContext(helpers);vm.runInContext(fs.readFileSync('ufutx_love_mp/src/utils/membership-plans.js','utf8').replace(/export /g,''),helpers);
const rows=[{id:12,month:12,name:'年卡',discount_price:388},{id:15,month:0,name:'隐藏测试'},{id:10,month:1,name:'月卡',discount_price:78},{id:11,month:3,name:'季卡',discount_price:108}];
const plans=helpers.membershipPlans(rows);assert.equal(plans.length,3);assert.equal(plans[helpers.defaultPlanIndex(plans)].id,11);assert.equal(plans[0].id,10);assert.equal(plans[2].id,12);
assert.equal(helpers.planPrice(plans[1],[]),'108');assert.equal(helpers.planPrice(plans[1],[{sub_rank_id:11,price_fen:10999}]),'109.99');assert.equal(helpers.planPrice({id:99},[]),'待确认');assert.equal(helpers.defaultPlanIndex([]),0);
let options;const wx={getStorageSync:()=>''};const source=fs.readFileSync('ufutx_love_mp/src/pages/users/upgradeVIP.wpy','utf8').match(/<script>([\s\S]*?)<\/script>/)[1].replace(/^import .*$/gm,'');
vm.runInNewContext(source,{...helpers,membershipPlans:helpers.membershipPlans,planPrice:helpers.planPrice,defaultPlanIndex:helpers.defaultPlanIndex,wepy:{page:o=>options=o},https:{},base:{},service:{},buyMembership:()=>{},wx,getApp:()=>({$wepy:{$options:{}}}),setTimeout:()=>0,clearTimeout:()=>{}});
const p=Object.assign({},options.methods,{rankList:plans,cardCur:1,payData:{},virtualProducts:[],purchasing:false});p.syncSelectedPlan();assert.equal(p.rankList[1].position,'is-selected');assert.equal(p.rankList[0].position,'is-left');assert.equal(p.rankList[2].position,'is-right');assert.equal(p.payData.discount_price,'108');
p.selectPlan(0);assert.equal(p.shareId,10);assert.equal(p.rankList[0].position,'is-selected');
p.planTouchStart({touches:[{clientX:200,clientY:20}]});p.planTouchEnd({changedTouches:[{clientX:100,clientY:25}]});assert.equal(p.cardCur,1);assert.equal(p.shareId,11);
p.planTouchStart({touches:[{clientX:100,clientY:20}]});p.planTouchEnd({changedTouches:[{clientX:103,clientY:120}]});assert.equal(p.cardCur,1);
console.log('PASS quarter default, duration order, live prices, unknown price, selection, swipe and vertical-scroll guard');