|
4 | 4 | and per-node event-registry teardown. The Lynx PAPI globals (`__GetConfig`, |
5 | 5 | `__AddEvent`) and the `runtime`/`lynx` host objects are stubbed here — in a web |
6 | 6 | build they don't exist, which is exactly why this code has no other coverage. */ |
7 | | -import { test, expect, describe, beforeEach, afterAll, beforeAll } from 'bun:test'; |
8 | | -import { routeEvent, destroyNodeEvents, drawingContext } from '../miso/native/mts/context'; |
| 7 | +import { test, expect, describe, beforeEach, afterEach, afterAll, beforeAll } from 'bun:test'; |
| 8 | +import { routeEvent, destroyNodeEvents, drawingContext, listStates, destroyListState } from '../miso/native/mts/context'; |
| 9 | +import { dropChildren } from '../miso/native/mts'; |
9 | 10 | import { vnode, vfrag, vcomp, vtext } from '../miso/smart'; |
10 | 11 | import type { EventContext, NodeId } from '../miso/types'; |
11 | 12 |
|
@@ -242,3 +243,63 @@ describe('drawingContext.removeAttribute — MTS', () => { |
242 | 243 | delete (globalThis as any).__SetAttribute; |
243 | 244 | }); |
244 | 245 | }); |
| 246 | + |
| 247 | +// Regression: dropChildren (ts/miso/native/mts.ts) recursively tears down a |
| 248 | +// removed subtree's event state via destroyNodeEvents, but previously never |
| 249 | +// touched listStates (ts/miso/native/mts/context.ts) -- so a <list> removed |
| 250 | +// via an ancestor wrapper (rather than directly) kept its {node, items, |
| 251 | +// known} entry, and every ElementRef in it, in that Map forever. |
| 252 | +describe('destroyListState / dropChildren — <list> virtualization teardown', () => { |
| 253 | + |
| 254 | + afterEach(() => { |
| 255 | + for (const g of ['__GetElementUniqueID', '__FirstElement', '__NextElement']) |
| 256 | + delete (globalThis as any)[g]; |
| 257 | + }); |
| 258 | + |
| 259 | + test('destroyListState clears a node\'s own list state', () => { |
| 260 | + const node: El = { nodeId: 72 }; |
| 261 | + (globalThis as any).__GetElementUniqueID = (n: El) => n.nodeId; |
| 262 | + |
| 263 | + listStates.set(72, { node: node as any, items: [{} as any], known: 1 }); |
| 264 | + expect(listStates.has(72)).toBeTrue(); |
| 265 | + |
| 266 | + destroyListState(node as any); |
| 267 | + expect(listStates.has(72)).toBeFalse(); |
| 268 | + }); |
| 269 | + |
| 270 | + test('destroyListState on a node with no list state is a harmless no-op', () => { |
| 271 | + const node: El = { nodeId: 73 }; |
| 272 | + (globalThis as any).__GetElementUniqueID = (n: El) => n.nodeId; |
| 273 | + expect(() => destroyListState(node as any)).not.toThrow(); |
| 274 | + }); |
| 275 | + |
| 276 | + test('dropChildren clears the listStates entry of a <list> nested under a removed wrapper', () => { |
| 277 | + // tree: wrapper(70) -> list(71), removed as a unit via the wrapper -- |
| 278 | + // the list itself is never the direct argument to removeChild/replaceChild. |
| 279 | + const wrapper: El = { nodeId: 70 }; |
| 280 | + const list: El = { nodeId: 71 }; |
| 281 | + |
| 282 | + (globalThis as any).__GetElementUniqueID = (n: El) => n.nodeId; |
| 283 | + const childrenOf = new Map<number, El[]>([[70, [list]], [71, []]]); |
| 284 | + (globalThis as any).__FirstElement = (n: El) => childrenOf.get(n.nodeId)?.[0] ?? null; |
| 285 | + (globalThis as any).__NextElement = (n: El) => { |
| 286 | + for (const kids of childrenOf.values()) { |
| 287 | + const i = kids.indexOf(n); |
| 288 | + if (i >= 0 && i + 1 < kids.length) return kids[i + 1]; |
| 289 | + } |
| 290 | + return null; |
| 291 | + }; |
| 292 | + |
| 293 | + listStates.set(71, { node: list as any, items: [{} as any, {} as any], known: 2 }); |
| 294 | + expect(listStates.has(71)).toBeTrue(); |
| 295 | + |
| 296 | + const nodeMap: Record<number, El> = { 70: wrapper, 71: list }; |
| 297 | + dropChildren(nodeMap as any, wrapper as any); |
| 298 | + |
| 299 | + // the list's virtualization state (and every ElementRef it held) is gone, |
| 300 | + // not just its runtime.nodes entry |
| 301 | + expect(listStates.has(71)).toBeFalse(); |
| 302 | + expect(nodeMap[70]).toBeUndefined(); |
| 303 | + expect(nodeMap[71]).toBeUndefined(); |
| 304 | + }); |
| 305 | +}); |
0 commit comments