import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { renderHook, waitFor } from '@testing-library/react' import type { ReactNode } from 'react' import { beforeEach, describe, expect, it, vi } from 'vitest' import { getBrokerOperations } from '../api/brokerOperationApi' import { useBrokerOperations } from '../model/useBrokerOperations' vi.mock('../api/brokerOperationApi', () => ({ getBrokerOperations: vi.fn(), })) function createWrapper(queryClient?: QueryClient) { const client = queryClient ?? new QueryClient({ defaultOptions: { queries: { retry: false } } }) return function Wrapper({ children }: { children: ReactNode }) { return {children} } } describe('useBrokerOperations', () => { beforeEach(() => { vi.clearAllMocks() }) it('returns operations page data from API', async () => { vi.mocked(getBrokerOperations).mockResolvedValue({ data: { accountId: 'acc-1', items: [], nextCursor: null, hasNext: false, asOf: '2026-06-19T00:00:00.000Z', }, meta: { fromCache: false, cachedAt: null }, }) const { result } = renderHook(() => useBrokerOperations('acc-1', { limit: 5 }), { wrapper: createWrapper(), }) await waitFor(() => expect(result.current.isSuccess).toBe(true)) expect(result.current.data?.accountId).toBe('acc-1') expect(getBrokerOperations).toHaveBeenCalledWith('acc-1', { limit: 5 }) }) it('reuses the broker operations cache key across the account overview and full history pages', async () => { const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }) const cachedPage = { accountId: 'acc-1', items: [], nextCursor: null, hasNext: false, asOf: '2026-06-19T00:00:00.000Z', } queryClient.setQueryData(['broker', 'operations', 'acc-1', { limit: 5 }], cachedPage) const { result } = renderHook(() => useBrokerOperations('acc-1', { limit: 5 }), { wrapper: createWrapper(queryClient), }) await waitFor(() => expect(result.current.data).toBe(cachedPage)) expect(getBrokerOperations).not.toHaveBeenCalled() }) })