bmc_hub/tests/js/case-week-calendar.test.cjs

43 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

const {test} = require('node:test');
const assert = require('node:assert/strict');
const vm = require('node:vm');
const fs = require('node:fs');
const source = fs.readFileSync('static/js/case-week-calendar.js', 'utf8');
function render(items) {
const host = {dataset: {}, style: {setProperty(name,value) {this[name]=value;}}, classList: {add() {}}, addEventListener() {}};
const window = {};
vm.runInNewContext(source, {window, document: {getElementById: () => ({textContent:''})}});
window.renderCaseWeekCalendar(host, new Date(2026,9,19,12), items);
return host;
}
function item(start,end) {return {id:1,version:2,starts_at:start,ends_at:end,user_name:'Test'};}
test('empty week defaults to five weekdays and 07–17', () => {
const host = render([]);
assert.equal(host.style['--week-days'],5);
assert.equal(host.dataset.firstHour,'7');
assert.equal(host.dataset.lastHour,'17');
assert.doesNotMatch(host.innerHTML,/data-date="2026-10-24"/);
});
test('early and late bookings extend the visible hours', () => {
const host = render([item('2026-10-20T05:30:00','2026-10-20T18:15:00')]);
assert.equal(host.dataset.firstHour,'5');
assert.equal(host.dataset.lastHour,'19');
assert.match(host.innerHTML,/data-allocation-id="1"/);
});
test('only occupied weekend days appear; other weeks do not extend range', () => {
const host = render([item('2026-10-25T10:00:00','2026-10-25T11:00:00'),item('2026-11-01T01:00:00','2026-11-01T23:00:00')]);
assert.equal(host.style['--week-days'],6);
assert.match(host.innerHTML,/data-date="2026-10-25"/);
assert.doesNotMatch(host.innerHTML,/data-date="2026-10-24"/);
assert.equal(host.dataset.firstHour,'7');
assert.equal(host.dataset.lastHour,'17');
});
test('overnight booking remains visible on both days', () => {
const host = render([item('2026-10-23T23:30:00','2026-10-24T01:00:00')]);
assert.equal(host.style['--week-days'],6);
assert.equal(host.dataset.firstHour,'0');
assert.equal(host.dataset.lastHour,'24');
assert.equal((host.innerHTML.match(/class="case-planner-calendar-item v5-time-event"/g)||[]).length,2);
assert.equal((host.innerHTML.match(/data-allocation-id="1"/g)||[]).length,1);
});