bmc_hub/tests/test_internal_message_dom.cjs
2026-09-08 01:55:47 +02:00

31 lines
2.6 KiB
JavaScript

const {JSDOM}=require('jsdom');
const fs=require('node:fs');
const assert=require('node:assert/strict');
(async()=>{
const dom=new JSDOM(fs.readFileSync('app/shared/frontend/internal_message.html','utf8'),{url:'http://localhost:8001',runScripts:'outside-only'});
const w=dom.window,d=w.document;let sent=null,fail=false;
w.bootstrap={Modal:{getOrCreateInstance:()=>({show(){},hide(){}})}};
w.fetch=async(url,options={})=>{
if(options.method==='POST'){sent=JSON.parse(options.body);return {ok:!fail,json:async()=>fail?{detail:'Testfejl'}:{message:'Besked sendt'}};}
return {ok:true,json:async()=>url.includes('auth/me')?{id:1}:url.includes('users')?[{id:1,full_name:'Mig'},{id:2,full_name:'Kollega'}]:[]};
};
w.eval(fs.readFileSync('static/js/internal-message.js','utf8'));
await w.openInternalMessage();
assert.equal(d.getElementById('im-recipient').options.length,2);
assert.equal(sent,null,'Opening must not send anything');
d.getElementById('im-recipient').value='2';d.getElementById('im-text').value='Kort besked';
d.getElementById('internalMessageForm').dispatchEvent(new w.Event('submit',{cancelable:true}));await new Promise(r=>setTimeout(r,10));
assert.equal(sent.contact_id,null);assert.equal(sent.message_kind,'message');assert.equal(sent.recipient_user_id,2);
await w.openInternalMessage({contact:{id:4,name:'Kontakt'}});
d.querySelector('[data-internal-kind="phone"]').click();
assert.ok(!d.getElementById('im-phone-fields').classList.contains('d-none'));
d.getElementById('im-recipient').value='2';d.getElementById('im-phone').value='12345678';d.getElementById('im-text').value='Ring venligst tilbage';
fail=true;d.getElementById('internalMessageForm').dispatchEvent(new w.Event('submit',{cancelable:true}));await new Promise(r=>setTimeout(r,10));
assert.equal(sent.contact_id,4);assert.equal(sent.caller_name,'Kontakt');assert.equal(sent.callback_phone,'12345678');assert.equal(sent.message_kind,'phone');
assert.equal(d.getElementById('im-text').value,'Ring venligst tilbage','Failure must preserve draft');
d.getElementById('im-contact').querySelector('button').click();fail=false;
d.getElementById('internalMessageForm').dispatchEvent(new w.Event('submit',{cancelable:true}));await new Promise(r=>setTimeout(r,10));
assert.equal(sent.contact_id,null);assert.equal(sent.caller_name,'Kontakt');assert.equal(d.getElementById('im-text').value,'');
dom.window.close();console.log('PASS: explicit sending, recipient choice, normal/phone messages, optional/removable contact, failure preserves text');
})().catch(e=>{console.error(e);process.exit(1);});