Skip to content

Commit 355730d

Browse files
committed
handle no entries - TODO also add a tests that works
1 parent e874d55 commit 355730d

3 files changed

Lines changed: 72 additions & 25 deletions

File tree

__tests__/setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { afterAll, afterEach, beforeAll } from "vitest";
33
// import { reset } from "drizzle-seed";
44

55
import { db } from "@/lib/db";
6-
import { NewClient, clients } from "@/lib/db/schema";
6+
import { type NewClient, clients } from "@/lib/db/schema";
77
import { sql } from "drizzle-orm";
88

99
beforeAll(async () => {

src/app/api/client/[clientId]/route.test.ts

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { describe, it, expect, vi, beforeEach } from "vitest";
22

33
import { GET } from "./route";
4-
import { db } from "@/lib/db";
5-
import { clients } from "@/lib/db/schema";
4+
// import { db } from "@/lib/db";
5+
// import { clients } from "@/lib/db/schema";
66
import time_entries from "@/../../__tests__/mocks/time_entries";
7-
import { z } from "zod";
7+
import { TogglAPI } from "@/lib/toggl";
8+
// import { z } from "zod";
89

910
// const mockClassConstructor = vi.fn().mockReturnValue({
1011
// getTimeEntries: vi.fn(() =>
@@ -15,27 +16,31 @@ import { z } from "zod";
1516
// ),
1617
// });
1718

19+
const mocks = vi.hoisted(() => {
20+
// vi.resetModules()
21+
return {
22+
TogglAPI: Object.assign(
23+
vi.fn().mockReturnValue({
24+
getTimeEntries: vi.fn(() =>
25+
// TODO mock API request using msw instead of mocking the entire function
26+
time_entries
27+
.filter((entry) => entry.workspace_id === 8997504)
28+
.filter((entry) => entry.tags?.includes("Dennis")),
29+
),
30+
}),
31+
{
32+
/**
33+
* This the mock for the static method.
34+
*/
35+
generateTogglLink: vi.fn(
36+
() => "https://track.toggl.com/reports/summary/2",
37+
),
38+
},
39+
),
40+
};
41+
});
42+
1843
// Mock dependencies
19-
vi.mock("@/lib/toggl", async (importActual) => ({
20-
TogglAPI: Object.assign(
21-
vi.fn().mockReturnValue({
22-
getTimeEntries: vi.fn(() =>
23-
// TODO mock API request using msw instead of mocking the entire function
24-
time_entries
25-
.filter((entry) => entry.workspace_id === 8997504)
26-
.filter((entry) => entry.tags?.includes("Dennis")),
27-
),
28-
}),
29-
{
30-
/**
31-
* This the mock for the static method.
32-
*/
33-
generateTogglLink: vi.fn(
34-
() => "https://track.toggl.com/reports/summary/2",
35-
),
36-
},
37-
),
38-
}));
3944

4045
describe("GET /api/client/[clientId]", () => {
4146
beforeEach(() => {
@@ -82,6 +87,9 @@ describe("GET /api/client/[clientId]", () => {
8287
const mockClient = {
8388
clientId: "123e4567-e89b-12d3-a456-426614174000",
8489
};
90+
vi.mock("@/lib/toggl", async (importActual) => ({
91+
TogglAPI: mocks.TogglAPI,
92+
}));
8593

8694
const mockTimeEntries = time_entries
8795
.filter((entry) => entry.workspace_id === 8997504)
@@ -110,4 +118,41 @@ describe("GET /api/client/[clientId]", () => {
110118
).toISOString(),
111119
});
112120
});
121+
122+
// FIXME
123+
it.skip("returns client data with no time entries", async () => {
124+
const mockClient = {
125+
clientId: "123e4567-e89b-12d3-a456-426614174000",
126+
};
127+
128+
vi.mocked(TogglAPI.prototype.getTimeEntries).mockReturnValue([]);
129+
130+
// vi.mock("@/lib/toggl", async (importActual) => ({
131+
// TogglAPI: mocks.TogglAPI,
132+
// }));
133+
134+
// vi.mock("@/lib/toggl", async (importActual) => ({
135+
// TogglAPI: {
136+
// // ...mocks.TogglAPI,
137+
138+
// getTimeEntries: vi.fn(() => []),
139+
// },
140+
// }));
141+
142+
const response = await GET(null, {
143+
params: { clientId: mockClient.clientId },
144+
});
145+
146+
expect(response.status).toBe(200);
147+
const data = await response.json();
148+
149+
expect(data).toEqual({
150+
clientId: mockClient.clientId,
151+
hoursRemaining: "16.00", // All hours remaining since no tracked time
152+
lastPaidDate: "2024-12-31T00:00:00.000Z",
153+
numTimeEntries: 0,
154+
togglLink: null,
155+
lastEntryTrackedDate: null, // No entries so no last tracked date
156+
});
157+
});
113158
});

src/app/api/client/[clientId]/route.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ export async function GET(
7777
hoursRemaining: hoursRemaining.toFixed(2),
7878
lastPaidDate: client.lastPaidDate,
7979
numTimeEntries: timeEntries.length,
80-
togglLink: TogglAPI.generateTogglLink(firstEntry.tag_ids[0]),
80+
togglLink: firstEntry
81+
? TogglAPI.generateTogglLink(firstEntry.tag_ids[0])
82+
: null,
8183
lastEntryTrackedDate: lastEntryDate?.toISOString() || null,
8284
});
8385
} catch (error) {

0 commit comments

Comments
 (0)