type TestMutationObserver = { callback: MutationCallback; options?: MutationObserverInit; }; let pendingAttributes: Record | undefined; const parseAttributes = (html: string) => Object.fromEntries( [...html.matchAll(/\s([^\s=]+)="([^"]*)"/g)] .map(([, name, value]) => [name, value]) ); class TestAttr { constructor(public nodeName: string, public nodeValue: string) {} } class TestNamedNodeMap extends Array { resolve?: TestAttr; constructor(attrs: Record = {}) { super(); Object.entries(attrs).forEach(([name, value]) => this.set(name, value)); } set(name: string, value: string) { const attr = new TestAttr(name, value); const index = this.findIndex(item => item.nodeName === name); if(index === -1) { this.push(attr); } else { this[index] = attr; } if(name === 'resolve') { this.resolve = attr; } } } class TestHTMLElement { attributes = new TestNamedNodeMap(pendingAttributes); childNodes: Node[] = []; shadowRoot: { innerHTML: string; children: TestHTMLElement[]; querySelectorAll: () => TestHTMLElement[] } | null = null; private observers: TestMutationObserver[] = []; attachShadow() { return this.shadowRoot = { innerHTML: '', children: [], querySelectorAll: () => [], }; } getAttribute(name: string) { return this.attributes.find(attr => attr.nodeName === name)?.nodeValue ?? null; } hasAttribute(name: string) { return this.attributes.some(attr => attr.nodeName === name); } removeAttribute(name: string) { const index = this.attributes.findIndex(attr => attr.nodeName === name); if(index > -1) { this.attributes.splice(index, 1); } } setAttribute(name: string, value: string) { this.attributes.set(name, String(value)); this.observers .filter(observer => observer.options?.attributes) .forEach(observer => observer.callback([{ attributeName: name } as MutationRecord], observer as unknown as MutationObserver)); } __observe(observer: TestMutationObserver) { this.observers.push(observer); } } class TestMutationObserverClass { constructor(private callback: MutationCallback) {} observe(target: TestHTMLElement, options?: MutationObserverInit) { target.__observe({ callback: this.callback, options }); } disconnect() {} } const registry = new Map(); let currentElement: TestHTMLElement | null = null; globalThis.HTMLElement = TestHTMLElement as unknown as typeof HTMLElement; globalThis.MutationObserver = TestMutationObserverClass as unknown as typeof MutationObserver; globalThis.customElements = { define: (tagName: string, constructor: CustomElementConstructor) => registry.set(tagName, constructor), get: (tagName: string) => registry.get(tagName), } as unknown as CustomElementRegistry; globalThis.document = { body: { set innerHTML(html: string) { const tagName = html.match(/^<([a-z0-9-]+)/)?.[1]; const Constructor = tagName ? registry.get(tagName) : undefined; if(!Constructor) { currentElement = null; return; } pendingAttributes = parseAttributes(html); currentElement = new Constructor() as unknown as TestHTMLElement; pendingAttributes = undefined; (currentElement as unknown as HTMLElement).connectedCallback?.(); }, querySelector: () => currentElement, } } as unknown as Document; const { RegisterElement } = await import("./custom-element"); const { Rimmel_Bind_Subtree } = await import("./lifecycle/data-binding"); const { rml } = await import("./parser/parser"); const { Subject } = await import("rxjs"); describe('Custom Element', () => { describe('RegisterElement', () => { it('should register a custom element without errors', () => { const tagName = `test-custom-element-${crypto.randomUUID()}`; RegisterElement(tagName, null, () => {}); expect(customElements.get(tagName)).toBeDefined(); }); describe('Virtual Web Component', () => { it('can access the host element in the init function', () => { const tagName = `test-virtual-component-element-${crypto.randomUUID()}`; let host: Element | undefined; RegisterElement(tagName, null, (el: Element) => { host = el; }); document.body.innerHTML = `<${tagName}>`; expect(host).toBeInstanceOf(HTMLElement); }); it('exposes a live attribute stream in the init function', async () => { const tagName = `test-virtual-component-stream-${crypto.randomUUID()}`; const seen: Array = []; RegisterElement(tagName, null, (_el: Element, { key }) => { key.subscribe((value: string | null) => seen.push(value)); }); document.body.innerHTML = `<${tagName} key="tweet">`; const el = document.body.querySelector(tagName)!; el.setAttribute('key', 'tweet2'); await new Promise(resolve => setTimeout(resolve, 0)); expect(seen).toEqual(['tweet', 'tweet2']); }); it('does not reflect virtual element sink updates back into attribute streams', async () => { const tagName = `test-virtual-component-bound-attribute-${crypto.randomUUID()}`; const source = new Subject(); const seen: unknown[] = []; RegisterElement(tagName, null, (_el: Element, { path }) => { path.subscribe((value: unknown) => seen.push(value)); }); document.body.innerHTML = rml`<${tagName} path="${source}">`; const el = document.body.querySelector(tagName)! as unknown as TestHTMLElement; Rimmel_Bind_Subtree(el as unknown as Element); const coords = [[0, 100], [100, 0]]; source.next(coords); await new Promise(resolve => setTimeout(resolve, 0)); expect(seen).toEqual([coords]); expect(el.getAttribute('path')).toBe(''); }); }) }); });