forked from unimatrix/web
35 lines
5.2 KiB
JavaScript
35 lines
5.2 KiB
JavaScript
import {test} from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {mountPostbankPayments} from '../control-panel/public/cp-v2/cp-v2-postbank-payments.mjs';
|
|
class Node{
|
|
constructor(tag){this.tagName=tag.toUpperCase();this.children=[];this.attrs={};this._text='';this._value=null;this.disabled=false;this.checked=false;this.hidden=false;}
|
|
set textContent(value){this._text=String(value);this.children=[];}get textContent(){return this._text+this.children.map(n=>n.textContent).join('');}
|
|
get value(){return this._value??(this.tagName==='SELECT'?this.children[0]?.value||'':'');}set value(value){this._value=String(value);}
|
|
get options(){return this.children;}append(...nodes){for(const node of nodes){node.parent=this;this.children.push(node);}}
|
|
replaceChildren(...nodes){this.children=[];this._text='';if(this.tagName==='SELECT')this._value=null;this.append(...nodes);}
|
|
setAttribute(key,value){this.attrs[key]=String(value);}closest(){return null;}focus(){}reportValidity(){return true;}
|
|
querySelectorAll(selector){return all(this).filter(n=>selector.toUpperCase().split(',').includes(n.tagName));}
|
|
remove(){if(this.parent)this.parent.children=this.parent.children.filter(n=>n!==this);}
|
|
}
|
|
const all=node=>node.children.flatMap(child=>[child,...all(child)]),settle=async()=>{for(let i=0;i<5;i++)await new Promise(resolve=>setImmediate(resolve));};
|
|
async function fixture(work,{search=async page=>({recipients:[{name:'External Savings',holder:'External',number:'DR22103000000002'}],page,hasMore:page===1})}={}){
|
|
const previousDocument=globalThis.document,previousWindow=globalThis.window;globalThis.document={createElement:tag=>new Node(tag)};globalThis.window={addEventListener(){},removeEventListener(){}};
|
|
const host=new Node('main'),calls=[],base={holder:'You',entityId:'person',canReceive:true,canView:true,canDebit:true,available:'100.00',instantQuota:5,isPersonalOwner:true};
|
|
const accounts=[{...base,id:'1',name:'Your Current',number:'DR25102000000005'},{...base,id:'2',name:'Your Savings',number:'DR50103000000006'},{...base,id:'3',name:'Foreign Account',number:'DR14102000000007',canView:false,canDebit:false},{...base,id:'4',name:'Treasury',number:'DR8297900000007',canView:false,canDebit:false},{...base,id:'5',name:'Delegated Account',number:'DR14102000000008',canDebit:false}];
|
|
const dispose=mountPostbankPayments(host,'bank-transfers',{fetcher:async(path,options)=>{calls.push({path,options});if(path.startsWith('/api/bank/recipients?'))return Response.json(await search(Number(new URL(path,'https://cp.dunerealms.com').searchParams.get('page'))));return Response.json({accounts,beneficiaries:[{active:true,label:'Saved External',number:accounts[2].number}],fees:{message:'No fee'},standard:{processing:'Every four hours'},security:{enabled:false}});}});
|
|
const nodes=()=>all(host),button=label=>nodes().find(n=>n.tagName==='BUTTON'&&n.textContent===label);
|
|
try{await settle();await work({host,calls,nodes,button});}finally{dispose();globalThis.document=previousDocument;globalThis.window=previousWindow;}
|
|
}
|
|
test('wire dropdown lists owned and delegated accounts plus search; inspection-only and saved external accounts stay out',()=>fixture(async({nodes})=>{
|
|
const selects=nodes().filter(n=>n.tagName==='SELECT'),debit=selects[0],credit=selects[1];assert.equal(debit.options.length,2);assert.equal(credit.options.length,4);
|
|
assert(credit.textContent.includes('Your Savings'));assert(credit.textContent.includes('Delegated Account'));assert(credit.textContent.includes('Search other accounts'));assert(!credit.textContent.includes('Foreign Account'));assert(!credit.textContent.includes('Treasury'));assert(!credit.textContent.includes('Saved External'));
|
|
}));
|
|
test('beneficiary search displays account names and allows every result page to be reached',()=>fixture(async({nodes,button,calls})=>{
|
|
const credit=nodes().filter(n=>n.tagName==='SELECT')[1];credit.value='external';credit.onchange();const query=nodes().find(n=>n.type==='search');query.value='External';query.oninput();await button('Find beneficiary').onclick();
|
|
const found=nodes().find(n=>n.attrs['aria-label']==='Verified beneficiary account');assert(found.textContent.includes('External Savings'));assert.equal(button('Next matches').disabled,false);await button('Next matches').onclick();assert.equal(button('Previous matches').disabled,false);assert.equal(button('Next matches').disabled,true);assert(calls.some(c=>c.path.includes('page=2')));
|
|
query.value='New search';query.oninput();assert.equal(found.options.length,0);assert.equal(button('Next matches').disabled,true);await button('Find beneficiary').onclick();assert(calls.at(-1).path.includes('page=1'));
|
|
}));
|
|
test('changing a search while results are loading discards the stale response',()=>fixture(async({nodes,button})=>{
|
|
const credit=nodes().filter(n=>n.tagName==='SELECT')[1];credit.value='external';credit.onchange();const query=nodes().find(n=>n.type==='search');query.value='First';
|
|
query.oninput();const pending=button('Find beneficiary').onclick();query.value='Changed';query.oninput();await pending;assert.equal(nodes().find(n=>n.attrs['aria-label']==='Verified beneficiary account').options.length,0);
|
|
},{search:async page=>{await new Promise(resolve=>setImmediate(resolve));return {recipients:[{name:'Stale',number:'DR22103000000002'}],page,hasMore:false};}}));
|

Staff