24 lines
2.7 KiB
JavaScript
24 lines
2.7 KiB
JavaScript
/* Exercise the production adapter's ISO values, mode changes and required-field guard. */
|
|
const assert=require('node:assert/strict'),fs=require('node:fs'),vm=require('node:vm');
|
|
function node(){return{children:[],classList:{add(){}},setAttribute(k,v){this[k]=v;},append(...items){this.children.push(...items);},prepend(item){this.children.unshift(item);},querySelector(){return this.children[0];}};}
|
|
const input={type:'date',dataset:{},value:'2026-10-25',required:true,disabled:false,labels:[{textContent:'Fra og med'}],getAttribute(){return null;},closest(selector){return selector==='dialog'?{}:null;}};
|
|
let options,opened=0,submit;
|
|
const error={textContent:''},form={dataset:{},querySelectorAll(){return[input];},querySelector(){return error;},addEventListener(name,handler){if(name==='submit')submit=handler;}};input.form=form;
|
|
const scope={querySelectorAll(selector){return selector==='input[data-planning-date-type]'&&!input.dataset.planningDateType?[]:[input];}};
|
|
function flatpickr(field,config){options=config;const picker={altInput:node(),calendarContainer:node(),setDate(value){field.value=value;},open(){opened++;},close(){},clear(){field.value='';},destroy(){delete field._flatpickr;}};field.type='hidden';field._flatpickr=picker;config.onReady([],field.value,picker);return picker;}
|
|
flatpickr.l10ns={da:{firstDayOfWeek:1}};
|
|
const context={window:{flatpickr},document:{createElement:node},Date};vm.createContext(context);vm.runInContext(fs.readFileSync('static/js/planning-dates.js','utf8'),context);
|
|
const dates=context.window.BMCPlanningDates;dates.mount(scope);
|
|
assert.equal(options.dateFormat,'Y-m-d');assert.equal(options.altFormat,'d.m.Y');assert.equal(options.enableTime,false);
|
|
assert.equal(input.value,'2026-10-25');assert.equal(options.locale.firstDayOfWeek,1);assert.equal(options.static,true);
|
|
assert.equal(input._flatpickr.altInput['aria-label'],'Fra og med');
|
|
dates.reset(scope);assert.equal(input.type,'date');assert.equal(input.value,'2026-10-25');
|
|
input.type='datetime-local';input.value='2026-10-25T08:30';dates.mount(scope);
|
|
assert.equal(options.enableTime,true);assert.equal(options.time_24hr,true);assert.equal(options.dateFormat,'Y-m-d\\TH:i');
|
|
assert.equal(input.value,'2026-10-25T08:30');
|
|
input.value='';let prevented=false,stopped=false;submit({preventDefault(){prevented=true;},stopImmediatePropagation(){stopped=true;}});
|
|
assert.ok(prevented&&stopped);assert.equal(opened,1);assert.match(error.textContent,/obligatoriske/);
|
|
input.value='2026-10-25T09:00';dates.mount(scope);assert.equal(input._flatpickr.altInput.disabled,false);
|
|
dates.reset(scope);assert.equal(input.type,'datetime-local');
|
|
console.log('Planning dates: Danish labels, ISO preservation, date/time switching, dialog placement and empty required fields OK');
|