import { RMLEventAttributeName } from "./types/dom"; import { camelCase } from "./utils/camelCase"; type HTMLEventSource = { [key in RMLEventAttributeName]?: any; } const kebabCase = (str: string) => str .replace(/([a-z0-9])([A-Z])/g, "$1-$2") .replace(/[_\s]+/g, "-") .toLowerCase() ; export const defer = (value: T, timeout = 10) => new Promise((resolve) => setTimeout(() => resolve(value), timeout)); export interface MockElement extends HTMLElement { autoplay?: boolean, dataset: DOMStringMap; checked?: boolean; disabled?: boolean; style: CSSStyleDeclaration; value: string; innerText: string; innerHTML: string; textContent: string; readOnly?: boolean; remove: () => void; selectedIndex?: number; setAttribute(name: string, value: string): void; getAttribute(name: string): string | null; hasAttribute: (name: string) => boolean; removeAttribute(name: string): void; className: string; insertAdjacentHTML(pos: InsertPosition, html: string): void; addEventListener(eventName: string, handler: EventListenerOrEventListenerObject): void; removeEventListener(eventName: string, handler: EventListenerOrEventListenerObject): void; dispatchEvent(event: Event): boolean; childNodes: NodeListOf; }; export const MockElement = (props?: Record): MockElement => { const el = { 'focus': () => {}, 'blur': () => {}, 'style': {}, 'tagName': 'MOCKELEMENT', 'textContent': '', 'innerText': '', 'innerHTML': '', 'value': ['INPUT', 'BUTTON'].includes(props?.tagName) ? '' : undefined, 'autoplay': undefined, remove: () => {}, setAttribute(name: string, value: string) { this[name] = value; }, getAttribute(name: string) { return this[name]; }, hasAttribute(name: string) { return this[name] !== undefined && this[name] != null; }, removeAttribute(name: string) { this[name] = null; }, className: '', classList: { add(name: string) { el.className = el.className.split(' ').filter(x => x !== name).concat(name).join(' '); }, contains(name: string) { return new RegExp(`(?:^| )${name}(?: |$)`).test(el.className); }, remove(name: string) { el.className = el.className.split(' ').filter(x => x !== name).join(' '); }, toggle(name: string) { el.className.includes(name) ? el.classList.remove(name) : el.classList.add(name) ; }, }, dataset: new Proxy({}, { get(target, key: string) { return el.getAttribute(`data-${kebabCase(key.replace(/^data-/, ''))}`); }, set(target, key: string, value) { el.setAttribute(`data-${kebabCase(key.replace(/^data-/, ''))}`, value); return true; }, }), insertAdjacentHTML(pos: InsertPosition, html: string) { if(pos === 'beforeend') el.innerHTML += html else if(pos === 'afterbegin') el.innerHTML = html + el.innerHTML }, addEventListener(eventName: string, handler: EventListenerOrEventListenerObject) { (el)[`on${eventName}` as RMLEventAttributeName] = handler; }, dispatchEvent(event: Event) { (el)[`on${event.type}` as RMLEventAttributeName]?.(event); }, childNodes: [] as unknown as NodeListOf, ...props, }; return el; }; export const MockEvent = (name: string, data: Partial) => ({ bubbles: true, cancelable: true, currentTarget: null, defaultPrevented: false, eventPhase: 0, isTrusted: false, target: null, preventDefault: () => {}, stopPropagation: () => {}, timeStamp: 0, type: name, ...data, }); // Sink binding configuration test helper export const expectSinkBindingConfiguration = ( config: any, expectedType: string, expectedTag: string, expectedSource: any, expectedSink: any ) => { expect(config.type).toBe(expectedType); expect(config.t).toBe(expectedTag); expect(config.source).toBe(expectedSource); expect(config.sink).toBe(expectedSink); };