|
| 1 | +import { test } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { |
| 4 | + buildSnapshotNodeByIndex, |
| 5 | + findNearestAncestor, |
| 6 | + findSnapshotAncestor, |
| 7 | +} from '../snapshot-processing.ts'; |
| 8 | +import type { SnapshotNode } from '../snapshot.ts'; |
| 9 | + |
| 10 | +test('findSnapshotAncestor walks non-contiguous parent indexes until resolver returns a value', () => { |
| 11 | + const nodes: SnapshotNode[] = [ |
| 12 | + { ref: 'e10', index: 10, type: 'Window' }, |
| 13 | + { ref: 'e30', index: 30, parentIndex: 20, type: 'Text' }, |
| 14 | + { ref: 'e20', index: 20, parentIndex: 10, type: 'Cell' }, |
| 15 | + ]; |
| 16 | + const visited: number[] = []; |
| 17 | + |
| 18 | + const ancestor = findSnapshotAncestor( |
| 19 | + nodes, |
| 20 | + nodes[1]!, |
| 21 | + buildSnapshotNodeByIndex(nodes), |
| 22 | + (node) => { |
| 23 | + visited.push(node.index); |
| 24 | + return node.type === 'Window' ? node : null; |
| 25 | + }, |
| 26 | + ); |
| 27 | + |
| 28 | + assert.deepEqual(visited, [20, 10]); |
| 29 | + assert.equal(ancestor?.index, 10); |
| 30 | +}); |
| 31 | + |
| 32 | +test('findNearestAncestor resolves parents by snapshot index rather than array position', () => { |
| 33 | + const nodes: SnapshotNode[] = [ |
| 34 | + { ref: 'e10', index: 10, type: 'Window' }, |
| 35 | + { ref: 'e30', index: 30, parentIndex: 20, type: 'Text' }, |
| 36 | + { ref: 'e20', index: 20, parentIndex: 10, type: 'Cell' }, |
| 37 | + ]; |
| 38 | + |
| 39 | + const ancestor = findNearestAncestor(nodes, nodes[1]!, (node) => node.type === 'Window'); |
| 40 | + |
| 41 | + assert.equal(ancestor?.index, 10); |
| 42 | +}); |
0 commit comments