From 17a9aa165a51cef4482d387bc8866e1b376aef35 Mon Sep 17 00:00:00 2001 From: privilegedescalation-engineer Date: Wed, 25 Mar 2026 09:03:03 +0000 Subject: [PATCH] fix test: properly mock pod selector calls to resolve immediately The withTimeout test was failing because: 1. The mock made ALL ApiProxy.request calls hang, but the implementation has 4 sequential requests (1 CRD + 3 pod selectors) each wrapped in their own withTimeout 2. Using advanceTimersByTimeAsync with hanging promises causes act() to hang because flushPromises() waits for pending promises Fix: - Use mockReturnValueOnce for the CRD call (hanging) and mockResolvedValueOnce for each pod selector call (resolves immediately) - Use synchronous advanceTimersByTime() instead of async version - Simplified test flow: check loading=true initially, advance timers, then verify crdAvailable=false and loading=false Fixes PRI-1040 --- src/api/IntelGpuDataContext.test.tsx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/api/IntelGpuDataContext.test.tsx b/src/api/IntelGpuDataContext.test.tsx index a17431f..d9e97bf 100644 --- a/src/api/IntelGpuDataContext.test.tsx +++ b/src/api/IntelGpuDataContext.test.tsx @@ -157,20 +157,20 @@ describe('IntelGpuDataProvider', () => { const nodeWrapper = { jsonData: {} }; vi.mocked(K8s.ResourceClasses.Node.useList).mockReturnValue([[nodeWrapper], null] as any); vi.mocked(K8s.ResourceClasses.Pod.useList).mockReturnValue([[nodeWrapper], null] as any); - vi.mocked(ApiProxy.request).mockReturnValue(new Promise(() => {})); + vi.mocked(ApiProxy.request) + .mockReturnValueOnce(new Promise(() => {})) + .mockResolvedValueOnce({ items: [] }) + .mockResolvedValueOnce({ items: [] }) + .mockResolvedValueOnce({ items: [] }); const { result } = renderHook(() => useIntelGpuContext(), { wrapper: Wrapper }); - await act(async () => { - await vi.advanceTimersByTimeAsync(1999); - }); expect(result.current.loading).toBe(true); - await act(async () => { - await vi.advanceTimersByTimeAsync(200); - }); - await waitFor(() => expect(result.current.loading).toBe(false)); + vi.advanceTimersByTime(2000); + await act(async () => {}); expect(result.current.crdAvailable).toBe(false); + expect(result.current.loading).toBe(false); vi.useRealTimers(); });