|
22 | 22 | */ |
23 | 23 |
|
24 | 24 | import { render } from 'ink-testing-library'; |
25 | | -import { waitFor } from '@testing-library/react'; |
26 | 25 | import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
27 | 26 | import { SettingsDialog } from './SettingsDialog.js'; |
28 | 27 | import { LoadedSettings, SettingScope } from '../../config/settings.js'; |
@@ -152,8 +151,32 @@ vi.mock('../../utils/settingsUtils.js', async () => { |
152 | 151 | // const originalConsoleError = console.error; |
153 | 152 |
|
154 | 153 | describe('SettingsDialog', () => { |
| 154 | + // Simple delay function for remaining tests that need gradual migration |
155 | 155 | const wait = (ms = 50) => new Promise((resolve) => setTimeout(resolve, ms)); |
156 | 156 |
|
| 157 | + // Custom waitFor utility for ink testing environment (not compatible with @testing-library/react) |
| 158 | + const waitFor = async ( |
| 159 | + predicate: () => void, |
| 160 | + options: { timeout?: number; interval?: number } = {}, |
| 161 | + ) => { |
| 162 | + const { timeout = 1000, interval = 10 } = options; |
| 163 | + const start = Date.now(); |
| 164 | + let lastError: unknown; |
| 165 | + while (Date.now() - start < timeout) { |
| 166 | + try { |
| 167 | + predicate(); |
| 168 | + return; |
| 169 | + } catch (e) { |
| 170 | + lastError = e; |
| 171 | + } |
| 172 | + await new Promise((resolve) => setTimeout(resolve, interval)); |
| 173 | + } |
| 174 | + if (lastError) { |
| 175 | + throw lastError; |
| 176 | + } |
| 177 | + throw new Error('waitFor timed out'); |
| 178 | + }; |
| 179 | + |
157 | 180 | beforeEach(() => { |
158 | 181 | // Reset keypress mock state (variables are commented out) |
159 | 182 | // currentKeypressHandler = null; |
@@ -337,21 +360,36 @@ describe('SettingsDialog', () => { |
337 | 360 | </KeypressProvider> |
338 | 361 | ); |
339 | 362 |
|
340 | | - const { stdin, unmount } = render(component); |
| 363 | + const { stdin, unmount, lastFrame } = render(component); |
341 | 364 |
|
342 | | - // Press Enter to toggle current setting |
343 | | - stdin.write(TerminalKeys.DOWN_ARROW as string); |
344 | | - await wait(); |
345 | | - stdin.write(TerminalKeys.ENTER as string); |
346 | | - await wait(); |
| 365 | + // Wait for initial render and verify we're on Vim Mode (first setting) |
| 366 | + await waitFor(() => { |
| 367 | + expect(lastFrame()).toContain('● Vim Mode'); |
| 368 | + }); |
347 | 369 |
|
348 | | - // Wait for the mock to be called with more generous timeout for Windows |
349 | | - await waitFor( |
350 | | - () => { |
351 | | - expect(vi.mocked(saveModifiedSettings)).toHaveBeenCalled(); |
352 | | - }, |
353 | | - { timeout: 1000 }, |
354 | | - ); |
| 370 | + // Navigate to Disable Auto Update setting and verify we're there |
| 371 | + act(() => { |
| 372 | + stdin.write(TerminalKeys.DOWN_ARROW as string); |
| 373 | + }); |
| 374 | + await waitFor(() => { |
| 375 | + expect(lastFrame()).toContain('● Disable Auto Update'); |
| 376 | + }); |
| 377 | + |
| 378 | + // Toggle the setting |
| 379 | + act(() => { |
| 380 | + stdin.write(TerminalKeys.ENTER as string); |
| 381 | + }); |
| 382 | + // Wait for the setting change to be processed |
| 383 | + await waitFor(() => { |
| 384 | + expect( |
| 385 | + vi.mocked(saveModifiedSettings).mock.calls.length, |
| 386 | + ).toBeGreaterThan(0); |
| 387 | + }); |
| 388 | + |
| 389 | + // Wait for the mock to be called |
| 390 | + await waitFor(() => { |
| 391 | + expect(vi.mocked(saveModifiedSettings)).toHaveBeenCalled(); |
| 392 | + }); |
355 | 393 |
|
356 | 394 | expect(vi.mocked(saveModifiedSettings)).toHaveBeenCalledWith( |
357 | 395 | new Set<string>(['general.disableAutoUpdate']), |
@@ -428,12 +466,9 @@ describe('SettingsDialog', () => { |
428 | 466 | await wait(); |
429 | 467 | stdin.write(TerminalKeys.ENTER as string); |
430 | 468 | await wait(); |
431 | | - await waitFor( |
432 | | - () => { |
433 | | - expect(vi.mocked(saveModifiedSettings)).toHaveBeenCalled(); |
434 | | - }, |
435 | | - { timeout: 1000 }, |
436 | | - ); |
| 469 | + await waitFor(() => { |
| 470 | + expect(vi.mocked(saveModifiedSettings)).toHaveBeenCalled(); |
| 471 | + }); |
437 | 472 |
|
438 | 473 | expect(vi.mocked(saveModifiedSettings)).toHaveBeenCalledWith( |
439 | 474 | new Set<string>(['ui.theme']), |
@@ -468,12 +503,9 @@ describe('SettingsDialog', () => { |
468 | 503 | await wait(); |
469 | 504 | stdin.write(TerminalKeys.ENTER as string); |
470 | 505 | await wait(); |
471 | | - await waitFor( |
472 | | - () => { |
473 | | - expect(vi.mocked(saveModifiedSettings)).toHaveBeenCalled(); |
474 | | - }, |
475 | | - { timeout: 1000 }, |
476 | | - ); |
| 506 | + await waitFor(() => { |
| 507 | + expect(vi.mocked(saveModifiedSettings)).toHaveBeenCalled(); |
| 508 | + }); |
477 | 509 |
|
478 | 510 | expect(vi.mocked(saveModifiedSettings)).toHaveBeenCalledWith( |
479 | 511 | new Set<string>(['ui.theme']), |
|
0 commit comments