11import { describe , it , expect , vi , beforeEach } from "vitest" ;
22
33import { 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";
66import 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
4045describe ( "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} ) ;
0 commit comments