Something went wrong. Try again.
Small reactivity library for the DOM. Inspired by Alpine.js
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242import { signal, effect } from "@preact/signals-core"
const styles = /*css*/` :host { border: 2px solid #686565; position: fixed; width: 300px; top: 0; right: 0; font-family: monospace; user-select: none; z-index: 9999; border-radius: 4px; font-size: 14px; }
#drag-header { cursor: move; padding: 8px; background-color: #f0f0f0; border-bottom: 1px solid #ccc; display: flex; align-items: center; justify-content: space-between; }
#drag-header button { cursor: pointer; padding: 2px 8px; }
summary { cursor: pointer; padding-block: 8px; }
span { display: inline-block; margin-inline-start: auto; }
#mock-console-content { padding: 8px; max-height: 90vh; overflow: scroll; background: #282525; color: white; }`
const baseMarkup = /*html*/` <div id="drag-header"> <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg" style="vertical-align: middle; margin-right: 6px; opacity: 0.6;"> <circle cx="5" cy="3" r="1.5" fill="currentColor"/> <circle cx="11" cy="3" r="1.5" fill="currentColor"/> <circle cx="5" cy="8" r="1.5" fill="currentColor"/> <circle cx="11" cy="8" r="1.5" fill="currentColor"/> <circle cx="5" cy="13" r="1.5" fill="currentColor"/> <circle cx="11" cy="13" r="1.5" fill="currentColor"/> </svg> <button id="run-btn">Run</button> </div> <div id="mock-console-content"></div>`
const pendingEmoji = "⏳"
function draggable(root) { const header = root.querySelector('#drag-header')
let startX = 0, startY = 0 let initialLeft = 0, initialTop = 0
const getPointerCoords = (e) => { return e.touches ? e.touches[0] : e }
const startDrag = (e) => { const pointer = getPointerCoords(e) startX = pointer.clientX startY = pointer.clientY
const rect = this.getBoundingClientRect() initialLeft = rect.left initialTop = rect.top
this.style.right = 'auto' this.style.left = `${initialLeft}px` this.style.top = `${initialTop}px`
document.addEventListener('mousemove', onDrag) document.addEventListener('mouseup', stopDrag) document.addEventListener('touchmove', onDrag, { passive: false }) document.addEventListener('touchend', stopDrag) }
const onDrag = (e) => { if (e.cancelable) e.preventDefault()
const pointer = getPointerCoords(e) const deltaX = pointer.clientX - startX const deltaY = pointer.clientY - startY
this.style.left = `${initialLeft + deltaX}px` this.style.top = `${initialTop + deltaY}px` }
const stopDrag = () => { document.removeEventListener('mousemove', onDrag) document.removeEventListener('mouseup', stopDrag) document.removeEventListener('touchmove', onDrag) document.removeEventListener('touchend', stopDrag) }
header.addEventListener('mousedown', startDrag) header.addEventListener('touchstart', startDrag, { passive: true })}
class TestConsole extends HTMLElement { static { customElements.define('test-console', TestConsole) }
data = signal([]) cleanupEffect = null
connectedCallback() { this.id = "mock-console" const shadow = this.attachShadow({ mode: "open" })
shadow.innerHTML = /*html*/` <style>${styles}</style> ${baseMarkup} ` draggable.bind(this)(shadow)
shadow.querySelector('#run-btn').addEventListener('click', () => { this.runAll() })
this.cleanupEffect = effect(() => { const tests = this.data.value tests.forEach(t => t.assertions.value) this.render() }) }
disconnectedCallback() { this.cleanupEffect?.() }
registerTestObject(testObj) { this.data.value = [...this.data.value, testObj] }
updateAssertion(testObj, label, status) { testObj.assertions.value = testObj.assertions.value.map(a => a.label === label ? { ...a, passed: status } : a ) }
async runAll() { for (const testObj of this.data.value) { for (const assertion of testObj.assertions.value) { if (assertion.passed !== "pending") continue
await new Promise((resolve) => { setTimeout(async () => { try { const result = await assertion.fn() this.updateAssertion(testObj, assertion.label, !!result) } catch { this.updateAssertion(testObj, assertion.label, false) } resolve() }, 1000) }) } } }
testComponent(data) { const assertions = data.assertions.value const num = assertions.length const passed = assertions.filter(d => d.passed === true).length const isPending = assertions.some(d => d.passed === "pending") const detailsText = isPending ? pendingEmoji : `${passed} of ${num} passed ${passed === num ? '✅' : '❌'}`
return /*html*/` <details open> <summary>[Run: ${data.label}] <span>${detailsText}</span></summary> <ul> ${assertions.map(a => /*html*/` <li>${a.label} ${a.passed === true ? "✅" : a.passed === "pending" ? pendingEmoji : "❌"}</li> `).join("")} </ul> </details> ` }
render() { const e = this.shadowRoot.getElementById('mock-console-content') if (e) { e.innerHTML = this.data.value.map(data => this.testComponent(data)).join("") } }}
if (!document.querySelector('test-console')) { document.body.append(document.createElement('test-console'))}
export const runner = document.querySelector('test-console')
let currentTestQueue = null
export const test = async (label, cb) => { const testObj = { label, assertions: signal([]) } runner.registerTestObject(testObj)
currentTestQueue = testObj try { await cb() } finally { currentTestQueue = null }}
export const assert = (label, cb) => { if (currentTestQueue) { currentTestQueue.assertions.value = [ ...currentTestQueue.assertions.value, { label, passed: "pending", fn: cb } ] }}