|
1 | 1 | import { describe, expect, it, vi } from 'vitest' |
2 | | -import { applyDwebKeyboardOverlay, type DwebPluginsModule } from './dweb-keyboard-overlay' |
| 2 | +import { |
| 3 | + applyDwebKeyboardOverlay, |
| 4 | + startDwebKeyboardOverlay, |
| 5 | + type DwebPluginsModule, |
| 6 | +} from './dweb-keyboard-overlay' |
3 | 7 |
|
4 | 8 | describe('dweb keyboard overlay', () => { |
5 | 9 | it('skips when current environment is not dweb', async () => { |
@@ -56,4 +60,53 @@ describe('dweb keyboard overlay', () => { |
56 | 60 |
|
57 | 61 | warnSpy.mockRestore() |
58 | 62 | }) |
| 63 | + |
| 64 | + it('retries until dweb environment is ready', async () => { |
| 65 | + vi.useFakeTimers() |
| 66 | + |
| 67 | + const setOverlay = vi.fn<(overlay: boolean) => Promise<void>>().mockResolvedValue() |
| 68 | + const loadPlugins = vi.fn<() => Promise<DwebPluginsModule>>().mockResolvedValue({ |
| 69 | + virtualKeyboardPlugin: { setOverlay }, |
| 70 | + }) |
| 71 | + |
| 72 | + let ready = false |
| 73 | + const stop = startDwebKeyboardOverlay({ |
| 74 | + isDweb: () => ready, |
| 75 | + loadPlugins, |
| 76 | + maxAttempts: 4, |
| 77 | + retryDelayMs: 100, |
| 78 | + }) |
| 79 | + |
| 80 | + expect(loadPlugins).not.toHaveBeenCalled() |
| 81 | + |
| 82 | + ready = true |
| 83 | + await vi.advanceTimersByTimeAsync(120) |
| 84 | + |
| 85 | + expect(loadPlugins).toHaveBeenCalledTimes(1) |
| 86 | + expect(setOverlay).toHaveBeenCalledWith(true) |
| 87 | + |
| 88 | + stop() |
| 89 | + vi.useRealTimers() |
| 90 | + }) |
| 91 | + |
| 92 | + it('stops retrying after cleanup', async () => { |
| 93 | + vi.useFakeTimers() |
| 94 | + |
| 95 | + const loadPlugins = vi.fn<() => Promise<DwebPluginsModule>>().mockResolvedValue({}) |
| 96 | + const stop = startDwebKeyboardOverlay({ |
| 97 | + isDweb: () => true, |
| 98 | + loadPlugins, |
| 99 | + maxAttempts: 10, |
| 100 | + retryDelayMs: 100, |
| 101 | + }) |
| 102 | + |
| 103 | + await vi.advanceTimersByTimeAsync(120) |
| 104 | + expect(loadPlugins).toHaveBeenCalledTimes(2) |
| 105 | + |
| 106 | + stop() |
| 107 | + await vi.advanceTimersByTimeAsync(500) |
| 108 | + expect(loadPlugins).toHaveBeenCalledTimes(2) |
| 109 | + |
| 110 | + vi.useRealTimers() |
| 111 | + }) |
59 | 112 | }) |
0 commit comments