|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 3 | +import { blockOwnerOf, type BlockOwnerDebugNg } from '../block-owner.ts'; |
| 4 | +import { elementById } from '../element-id.ts'; |
| 5 | + |
| 6 | +class _SignalsExample {} |
| 7 | +class _ExamplePanel {} |
| 8 | +class _Card {} |
| 9 | + |
| 10 | +function page() { |
| 11 | + document.body.innerHTML = ` |
| 12 | + <app-root> |
| 13 | + <app-example-panel><div class="panel"><app-card></app-card></div></app-example-panel> |
| 14 | + </app-root>`; |
| 15 | + const root = document.querySelector('app-root')!; |
| 16 | + const panel = document.querySelector('app-example-panel')!; |
| 17 | + const card = document.querySelector('app-card')!; |
| 18 | + const anchor = document.createComment('container'); |
| 19 | + panel.querySelector('.panel')!.appendChild(anchor); |
| 20 | + const instances = new Map<Element, object>([ |
| 21 | + [root, new _SignalsExample()], |
| 22 | + [panel, new _ExamplePanel()], |
| 23 | + [card, new _Card()], |
| 24 | + ]); |
| 25 | + const hosts = new Map([...instances].map(([el, instance]) => [instance, el])); |
| 26 | + return { root, panel, card, anchor, instances, hosts }; |
| 27 | +} |
| 28 | + |
| 29 | +describe('blockOwnerOf', () => { |
| 30 | + afterEach(() => { |
| 31 | + document.body.innerHTML = ''; |
| 32 | + }); |
| 33 | + |
| 34 | + it('names the component that declared a projected block', () => { |
| 35 | + const { root, anchor, instances, hosts } = page(); |
| 36 | + const ng: BlockOwnerDebugNg = { |
| 37 | + getComponent: (el) => instances.get(el) ?? null, |
| 38 | + getOwningComponent: (node) => (node === anchor ? instances.get(root) : null), |
| 39 | + getHostElement: (instance) => hosts.get(instance) ?? null, |
| 40 | + }; |
| 41 | + const owner = blockOwnerOf(ng, anchor)!; |
| 42 | + expect(owner).toMatchObject({ name: 'SignalsExample', tag: 'app-root' }); |
| 43 | + expect(elementById(owner.id)).toBe(root); |
| 44 | + expect(blockOwnerOf(ng, anchor)!.id).toBe(owner.id); |
| 45 | + }); |
| 46 | + |
| 47 | + it('falls back to the nearest host above the node without getOwningComponent', () => { |
| 48 | + const { anchor, instances } = page(); |
| 49 | + const ng: BlockOwnerDebugNg = { getComponent: (el) => instances.get(el) ?? null }; |
| 50 | + expect(blockOwnerOf(ng, anchor)).toMatchObject({ name: 'ExamplePanel' }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('falls back when the utils throw or give no host element', () => { |
| 54 | + const { anchor, instances } = page(); |
| 55 | + const throwing: BlockOwnerDebugNg = { |
| 56 | + getComponent: (el) => instances.get(el) ?? null, |
| 57 | + getOwningComponent: () => { |
| 58 | + throw new Error('not in a view'); |
| 59 | + }, |
| 60 | + }; |
| 61 | + expect(blockOwnerOf(throwing, anchor)).toMatchObject({ name: 'ExamplePanel' }); |
| 62 | + const hostless: BlockOwnerDebugNg = { |
| 63 | + getComponent: (el) => instances.get(el) ?? null, |
| 64 | + getOwningComponent: () => ({}), |
| 65 | + getHostElement: () => null, |
| 66 | + }; |
| 67 | + expect(blockOwnerOf(hostless, anchor)).toMatchObject({ name: 'ExamplePanel' }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('starts the fallback above a root node that is a component host', () => { |
| 71 | + const { card, instances } = page(); |
| 72 | + const ng: BlockOwnerDebugNg = { getComponent: (el) => instances.get(el) ?? null }; |
| 73 | + expect(blockOwnerOf(ng, card)).toMatchObject({ name: 'ExamplePanel' }); |
| 74 | + expect(blockOwnerOf(ng, null)).toBeUndefined(); |
| 75 | + }); |
| 76 | +}); |
0 commit comments