1- import { wrapMockAsFetch } from '@codebuff/common/testing/fixtures'
1+ import { wrapMockAsFetch , type FetchCallFn } from '@codebuff/common/testing/fixtures'
22import { QueryClient , QueryClientProvider } from '@tanstack/react-query'
33import { renderHook , waitFor } from '@testing-library/react'
44import {
@@ -12,7 +12,7 @@ import {
1212} from 'bun:test'
1313import React from 'react'
1414
15- import type { ClientEnv } from '@codebuff/common/types/contracts/env '
15+ import type { Logger } from '@codebuff/common/types/contracts/logger '
1616
1717import { useChatStore } from '../../state/chat-store'
1818import * as authModule from '../../utils/auth'
@@ -46,7 +46,7 @@ describe('fetchUsageData', () => {
4646 }
4747
4848 globalThis . fetch = wrapMockAsFetch (
49- mock (
49+ mock < FetchCallFn > (
5050 async ( ) =>
5151 new Response ( JSON . stringify ( mockResponse ) , {
5252 status : 200 ,
@@ -61,10 +61,19 @@ describe('fetchUsageData', () => {
6161 } )
6262
6363 test ( 'should throw error on failed request' , async ( ) => {
64- globalThis . fetch = wrapMockAsFetch ( mock ( async ( ) => new Response ( 'Error' , { status : 500 } ) ) )
65- const mockLogger = { error : mock ( ( ) => { } ) , warn : mock ( ( ) => { } ) , info : mock ( ( ) => { } ) , debug : mock ( ( ) => { } ) }
64+ globalThis . fetch = wrapMockAsFetch (
65+ mock < FetchCallFn > ( async ( ) => new Response ( 'Error' , { status : 500 } ) ) ,
66+ )
67+ const mockLogger : Logger = {
68+ error : mock < Logger [ 'error' ] > ( ( ) => { } ) ,
69+ warn : mock < Logger [ 'warn' ] > ( ( ) => { } ) ,
70+ info : mock < Logger [ 'info' ] > ( ( ) => { } ) ,
71+ debug : mock < Logger [ 'debug' ] > ( ( ) => { } ) ,
72+ }
6673
67- await expect ( fetchUsageData ( { authToken : 'test-token' , logger : mockLogger as any } ) ) . rejects . toThrow (
74+ await expect (
75+ fetchUsageData ( { authToken : 'test-token' , logger : mockLogger } ) ,
76+ ) . rejects . toThrow (
6877 'Failed to fetch usage: 500' ,
6978 )
7079 } )
@@ -73,7 +82,7 @@ describe('fetchUsageData', () => {
7382 await expect (
7483 fetchUsageData ( {
7584 authToken : 'test-token' ,
76- clientEnv : { NEXT_PUBLIC_CODEBUFF_APP_URL : undefined } as unknown as ClientEnv ,
85+ clientEnv : { NEXT_PUBLIC_CODEBUFF_APP_URL : undefined } ,
7786 } ) ,
7887 ) . rejects . toThrow ( 'NEXT_PUBLIC_CODEBUFF_APP_URL is not set' )
7988 } )
@@ -122,7 +131,7 @@ describe('useUsageQuery', () => {
122131 }
123132
124133 globalThis . fetch = wrapMockAsFetch (
125- mock (
134+ mock < FetchCallFn > (
126135 async ( ) =>
127136 new Response ( JSON . stringify ( mockResponse ) , {
128137 status : 200 ,
@@ -144,7 +153,7 @@ describe('useUsageQuery', () => {
144153 getAuthTokenSpy = spyOn ( authModule , 'getAuthToken' ) . mockReturnValue (
145154 'test-token' ,
146155 )
147- const fetchMock = mock ( async ( ) => new Response ( '{}' ) )
156+ const fetchMock = mock < FetchCallFn > ( async ( ) => new Response ( '{}' ) )
148157 globalThis . fetch = wrapMockAsFetch ( fetchMock )
149158
150159 const { result } = renderHook ( ( ) => useUsageQuery ( { enabled : false } ) , {
@@ -161,7 +170,7 @@ describe('useUsageQuery', () => {
161170 getAuthTokenSpy = spyOn ( authModule , 'getAuthToken' ) . mockReturnValue (
162171 undefined ,
163172 )
164- const fetchMock = mock ( async ( ) => new Response ( '{}' ) )
173+ const fetchMock = mock < FetchCallFn > ( async ( ) => new Response ( '{}' ) )
165174 globalThis . fetch = wrapMockAsFetch ( fetchMock )
166175
167176 renderHook ( ( ) => useUsageQuery ( ) , {
@@ -191,8 +200,7 @@ describe('useRefreshUsage', () => {
191200 } )
192201
193202 test . skip ( 'should invalidate usage queries' , async ( ) => {
194- const invalidateSpy = mock ( queryClient . invalidateQueries . bind ( queryClient ) )
195- queryClient . invalidateQueries = invalidateSpy as any
203+ const invalidateSpy = spyOn ( queryClient , 'invalidateQueries' )
196204
197205 const { result } = renderHook ( ( ) => useRefreshUsage ( ) , {
198206 wrapper : createWrapper ( ) ,
0 commit comments