88 lines
2.8 KiB
TypeScript
88 lines
2.8 KiB
TypeScript
import { describe, it, expect } from "vitest"
|
|
import { groupChildren } from "../../app/utils/groupChildren"
|
|
|
|
const node = (id: string, start: string | null): unknown => ({
|
|
id,
|
|
event: { id: Number(id) || 0, title: `event ${id}`, startDate: start },
|
|
trip: null,
|
|
children: [],
|
|
})
|
|
|
|
describe("groupChildren", () => {
|
|
it("splits dated children spanning two days into two labelled groups, excluding undated", () => {
|
|
const children = [
|
|
node("a", "2026-06-13T09:00:00Z"),
|
|
node("b", "2026-06-13T18:30:00Z"),
|
|
node("u", null),
|
|
node("c", "2026-06-14T10:00:00Z"),
|
|
] as never[]
|
|
|
|
const groups = groupChildren(children)
|
|
|
|
expect(groups).toHaveLength(2)
|
|
|
|
// First group: only the 13th's events, labelled for the 13th.
|
|
expect(groups[0]!.label).toBe(dayLabel("2026-06-13T09:00:00Z"))
|
|
expect(groups[0]!.events.map((e) => e.id)).toEqual(["a", "b"])
|
|
|
|
// Second group: only the 14th's event, labelled for the 14th.
|
|
expect(groups[1]!.label).toBe(dayLabel("2026-06-14T10:00:00Z"))
|
|
expect(groups[1]!.events.map((e) => e.id)).toEqual(["c"])
|
|
|
|
// Undated child is in no group.
|
|
const allIds = groups.flatMap((g) => g.events.map((e) => e.id))
|
|
expect(allIds).not.toContain("u")
|
|
})
|
|
|
|
it("returns an empty array when every child is undated", () => {
|
|
const children = [node("u1", null), node("u2", null)] as never[]
|
|
expect(groupChildren(children)).toEqual([])
|
|
})
|
|
|
|
it("returns an empty array for no children", () => {
|
|
expect(groupChildren([] as never[])).toEqual([])
|
|
})
|
|
|
|
it("keeps a single group for children all on the same day", () => {
|
|
const children = [
|
|
node("a", "2026-06-13T08:00:00Z"),
|
|
node("b", "2026-06-13T20:00:00Z"),
|
|
] as never[]
|
|
|
|
const groups = groupChildren(children)
|
|
|
|
expect(groups).toHaveLength(1)
|
|
expect(groups[0]!.events.map((e) => e.id)).toEqual(["a", "b"])
|
|
})
|
|
|
|
it("opens a fresh group when a day recurs non-consecutively", () => {
|
|
// Groups by *consecutive* dayKey, so the repeated day starts a new group.
|
|
const children = [
|
|
node("a", "2026-06-13T09:00:00Z"),
|
|
node("c", "2026-06-14T10:00:00Z"),
|
|
node("a2", "2026-06-13T11:00:00Z"),
|
|
] as never[]
|
|
|
|
const groups = groupChildren(children)
|
|
|
|
expect(groups).toHaveLength(3)
|
|
expect(groups.map((g) => g.events.map((e) => e.id))).toEqual([
|
|
["a"],
|
|
["c"],
|
|
["a2"],
|
|
])
|
|
})
|
|
})
|
|
|
|
// Local mirror of dayLabel so the expected labels track the impl without
|
|
// value-importing types or pulling the whole events module.
|
|
const dayLabel = (iso: string): string => {
|
|
const d = new Date(`${iso.slice(0, 10)}T12:00:00Z`)
|
|
const s = new Intl.DateTimeFormat("fr-FR", {
|
|
weekday: "long",
|
|
day: "numeric",
|
|
month: "long",
|
|
timeZone: "UTC",
|
|
}).format(d)
|
|
return s.charAt(0).toUpperCase() + s.slice(1)
|
|
}
|