fix: resolve three flaky tests (#7058)

This commit is contained in:
Arya Gummadi
2025-08-25 17:27:36 -07:00
committed by GitHub
parent 8075300e34
commit 2c6794feed
3 changed files with 69 additions and 44 deletions

View File

@@ -201,12 +201,14 @@ describe('useAtCompletion', () => {
});
await realFileSearch.initialize();
// Mock that returns results immediately but we'll control timing with fake timers
const mockFileSearch: FileSearch = {
initialize: vi.fn().mockResolvedValue(undefined),
search: vi.fn().mockImplementation(async (...args) => {
await new Promise((resolve) => setTimeout(resolve, 300));
return realFileSearch.search(...args);
}),
search: vi
.fn()
.mockImplementation(async (...args) =>
realFileSearch.search(...args),
),
};
vi.spyOn(FileSearchFactory, 'create').mockReturnValue(mockFileSearch);
@@ -216,33 +218,42 @@ describe('useAtCompletion', () => {
{ initialProps: { pattern: 'a' } },
);
// Wait for the initial (slow) search to complete
// Wait for the initial search to complete (using real timers)
await waitFor(() => {
expect(result.current.suggestions.map((s) => s.value)).toEqual([
'a.txt',
]);
});
// Now, rerender to trigger the second search
rerender({ pattern: 'b' });
// Now switch to fake timers for precise control of the loading behavior
vi.useFakeTimers();
// Wait for the loading indicator to appear
await waitFor(() => {
expect(result.current.isLoadingSuggestions).toBe(true);
// Trigger the second search
act(() => {
rerender({ pattern: 'b' });
});
// Suggestions should be cleared while loading
// Initially, loading should be false (before 200ms timer)
expect(result.current.isLoadingSuggestions).toBe(false);
// Advance time by exactly 200ms to trigger the loading state
act(() => {
vi.advanceTimersByTime(200);
});
// Now loading should be true and suggestions should be cleared
expect(result.current.isLoadingSuggestions).toBe(true);
expect(result.current.suggestions).toEqual([]);
// Wait for the final (slow) search to complete
await waitFor(
() => {
expect(result.current.suggestions.map((s) => s.value)).toEqual([
'b.txt',
]);
},
{ timeout: 1000 },
); // Increase timeout for the slow search
// Switch back to real timers for the final waitFor
vi.useRealTimers();
// Wait for the search results to be processed
await waitFor(() => {
expect(result.current.suggestions.map((s) => s.value)).toEqual([
'b.txt',
]);
});
expect(result.current.isLoadingSuggestions).toBe(false);
});