|
| 1 | +import { describe, expect, test } from 'vitest'; |
| 2 | +import { create, destroy, id, LifecycleApi } from './lifecycle.ts'; |
| 3 | +import type { Node } from './node.ts'; |
| 4 | + |
| 5 | +describe('lifecycle', () => { |
| 6 | + describe('create', () => { |
| 7 | + test('with no parent, roots the node in the global scope', () => { |
| 8 | + const node = create(); |
| 9 | + expect(node.parent).toBeDefined(); |
| 10 | + }); |
| 11 | + |
| 12 | + test('wires the child into its parent', () => { |
| 13 | + const parent = create(); |
| 14 | + const child = create(parent); |
| 15 | + expect(child.parent).toBe(parent); |
| 16 | + expect(parent.children.has(child)).toBe(true); |
| 17 | + }); |
| 18 | + |
| 19 | + test('autogenerates an id', () => { |
| 20 | + const node = create(); |
| 21 | + expect(typeof id(node)).toBe('string'); |
| 22 | + expect(id(node).length).toBeGreaterThan(0); |
| 23 | + }); |
| 24 | + |
| 25 | + test('autogenerated ids are distinct', () => { |
| 26 | + expect(id(create())).not.toBe(id(create())); |
| 27 | + }); |
| 28 | + |
| 29 | + test('a child owns its id rather than inheriting its parent’s', () => { |
| 30 | + const parent = create(); |
| 31 | + const child = create(parent); |
| 32 | + expect(id(child)).not.toBe(id(parent)); |
| 33 | + }); |
| 34 | + |
| 35 | + test('hooks fire through create middleware', () => { |
| 36 | + const created: Node[] = []; |
| 37 | + const root = create(); |
| 38 | + LifecycleApi.around(root, { |
| 39 | + create: (args, next) => { |
| 40 | + const node = next(...args); |
| 41 | + created.push(node); |
| 42 | + return node; |
| 43 | + }, |
| 44 | + }); |
| 45 | + const a = create(root); |
| 46 | + const b = create(a); |
| 47 | + expect(created).toEqual([a, b]); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('destroy', () => { |
| 52 | + test('detaches a node from its parent', () => { |
| 53 | + const parent = create(); |
| 54 | + const child = create(parent); |
| 55 | + destroy(child); |
| 56 | + expect(parent.children.has(child)).toBe(false); |
| 57 | + }); |
| 58 | + |
| 59 | + test('cascades to descendants', () => { |
| 60 | + const root = create(); |
| 61 | + const parent = create(root); |
| 62 | + const child = create(parent); |
| 63 | + destroy(root); |
| 64 | + expect(root.children.has(parent)).toBe(false); |
| 65 | + expect(parent.children.has(child)).toBe(false); |
| 66 | + }); |
| 67 | + |
| 68 | + test('tears down bottom-up, each node running its destroy middleware', () => { |
| 69 | + const order: Node[] = []; |
| 70 | + const root = create(); |
| 71 | + LifecycleApi.around(root, { |
| 72 | + destroy: (args, next) => { |
| 73 | + const result = next(...args); |
| 74 | + order.push(args[0]); |
| 75 | + return result; |
| 76 | + }, |
| 77 | + }); |
| 78 | + const parent = create(root); |
| 79 | + const child = create(parent); |
| 80 | + destroy(root); |
| 81 | + expect(order).toEqual([child, parent, root]); |
| 82 | + }); |
| 83 | + }); |
| 84 | +}); |
0 commit comments