-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdweb-keyboard-overlay.test.ts
More file actions
112 lines (87 loc) · 3.08 KB
/
dweb-keyboard-overlay.test.ts
File metadata and controls
112 lines (87 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { describe, expect, it, vi } from 'vitest'
import {
applyDwebKeyboardOverlay,
startDwebKeyboardOverlay,
type DwebPluginsModule,
} from './dweb-keyboard-overlay'
describe('dweb keyboard overlay', () => {
it('skips when current environment is not dweb', async () => {
const loadPlugins = vi.fn<() => Promise<DwebPluginsModule>>()
const result = await applyDwebKeyboardOverlay({
isDweb: () => false,
loadPlugins,
})
expect(result).toBe(false)
expect(loadPlugins).not.toHaveBeenCalled()
})
it('applies overlay in dweb environment', async () => {
const setOverlay = vi.fn<(overlay: boolean) => Promise<void>>().mockResolvedValue()
const loadPlugins = vi.fn<() => Promise<DwebPluginsModule>>().mockResolvedValue({
virtualKeyboardPlugin: { setOverlay },
})
const result = await applyDwebKeyboardOverlay({
isDweb: () => true,
loadPlugins,
})
expect(result).toBe(true)
expect(loadPlugins).toHaveBeenCalledTimes(1)
expect(setOverlay).toHaveBeenCalledWith(true)
})
it('returns false when virtual keyboard plugin is unavailable', async () => {
const loadPlugins = vi.fn<() => Promise<DwebPluginsModule>>().mockResolvedValue({})
const result = await applyDwebKeyboardOverlay({
isDweb: () => true,
loadPlugins,
})
expect(result).toBe(false)
expect(loadPlugins).toHaveBeenCalledTimes(1)
})
it('returns false when plugin loading fails', async () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
const loadPlugins = vi.fn<() => Promise<DwebPluginsModule>>().mockRejectedValue(new Error('load-failed'))
const result = await applyDwebKeyboardOverlay({
isDweb: () => true,
loadPlugins,
})
expect(result).toBe(false)
expect(warnSpy).toHaveBeenCalledTimes(1)
warnSpy.mockRestore()
})
it('retries until dweb environment is ready', async () => {
vi.useFakeTimers()
const setOverlay = vi.fn<(overlay: boolean) => Promise<void>>().mockResolvedValue()
const loadPlugins = vi.fn<() => Promise<DwebPluginsModule>>().mockResolvedValue({
virtualKeyboardPlugin: { setOverlay },
})
let ready = false
const stop = startDwebKeyboardOverlay({
isDweb: () => ready,
loadPlugins,
maxAttempts: 4,
retryDelayMs: 100,
})
expect(loadPlugins).not.toHaveBeenCalled()
ready = true
await vi.advanceTimersByTimeAsync(120)
expect(loadPlugins).toHaveBeenCalledTimes(1)
expect(setOverlay).toHaveBeenCalledWith(true)
stop()
vi.useRealTimers()
})
it('stops retrying after cleanup', async () => {
vi.useFakeTimers()
const loadPlugins = vi.fn<() => Promise<DwebPluginsModule>>().mockResolvedValue({})
const stop = startDwebKeyboardOverlay({
isDweb: () => true,
loadPlugins,
maxAttempts: 10,
retryDelayMs: 100,
})
await vi.advanceTimersByTimeAsync(120)
expect(loadPlugins).toHaveBeenCalledTimes(2)
stop()
await vi.advanceTimersByTimeAsync(500)
expect(loadPlugins).toHaveBeenCalledTimes(2)
vi.useRealTimers()
})
})