|
1 | 1 | import { describe, expect, it } from 'vitest'; |
2 | 2 | import { collectComponentTree, hostPath, type ComponentDebugNg } from '../component-tree.ts'; |
3 | 3 | import { elementId } from '../element-id.ts'; |
4 | | -import type { HostTree } from '../host-tree.ts'; |
| 4 | +import { hostBySelector, type HostTree } from '../host-tree.ts'; |
5 | 5 | import { collectInjectorTree } from '../injector-tree.ts'; |
6 | 6 | import { createNgrxCollector } from '../ngrx-collector.ts'; |
| 7 | +import { collectSignalGraph, type SignalDebugNg } from '../signal-graph.ts'; |
7 | 8 |
|
8 | 9 | interface View { |
9 | 10 | type: string; |
@@ -105,4 +106,45 @@ describe('collectors over a non-DOM host tree', () => { |
105 | 106 | expect(seen.slice(0, 5)).toEqual(['App', 'List', 'StackLayout', 'Row', 'Row']); |
106 | 107 | collector.stop(); |
107 | 108 | }); |
| 109 | + |
| 110 | + it('finds a host by selector without reordering the tree', () => { |
| 111 | + const withSelectors: HostTree<View> = { |
| 112 | + ...tree, |
| 113 | + selector: (host) => { |
| 114 | + const parent = host.parent; |
| 115 | + const own = parent |
| 116 | + ? `${host.type}:nth-child(${parent.children.indexOf(host) + 1})` |
| 117 | + : host.type; |
| 118 | + return parent ? `${withSelectors.selector!(parent)} > ${own}` : own; |
| 119 | + }, |
| 120 | + }; |
| 121 | + const order = () => list.children[0]!.children.map((host) => host === first); |
| 122 | + expect(order()).toEqual([true, false]); |
| 123 | + expect( |
| 124 | + hostBySelector( |
| 125 | + withSelectors, |
| 126 | + 'App > List:nth-child(1) > StackLayout:nth-child(1) > Row:nth-child(2)', |
| 127 | + ), |
| 128 | + ).toBe(second); |
| 129 | + expect(hostBySelector(withSelectors, 'App > Nope')).toBeNull(); |
| 130 | + expect(order()).toEqual([true, false]); |
| 131 | + expect(hostBySelector(tree, 'App')).toBeNull(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('collects the signal graph of a host found by id', () => { |
| 135 | + const signals: SignalDebugNg<View> = { |
| 136 | + ...ng, |
| 137 | + ɵgetSignalGraph: (injector) => |
| 138 | + (injector as { host?: View }).host === second |
| 139 | + ? { |
| 140 | + nodes: [{ id: '2', kind: 'signal', label: 'label', epoch: 1, value: 'second' }], |
| 141 | + edges: [], |
| 142 | + } |
| 143 | + : { nodes: [], edges: [] }, |
| 144 | + }; |
| 145 | + const graph = collectSignalGraph(signals, { id: elementId(second) }, tree)!; |
| 146 | + expect(graph.source).toBe('selected'); |
| 147 | + expect(graph.component).toMatchObject({ name: 'Row', path: 'App > List > Row[2]' }); |
| 148 | + expect(graph.nodes.map((n) => n.id)).toEqual(['2']); |
| 149 | + }); |
108 | 150 | }); |
0 commit comments