lion/packages/calendar/test-helpers/DayObject.js
2020-10-06 12:41:03 +02:00

92 lines
2 KiB
JavaScript

import { weekdayNames } from './weekdayNames.js';
/**
* Abstraction around calendar day DOM structure,
* allows for writing readable, 'DOM structure agnostic' tests
*/
export class DayObject {
/**
* @param {HTMLElement} dayEl
*/
constructor(dayEl) {
this.el = dayEl;
}
/**
* Node references
*/
get calendarShadowRoot() {
return this.el.parentNode?.parentNode?.parentNode?.parentNode?.parentNode?.parentNode
?.parentNode;
}
get cellEl() {
return /** @type {HTMLElement} */ (this.el.parentElement);
}
get buttonEl() {
return this.el;
}
/**
* States
*/
get isDisabled() {
return this.buttonEl.hasAttribute('disabled');
}
get isSelected() {
return this.buttonEl.hasAttribute('selected');
}
get isToday() {
return this.buttonEl.hasAttribute('today');
}
get isCentral() {
return this.buttonEl.getAttribute('tabindex') === '0';
}
get isFocused() {
return /** @type {ShadowRoot} */ (this.calendarShadowRoot).activeElement === this.buttonEl;
}
get monthday() {
return Number(this.buttonEl.textContent);
}
/**
* Text
*/
get weekdayNameShort() {
const weekdayEls = Array.from(
/** @type {HTMLElement} */ (this.el.parentElement?.parentElement).querySelectorAll(
'.calendar__day-cell',
),
);
const dayIndex = weekdayEls.indexOf(/** @type {HTMLElement} */ (this.el.parentElement));
return weekdayNames['en-GB'].Sunday.short[dayIndex];
}
get weekdayNameLong() {
const weekdayEls = Array.from(
/** @type {HTMLElement} */ (this.el.parentElement?.parentElement).querySelectorAll(
'.calendar__day-cell',
),
);
const dayIndex = weekdayEls.indexOf(/** @type {HTMLElement} */ (this.el.parentElement));
return weekdayNames['en-GB'].Sunday.long[dayIndex];
}
/**
* Other
*/
get cellIndex() {
return Array.from(/** @type {HTMLElement} */ (this.cellEl.parentElement).children).indexOf(
this.cellEl,
);
}
}