|
| 1 | +import { beforeEach, describe, expect, it } from "vitest"; |
| 2 | +import { Entry, resolveGaps, resolveGapsInDay } from "./entry"; |
| 3 | + |
| 4 | +describe("entry utils", () => { |
| 5 | + let entries: Partial<Entry>[]; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + entries = [ |
| 9 | + { startedAt: "08:00", endedAt: "10:00", weekday: 1 }, |
| 10 | + { startedAt: "12:00", endedAt: "14:00", weekday: 1 }, |
| 11 | + ]; |
| 12 | + }); |
| 13 | + |
| 14 | + it("should detect gap between two entries", () => { |
| 15 | + const gaps = resolveGapsInDay(entries as Entry[]); |
| 16 | + |
| 17 | + expect(gaps).toEqual([["10:00", "12:00"]]); |
| 18 | + }); |
| 19 | + |
| 20 | + it("shouldn't detect gap with overlapping entries", () => { |
| 21 | + entries.push({ startedAt: "09:00", endedAt: "13:00" }); |
| 22 | + |
| 23 | + const gaps = resolveGapsInDay(entries as Entry[]); |
| 24 | + |
| 25 | + expect(gaps).toEqual([]); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should detect gap with overlapping entries", () => { |
| 29 | + entries.push({ startedAt: "09:00", endedAt: "11:00" }); |
| 30 | + |
| 31 | + const gaps = resolveGapsInDay(entries as Entry[]); |
| 32 | + |
| 33 | + expect(gaps).toEqual([["11:00", "12:00"]]); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should detect gaps with entries in diffrent sort order", () => { |
| 37 | + entries = entries.toReversed(); |
| 38 | + |
| 39 | + const gaps = resolveGapsInDay(entries as Entry[]); |
| 40 | + |
| 41 | + expect(gaps).toEqual([["10:00", "12:00"]]); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should detect multiple gaps", () => { |
| 45 | + entries.push({ startedAt: "15:00", endedAt: "16:00" }); |
| 46 | + |
| 47 | + const gaps = resolveGapsInDay(entries as Entry[]); |
| 48 | + |
| 49 | + expect(gaps.length).toBe(2); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should detect gaps for multiple weekdays", () => { |
| 53 | + entries.push({ startedAt: "08:00", endedAt: "10:00", weekday: 2 }); |
| 54 | + entries.push({ startedAt: "12:00", endedAt: "14:00", weekday: 2 }); |
| 55 | + |
| 56 | + const gapsPerDay = resolveGaps(entries as Entry[]); |
| 57 | + |
| 58 | + console.log(gapsPerDay); |
| 59 | + |
| 60 | + expect(gapsPerDay[1]).toHaveLength(1); |
| 61 | + expect(gapsPerDay[2]).toHaveLength(1); |
| 62 | + }); |
| 63 | +}); |
0 commit comments