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

51 lines
1.5 KiB
TypeScript

import { describe, it, expect } from "vitest"
import type { Event } from "~/types"
import { byStart } from "../../app/utils/byStart"
const ev = (startDate?: string | null): Event => ({ startDate }) as Event
describe("byStart", () => {
it("sorts two dated events ascending by startDate", () => {
const a = ev("2026-06-10T08:00:00Z")
const b = ev("2026-06-11T08:00:00Z")
expect(byStart(a, b)).toBe(-1)
expect(byStart(b, a)).toBe(1)
})
it("returns 0 for two dated events with the same startDate", () => {
const a = ev("2026-06-10T08:00:00Z")
const b = ev("2026-06-10T08:00:00Z")
expect(byStart(a, b)).toBe(0)
})
it("places a dated event before an undated one", () => {
const dated = ev("2026-06-10T08:00:00Z")
const undated = ev(null)
expect(byStart(dated, undated)).toBe(-1)
expect(byStart(undated, dated)).toBe(1)
})
it("keeps order for two undated events (returns 0)", () => {
const a = ev(null)
const b = ev(undefined)
expect(byStart(a, b)).toBe(0)
})
it("sorts a full array: dated chronological first, undated last", () => {
const arr = [
ev(null),
ev("2026-06-12T08:00:00Z"),
ev("2026-06-10T08:00:00Z"),
ev(undefined),
ev("2026-06-11T08:00:00Z"),
]
const sorted = arr.slice().sort(byStart)
expect(sorted.map((e) => e.startDate)).toEqual([
"2026-06-10T08:00:00Z",
"2026-06-11T08:00:00Z",
"2026-06-12T08:00:00Z",
null,
undefined,
])
})
})