ptitlutins/test/utils/siblingsOf.test.ts
2026-06-10 23:37:21 +02:00

45 lines
1.4 KiB
TypeScript

import { describe, it, expect } from "vitest"
import { siblingsOf } from "../../app/utils/siblingsOf"
const node = (id: string, children: unknown[] = []) =>
({
id,
title: id,
start: null,
end: null,
location: null,
coord: null,
trip: null,
children,
}) as unknown as import("~/types").Node
const childB = node("b")
const grandchild = node("a1")
const childAWithKid = node("a", [grandchild])
const root = node("__voyage", [childAWithKid, childB])
describe("siblingsOf", () => {
it("returns [root] for an empty path (depth 0)", () => {
expect(siblingsOf(root, [])).toEqual([root])
})
it("returns [root] when the path does not resolve past the root", () => {
expect(siblingsOf(root, ["missing"])).toEqual([root])
})
it("returns the parent's children (incl. the node) for a path to a child", () => {
const siblings = siblingsOf(root, ["a"])
expect(siblings).toEqual([childAWithKid, childB])
expect(siblings.map((n) => n.id)).toContain("a")
})
it("returns the parent's children for a path to a grandchild", () => {
const siblings = siblingsOf(root, ["a", "a1"])
expect(siblings).toEqual([grandchild])
expect(siblings.map((n) => n.id)).toContain("a1")
})
it("uses the resolvable prefix when the path breaks midway", () => {
expect(siblingsOf(root, ["a", "nope"])).toEqual([childAWithKid, childB])
})
})