Skip to content

Commit 66ebdff

Browse files
committed
chore: add tests for entry util
1 parent c63d099 commit 66ebdff

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

apps/frontend/src/utils/entry.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
});

apps/frontend/src/utils/entry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { components } from "../api.gen";
22

3-
type Entry = components["schemas"]["QuestionnaireEntryDto"];
3+
export type Entry = components["schemas"]["QuestionnaireEntryDto"];
44
export type Gap = [string, string];
55
export type GapsPerDay = [Gap[], Gap[], Gap[], Gap[], Gap[], Gap[], Gap[]];
66

@@ -13,7 +13,7 @@ const groupByWeekday = (entries: Entry[]) =>
1313
export const resolveGaps = (entries: Entry[]) => groupByWeekday(entries).map(resolveGapsInDay) as GapsPerDay;
1414

1515
// inspired by: https://cs.stackexchange.com/questions/133276/algorithm-to-compute-the-gaps-between-a-set-of-intervals
16-
const resolveGapsInDay = (entriesOfSameDay: Entry[]) => {
16+
export const resolveGapsInDay = (entriesOfSameDay: Entry[]) => {
1717
const entriesSortedByStart = entriesOfSameDay.toSorted((a, b) => a.startedAt.localeCompare(b.startedAt));
1818

1919
const gaps: Gap[] = [];

0 commit comments

Comments
 (0)