11import { describe , it , expect , vi , beforeEach } from "vitest" ;
2+
23import { GET } from "./route" ;
34import { db } from "@/lib/db" ;
4- import { TogglAPI } from "@/lib/toggl" ;
5- import { NextRequest } from "next/server" ;
65import { clients } from "@/lib/db/schema" ;
76import time_entries from "@/../../__tests__/mocks/time_entries" ;
8-
7+ import { z } from "zod" ;
8+
9+ // const mockClassConstructor = vi.fn().mockReturnValue({
10+ // getTimeEntries: vi.fn(() =>
11+ // // TODO mock API request using msw instead of mocking the entire function
12+ // time_entries
13+ // .filter((entry) => entry.workspace_id === 8997504)
14+ // .filter((entry) => entry.tags?.includes("Dennis")),
15+ // ),
16+ // });
917// Mock dependencies
10- vi . mock ( "@/lib/toggl" , ( ) => ( {
11- TogglAPI : vi . fn ( ( ) => ( {
12- getTimeEntries : vi . fn ( ( ) => time_entries ) ,
13- } ) ) ,
18+ vi . mock ( "@/lib/toggl" , async ( importActual ) => ( {
19+ TogglAPI : Object . assign (
20+ vi . fn ( ) . mockReturnValue ( {
21+ getTimeEntries : vi . fn ( ( ) =>
22+ // TODO mock API request using msw instead of mocking the entire function
23+ time_entries
24+ . filter ( ( entry ) => entry . workspace_id === 8997504 )
25+ . filter ( ( entry ) => entry . tags ?. includes ( "Dennis" ) ) ,
26+ ) ,
27+ } ) ,
28+ {
29+ /**
30+ * This the mock for the static method.
31+ */
32+ generateTogglLink : vi . fn (
33+ ( ) => "https://track.toggl.com/reports/summary/2" ,
34+ ) ,
35+ } ,
36+ ) ,
1437} ) ) ;
1538
1639describe ( "GET /api/client/[clientId]" , ( ) => {
@@ -40,7 +63,7 @@ describe("GET /api/client/[clientId]", () => {
4063 expect ( await response . json ( ) ) . toEqual ( { error : "Client not found" } ) ;
4164 } ) ;
4265
43- it ( "returns Toggl API credentials not configured" , async ( ) => {
66+ it . skip ( "returns Toggl API credentials not configured" , async ( ) => {
4467 // const mockDb = db.select().from(clients).get;
4568 // mockDb.mockResolvedValueOnce(null);
4669
@@ -57,37 +80,33 @@ describe("GET /api/client/[clientId]", () => {
5780 it ( "returns client data with remaining hours" , async ( ) => {
5881 const mockClient = {
5982 clientId : "123e4567-e89b-12d3-a456-426614174000" ,
60- togglTag : "client-123" ,
61- totalHoursPaid : 40 ,
62- lastPaidDate : "2024-01-01T00:00:00.000Z" ,
6383 } ;
6484
65- const mockTimeEntries = [
66- {
67- duration : 3600 , // 1 hour
68- start : "2024-01-02T10:00:00.000Z" ,
69- tags : [ "client-123" ] ,
70- } ,
71- ] ;
85+ const mockTimeEntries = time_entries
86+ . filter ( ( entry ) => entry . workspace_id === 8997504 )
87+ . filter ( ( entry ) => entry . tags ?. includes ( "Dennis" ) ) ;
7288
7389 // const mockDb = db.select().from().where().get;
7490 // mockDb.mockResolvedValueOnce(mockClient);
7591
76- // const mockToggl = new TogglAPI();
77- // mockToggl.getTimeEntries.mockResolvedValueOnce(mockTimeEntries);
78-
7992 const response = await GET ( null , {
8093 params : { clientId : mockClient . clientId } ,
8194 } ) ;
8295
8396 expect ( response . status ) . toBe ( 200 ) ;
8497 const data = await response . json ( ) ;
98+
8599 expect ( data ) . toEqual ( {
86100 clientId : mockClient . clientId ,
87- hoursRemaining : 39 , // 40 paid - 1 tracked
88- lastPaidDate : mockClient . lastPaidDate ,
89- togglLink : expect . any ( String ) ,
90- lastEntryTrackedDate : mockTimeEntries [ 0 ] . start ,
101+ hoursRemaining : "2.50" , // 16 paid - 13.496 tracked
102+ lastPaidDate : "2024-12-31T00:00:00.000Z" ,
103+ numTimeEntries : 20 ,
104+ togglLink : expect . stringMatching (
105+ / ^ h t t p s : \/ \/ t r a c k \. t o g g l \. c o m \/ r e p o r t s \/ s u m m a r y \/ .* $ / ,
106+ ) ,
107+ lastEntryTrackedDate : new Date (
108+ mockTimeEntries . at ( - 1 ) . start ,
109+ ) . toISOString ( ) ,
91110 } ) ;
92111 } ) ;
93112} ) ;
0 commit comments