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
This commit is contained in:
privilegedescalation-engineer
2026-03-25 09:03:03 +00:00
parent 3e306b70f8
commit 17a9aa165a
+8 -8
View File
@@ -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();
});