Files
Halloween/tests/test_event_images_ui.js
T
2026-10-14 17:40:31 +02:00

49 lines
2.5 KiB
JavaScript

// Verify picture controls on real event-list renderer using fabricated records.
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const html = fs.readFileSync(path.join(__dirname, '../halloween/index.html'), 'utf8');
const renderer = html.slice(html.indexOf(' async function refreshEvents() {'), html.indexOf(' async function refreshDb() {'));
class Element {
constructor(tag) {this.tag=tag; this.children=[]; this.textContent='';}
append(...children) {this.children.push(...children);}
prepend(...children) {this.children.unshift(...children);}
add(child) {this.append(child);}
setAttribute(name,value) {this[name]=value;}
replaceChildren() {this.children=[];}
}
(async () => {
const list = new Element('ul');
let allowDelete = false, deleted = false;
const calls = [];
const record = {id:1, occurred_at:'2026-10-14T10:00:00+00:00', kind:'motion_direction_test', mode:'sanft', profile_id:null};
const context = vm.createContext({
document:{createElement:tag => new Element(tag)},
$:id => id === 'events-list' ? list : new Element('p'),
profiles:[], names:{sanft:'Sanft'}, Option:class {},
confirm:() => allowDelete,
api:async (url, body, method) => {calls.push({url,method}); deleted=true;},
fetch:async () => ({ok:true, json:async () => ({events:[{...record,image_url:deleted ? null : '/api/events/1/image'}]})})
});
vm.runInContext(renderer, context);
await vm.runInContext('refreshEvents()', context);
let item = list.children[0];
const link = item.children.find(element => element.tag === 'a');
assert.equal(link.href, '/api/events/1/image');
assert.equal(link.textContent, 'Bild ansehen');
assert.equal(link.children[0].src, link.href);
assert.equal(link.children[0].loading, 'lazy');
const remove = item.children.find(element => element.textContent === 'Bild löschen');
await remove.onclick();
assert.equal(calls.length, 0, 'Cancel never deletes a picture');
allowDelete=true;
await remove.onclick();
assert.deepEqual(calls, [{url:'/api/events/1/image',method:'DELETE'}]);
item=list.children[0];
assert.equal(item.children.some(element => element.tag === 'a'), false);
assert.ok(item.children.some(element => element.textContent.includes('Kein gespeichertes Bild')));
assert.ok(item.children.some(element => element.textContent.includes('#1')));
console.log('Event image UI tests passed');
})().catch(error => {console.error(error); process.exitCode=1;});