Fix tests to wrap all calls changing the UI with act. (#12268)

This commit is contained in:
Jacob Richman
2025-10-30 11:50:26 -07:00
committed by GitHub
parent cc081337b7
commit 54fa26ef0e
69 changed files with 2002 additions and 1291 deletions

View File

@@ -7,6 +7,7 @@
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
import { act, useState } from 'react';
import { renderHook } from '../../test-utils/render.js';
import { waitFor } from '../../test-utils/async.js';
import { useAtCompletion } from './useAtCompletion.js';
import type { Config, FileSearch } from '@google/gemini-cli-core';
import { FileSearchFactory } from '@google/gemini-cli-core';
@@ -74,10 +75,11 @@ describe('useAtCompletion', () => {
useTestHarnessForAtCompletion(true, '', mockConfig, testRootDir),
);
await vi.waitFor(() => {
expect(result.current.suggestions.length).toBeGreaterThan(0);
await waitFor(() => {
expect(result.current.suggestions.length).toBeGreaterThan(5);
});
expect(result.current.suggestions.length).toBeGreaterThan(0);
expect(result.current.suggestions.map((s) => s.value)).toEqual([
'src/',
'src/components/',
@@ -104,7 +106,7 @@ describe('useAtCompletion', () => {
useTestHarnessForAtCompletion(true, 'src/', mockConfig, testRootDir),
);
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.suggestions.length).toBeGreaterThan(0);
});
@@ -127,7 +129,7 @@ describe('useAtCompletion', () => {
useTestHarnessForAtCompletion(true, '', mockConfig, testRootDir),
);
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.suggestions.length).toBeGreaterThan(0);
});
@@ -164,7 +166,7 @@ describe('useAtCompletion', () => {
);
// The hook should find 'cRaZycAsE.txt' even though the pattern is 'CrAzYCaSe'.
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.suggestions.map((s) => s.value)).toEqual([
'cRaZycAsE.txt',
]);
@@ -192,12 +194,12 @@ describe('useAtCompletion', () => {
);
// It's initially true because the effect runs synchronously.
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.isLoadingSuggestions).toBe(true);
});
// Wait for the loading to complete.
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.isLoadingSuggestions).toBe(false);
});
});
@@ -212,7 +214,7 @@ describe('useAtCompletion', () => {
{ initialProps: { pattern: 'a' } },
);
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.suggestions.map((s) => s.value)).toEqual([
'a.txt',
]);
@@ -222,7 +224,7 @@ describe('useAtCompletion', () => {
rerender({ pattern: 'b' });
// Wait for the final result
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.suggestions.map((s) => s.value)).toEqual([
'b.txt',
]);
@@ -265,7 +267,7 @@ describe('useAtCompletion', () => {
);
// Wait for the initial search to complete (using real timers)
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.suggestions.map((s) => s.value)).toEqual([
'a.txt',
]);
@@ -295,7 +297,7 @@ describe('useAtCompletion', () => {
vi.useRealTimers();
// Wait for the search results to be processed
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.suggestions.map((s) => s.value)).toEqual([
'b.txt',
]);
@@ -326,7 +328,7 @@ describe('useAtCompletion', () => {
);
// Wait for the hook to be ready (initialization is complete)
await vi.waitFor(() => {
await waitFor(() => {
expect(mockFileSearch.search).toHaveBeenCalledWith(
'a',
expect.any(Object),
@@ -342,7 +344,7 @@ describe('useAtCompletion', () => {
expect(abortSpy).toHaveBeenCalledTimes(1);
// Wait for the final result, which should be from the second, faster search.
await vi.waitFor(
await waitFor(
() => {
expect(result.current.suggestions.map((s) => s.value)).toEqual(['b']);
},
@@ -369,7 +371,7 @@ describe('useAtCompletion', () => {
);
// Wait for the hook to be ready and have suggestions
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.suggestions.map((s) => s.value)).toEqual([
'a.txt',
]);
@@ -401,7 +403,7 @@ describe('useAtCompletion', () => {
);
// Wait for the hook to enter the error state
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.isLoadingSuggestions).toBe(false);
});
expect(result.current.suggestions).toEqual([]); // No suggestions on error
@@ -432,7 +434,7 @@ describe('useAtCompletion', () => {
useTestHarnessForAtCompletion(true, '', mockConfig, testRootDir),
);
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.suggestions.length).toBeGreaterThan(0);
});
@@ -453,7 +455,7 @@ describe('useAtCompletion', () => {
useTestHarnessForAtCompletion(true, '', undefined, testRootDir),
);
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.suggestions.length).toBeGreaterThan(0);
});
@@ -481,7 +483,7 @@ describe('useAtCompletion', () => {
);
// Wait for initial suggestions from the first directory
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.suggestions.map((s) => s.value)).toEqual([
'file1.txt',
]);
@@ -493,13 +495,13 @@ describe('useAtCompletion', () => {
});
// After CWD changes, suggestions should be cleared and it should load again.
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.isLoadingSuggestions).toBe(true);
expect(result.current.suggestions).toEqual([]);
});
// Wait for the new suggestions from the second directory
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.suggestions.map((s) => s.value)).toEqual([
'file2.txt',
]);
@@ -537,7 +539,7 @@ describe('useAtCompletion', () => {
),
);
await vi.waitFor(() => {
await waitFor(() => {
expect(result.current.suggestions.length).toBeGreaterThan(0);
});