34 lines
2.6 KiB
JavaScript
34 lines
2.6 KiB
JavaScript
const test=require('node:test');
|
|
const assert=require('node:assert/strict');
|
|
const fs=require('node:fs');
|
|
const path=require('node:path');
|
|
const createLoader=require('./load-module.cjs');
|
|
function setup(overrides={},coupleId=1) {
|
|
let definition;const urls=[];
|
|
const api={timelineList:async()=>[{id:1},{id:2}],albumList:async()=>[{photo_count:'3'},{photo_count:7}],diaryList:async()=>({items:[{id:1}],total:'27'}),songsList:async()=>[{id:1}],checkinList:async()=>({items:[{id:1}],checkin_log_total:'17'}),petInfo:async()=>({has_pet:true,pet:{level:'13'}}),...overrides};
|
|
const load=createLoader({Page:value=>definition=value,wx:{getStorageSync:key=>key==='coupleId'?coupleId:1,setStorageSync(){},navigateTo:value=>urls.push(value.url)},console,Date}, {'../../utils/request':{api},'../../utils/page-cache':{withDataCache:value=>value}});
|
|
load(path.join(__dirname,'../pages/profile/profile.js'));
|
|
const page={...definition,data:JSON.parse(JSON.stringify(definition.data)),setData:value=>Object.assign(page.data,value)};
|
|
return {page,urls};
|
|
}
|
|
test('profile statistics use photo and diary totals, checkin count and explicit pet level; all eight destinations exist',async()=>{
|
|
const e=setup();await e.page.loadProfileStats();
|
|
assert.deepEqual(e.page.data.statisticItems.map(item=>item.count),[2,10,27,1,17,'Lv.13','','']);
|
|
assert.equal(e.page.data.statsError,false);assert.equal(e.page.data.statsLoading,false);
|
|
const config=JSON.parse(fs.readFileSync(path.join(__dirname,'../app.json'),'utf8'));
|
|
for(const item of e.page.data.statisticItems) e.page.openStatistic({currentTarget:{dataset:{key:item.key}}});
|
|
assert.equal(e.urls.length,8);for(const url of e.urls)assert.ok(config.pages.includes(url.slice(1)),url);
|
|
});
|
|
test('a failed statistic is unknown while other entries stay available and retry replaces it',async()=>{
|
|
let fail=true;const e=setup({albumList:async()=>{if(fail)throw Error('offline');return [{photo_count:4}];}});
|
|
await e.page.loadProfileStats();assert.equal(e.page.data.statisticItems[1].count,'—');assert.equal(e.page.data.statisticItems[2].count,27);assert.equal(e.page.data.statsError,true);
|
|
fail=false;await e.page.loadProfileStats();assert.equal(e.page.data.statisticItems[1].count,4);assert.equal(e.page.data.statsError,false);
|
|
});
|
|
|
|
test('unbound profile skips couple-only pets without reporting a statistics failure',async()=>{
|
|
let calls=0;const e=setup({petInfo:async()=>{calls++;throw Error('请先绑定情侣');}},null);
|
|
await e.page.loadProfileStats();
|
|
assert.equal(calls,0);assert.equal(e.page.data.statsError,false);
|
|
assert.equal(e.page.data.statisticItems.find(item=>item.key==='pet').count,'未领养');
|
|
});
|