|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { createElement as h, type Context, type Element as CrankElement } from '@b9g/crank'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { Document, Element, Text } from '@bomb.sh/dom'; |
| 5 | +import { Event } from '@bomb.sh/dom'; |
| 6 | +import { createRoot } from '../src/index.ts'; |
| 7 | + |
| 8 | +describe('actual Crank', () => { |
| 9 | + it('runs generator components, refreshes asynchronously, and cleans up without globals', async () => { |
| 10 | + const documents = [new Document(), new Document()]; |
| 11 | + await using first = createRoot(documents[0]!.createElement('box')); |
| 12 | + await using second = createRoot(documents[1]!.createElement('box')); |
| 13 | + const refreshes: Array<() => void> = []; |
| 14 | + const cleanups: number[] = []; |
| 15 | + function* Counter( |
| 16 | + this: Context<{ index: number }>, |
| 17 | + { index }: { index: number }, |
| 18 | + ): Generator<CrankElement> { |
| 19 | + let count = 0; |
| 20 | + refreshes[index] = () => { |
| 21 | + count++; |
| 22 | + void this.refresh(); |
| 23 | + }; |
| 24 | + this.cleanup(() => { |
| 25 | + cleanups.push(index); |
| 26 | + }); |
| 27 | + for (const _ of this) yield h('text', null, count); |
| 28 | + } |
| 29 | + await Promise.all([ |
| 30 | + first.render(h(Counter, { index: 0 })), |
| 31 | + second.render(h(Counter, { index: 1 })), |
| 32 | + ]); |
| 33 | + const original = first.element.firstChild; |
| 34 | + assert.ok(original instanceof Element); |
| 35 | + const content = original.firstChild; |
| 36 | + assert.ok(content instanceof Text); |
| 37 | + await Promise.resolve(); |
| 38 | + refreshes[0]?.(); |
| 39 | + refreshes[1]?.(); |
| 40 | + await expect.poll(() => first.element.textContent).toBe('1'); |
| 41 | + await expect.poll(() => second.element.textContent).toBe('1'); |
| 42 | + expect(first.element.firstChild).toBe(original); |
| 43 | + expect(original.firstChild).toBe(content); |
| 44 | + expect(content.ownerDocument).toBe(documents[0]); |
| 45 | + expect(second.element.firstChild?.ownerDocument).toBe(documents[1]); |
| 46 | + await first.unmount(); |
| 47 | + await second.unmount(); |
| 48 | + expect(cleanups).toEqual([0, 1]); |
| 49 | + expect(first.element.childNodes).toHaveLength(0); |
| 50 | + expect(globalThis).not.toHaveProperty('document'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('reorders keyed nodes and replaces native event handlers', async () => { |
| 54 | + const document = new Document(); |
| 55 | + await using root = createRoot(document.createElement('box')); |
| 56 | + const calls: string[] = []; |
| 57 | + const rows = (names: string[], label: string): CrankElement[] => |
| 58 | + names.map((name) => |
| 59 | + h( |
| 60 | + 'text', |
| 61 | + { |
| 62 | + key: name, |
| 63 | + onClick: () => calls.push(label), |
| 64 | + }, |
| 65 | + name, |
| 66 | + ), |
| 67 | + ); |
| 68 | + await root.render(rows(['a', 'b', 'c'], 'before')); |
| 69 | + const original = [...root.element.childNodes]; |
| 70 | + await root.render(rows(['c', 'a'], 'after')); |
| 71 | + expect(root.element.childNodes).toEqual([original[2], original[0]]); |
| 72 | + root.element.firstChild?.dispatchEvent(new Event('click')); |
| 73 | + expect(calls).toEqual(['after']); |
| 74 | + }); |
| 75 | +}); |
0 commit comments