|
1 | | -import { describe, test, expect, mock, beforeEach, afterEach } from 'bun:test' |
| 1 | +import { describe, test, expect, mock, beforeEach, afterEach, spyOn } from 'bun:test' |
2 | 2 |
|
3 | 3 | import { createMockApiClient } from '../../__tests__/helpers/mock-api-client' |
4 | 4 | import { fetchUserDetails } from '../use-user-details-query' |
5 | 5 |
|
6 | 6 | import type { Logger } from '@codebuff/common/types/contracts/logger' |
| 7 | +import type * as CodebuffApiModule from '../../utils/codebuff-api' |
7 | 8 |
|
8 | 9 | describe('fetchUserDetails', () => { |
9 | 10 | const mockLogger: Logger = { |
@@ -179,18 +180,29 @@ describe('fetchUserDetails', () => { |
179 | 180 | }) |
180 | 181 |
|
181 | 182 | describe('environment validation', () => { |
182 | | - test('throws error when NEXT_PUBLIC_CODEBUFF_APP_URL is not set', async () => { |
183 | | - delete process.env.NEXT_PUBLIC_CODEBUFF_APP_URL |
| 183 | + test('uses shared API client when apiClient is not provided', async () => { |
| 184 | + const meMock = mock(() => |
| 185 | + Promise.resolve({ |
| 186 | + ok: true, |
| 187 | + status: 200, |
| 188 | + data: { email: 'test@example.com' }, |
| 189 | + }), |
| 190 | + ) |
| 191 | + const apiClient = createMockApiClient({ me: meMock }) |
| 192 | + |
| 193 | + const apiModule = require('../../utils/codebuff-api') as typeof CodebuffApiModule |
| 194 | + const setTokenSpy = spyOn(apiModule, 'setApiClientAuthToken') |
| 195 | + spyOn(apiModule, 'getApiClient').mockReturnValue(apiClient as any) |
184 | 196 |
|
185 | | - // When no apiClient is provided and env is not set, createCodebuffApiClient |
186 | | - // will throw due to missing baseUrl (or we test directly) |
187 | 197 | await expect( |
188 | 198 | fetchUserDetails({ |
189 | 199 | authToken: 'valid-token', |
190 | 200 | fields: ['email'] as const, |
191 | 201 | logger: mockLogger, |
192 | 202 | }), |
193 | | - ).rejects.toThrow() |
| 203 | + ).resolves.toEqual({ email: 'test@example.com' }) |
| 204 | + |
| 205 | + expect(setTokenSpy).toHaveBeenCalledWith('valid-token') |
194 | 206 | }) |
195 | 207 | }) |
196 | 208 | }) |
0 commit comments