eletrotupi / tcc/ frontend/tests/hooks/queryKeys.unit.test.tsc901c35af9c84b6a29e5185545ced1129f33b8f4
3.4 KB | 110 lines Raw
1
import { careActionKeys } from "@/hooks/useCareAction.queries";
2
import { insightKeys } from "@/hooks/useInsights.queries";
3
import { medicineRegimenKeys } from "@/hooks/useMedicineRegimen.queries";
4
import { medicineTodayKeys } from "@/hooks/useMedicineToday.queries";
5
import { moodKeys } from "@/hooks/useMoodEntries.queries";
6
import { notificationKeys } from "@/hooks/useNotifications.queries";
7
import { sleepRecordKeys } from "@/hooks/useSleepRecord.queries";
8
import { triggerKeys } from "@/hooks/useTrigger.queries";
9

10
const nested = [
11
  { name: "moodKeys", keys: moodKeys, base: "mood-entries" },
12
  { name: "triggerKeys", keys: triggerKeys, base: "triggers" },
13
  { name: "sleepRecordKeys", keys: sleepRecordKeys, base: "sleep-records" },
14
  { name: "careActionKeys", keys: careActionKeys, base: "careActions" },
15
];
16

17
describe.each(nested)("$name", ({ keys, base }) => {
18
  it("nests the list and detail builders under the base", () => {
19
    expect(keys.all()).toStrictEqual([base]);
20
    expect(keys.lists()).toStrictEqual([base, "list"]);
21
    expect(keys.detail("9")).toStrictEqual([base, "detail", "9"]);
22
  });
23

24
  it("carries the filters into the list key", () => {
25
    expect(keys.list({ limit: 10 })).toStrictEqual([
26
      base,
27
      "list",
28
      { limit: 10 },
29
    ]);
30
    expect(keys.list()).toStrictEqual([base, "list", undefined]);
31
  });
32
});
33

34
describe("medicineRegimenKeys", () => {
35
  it("nests the unfiltered list and detail builders under the base", () => {
36
    expect(medicineRegimenKeys.all()).toStrictEqual(["medicineRegimens"]);
37
    expect(medicineRegimenKeys.lists()).toStrictEqual([
38
      "medicineRegimens",
39
      "list",
40
    ]);
41
    expect(medicineRegimenKeys.list()).toStrictEqual([
42
      "medicineRegimens",
43
      "list",
44
    ]);
45
    expect(medicineRegimenKeys.detail("2")).toStrictEqual([
46
      "medicineRegimens",
47
      "detail",
48
      "2",
49
    ]);
50
  });
51
});
52

53
describe("medicineTodayKeys", () => {
54
  it("nests a single list builder under the base", () => {
55
    expect(medicineTodayKeys.all()).toStrictEqual(["medicineToday"]);
56
    expect(medicineTodayKeys.list()).toStrictEqual(["medicineToday", "list"]);
57
  });
58
});
59

60
describe("notificationKeys", () => {
61
  it("nests the infinite builder under the list segment", () => {
62
    expect(notificationKeys.all()).toStrictEqual(["notifications"]);
63
    expect(notificationKeys.lists()).toStrictEqual(["notifications", "list"]);
64
    expect(notificationKeys.infinite()).toStrictEqual([
65
      "notifications",
66
      "list",
67
      "infinite",
68
    ]);
69
  });
70
});
71

72
describe("insightKeys", () => {
73
  beforeEach(() => {
74
    jest.useFakeTimers();
75
  });
76

77
  afterEach(() => {
78
    jest.useRealTimers();
79
  });
80

81
  it("nests the list builder under the base", () => {
82
    expect(insightKeys.all()).toStrictEqual(["insights"]);
83
    expect(insightKeys.lists()).toStrictEqual(["insights", "list"]);
84
    expect(insightKeys.list({ period: "DAILY" })).toStrictEqual([
85
      "insights",
86
      "list",
87
      { period: "DAILY" },
88
    ]);
89
  });
90

91
  it("scopes a type key to the local day and rolls it over at midnight", () => {
92
    jest.setSystemTime(new Date(2025, 0, 15, 23, 59));
93

94
    expect(insightKeys.byType("DAILY_SLEEP")).toStrictEqual([
95
      "insights",
96
      "type",
97
      "DAILY_SLEEP",
98
      "2025-01-15",
99
    ]);
100

101
    jest.setSystemTime(new Date(2025, 0, 16, 0, 1));
102

103
    expect(insightKeys.byType("DAILY_SLEEP")).toStrictEqual([
104
      "insights",
105
      "type",
106
      "DAILY_SLEEP",
107
      "2025-01-16",
108
    ]);
109
  });
110
});