ptitlutins/test/utils/eventToNode.test.ts
2026-06-15 23:34:49 +02:00

46 lines
1.6 KiB
TypeScript

import { describe, it, expect } from "vitest"
import { eventToNode } from "../../app/utils/eventToNode"
import type { Event, Trip } from "../../app/types"
const ev = (id: number, parent: number | null, startDate: string | null = null): Event =>
({ id, title: `e${id}`, parent, startDate }) as Event
const byParentOf = (events: Event[]): Map<number | null, Event[]> => {
const m = new Map<number | null, Event[]>()
for (const e of events) {
const arr = m.get(e.parent ?? null) ?? []
arr.push(e)
m.set(e.parent ?? null, arr)
}
return m
}
describe("eventToNode", () => {
const parent = ev(1, null)
const childA = ev(2, 1, "2026-06-13T09:00:00")
const childB = ev(3, 1, "2026-06-13T08:00:00")
const byParent = byParentOf([parent, childA, childB])
const trips = new Map<number, Trip>([[1, { id: 9, mode: "train", eventStart: 1 } as Trip]])
it("references the event and stringifies its id", () => {
const node = eventToNode(parent, byParent, trips)
expect(node.id).toBe("1")
expect(node.event).toBe(parent)
})
it("recurses into children, sorted chronologically", () => {
const node = eventToNode(parent, byParent, trips)
expect(node.children.map((c) => c.id)).toEqual(["3", "2"])
})
it("folds in the trip for the matching event, null otherwise", () => {
const node = eventToNode(parent, byParent, trips)
expect(node.trip).toEqual({ mode: "train" })
expect(node.children[0]!.trip).toBeNull()
})
it("returns an empty children array for a leaf", () => {
const node = eventToNode(childA, byParent, trips)
expect(node.children).toEqual([])
})
})