forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontextMenu.test.ts
More file actions
143 lines (122 loc) · 4.49 KB
/
Copy pathcontextMenu.test.ts
File metadata and controls
143 lines (122 loc) · 4.49 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { BrowserWindow, ContextMenuParams } from 'electron'
type Item = { role?: string; type?: string; label?: string; click?: (...args: unknown[]) => void }
const popup = vi.fn()
const buildFromTemplate = vi.fn((_template: Item[]) => ({ popup }))
const writeText = vi.fn()
const openExternal = vi.fn()
vi.mock('electron', () => ({
Menu: { buildFromTemplate: (t: Item[]) => buildFromTemplate(t) },
clipboard: { writeText: (s: string) => writeText(s) },
shell: { openExternal: (u: string) => openExternal(u) }
}))
const { installContextMenu } = await import('./contextMenu')
type Handler = (e: unknown, p: ContextMenuParams) => void
/** A window that just records the 'context-menu' handler installed on it. */
function fakeWindow(): { win: BrowserWindow; fire: (p: Partial<ContextMenuParams>) => void } {
let handler: Handler | undefined
const win = {
webContents: {
on: (event: string, cb: Handler) => {
if (event === 'context-menu') handler = cb
}
}
} as unknown as BrowserWindow
const fire = (p: Partial<ContextMenuParams>): void => {
if (!handler) throw new Error('no context-menu handler installed')
handler({}, {
isEditable: false,
selectionText: '',
linkURL: '',
misspelledWord: '',
dictionarySuggestions: [],
editFlags: {
canUndo: false,
canRedo: false,
canCut: false,
canCopy: false,
canPaste: false,
canDelete: false,
canSelectAll: false,
canEditRichly: false
},
...p
} as ContextMenuParams)
}
return { win, fire }
}
const lastTemplate = (): Item[] => {
const t = buildFromTemplate.mock.calls.at(-1)?.[0]
if (!t) throw new Error('Menu.buildFromTemplate was never called')
return t
}
const roles = (): (string | undefined)[] => lastTemplate().map((i) => i.role ?? i.type)
const ids = (): (string | undefined)[] => lastTemplate().map((i) => i.role ?? i.type ?? i.label)
describe('installContextMenu', () => {
beforeEach(() => {
popup.mockClear()
buildFromTemplate.mockClear()
writeText.mockClear()
openExternal.mockClear()
})
it('pops a native menu over the owning window on a right-click in a text field', () => {
const { win, fire } = fakeWindow()
installContextMenu(win)
fire({
isEditable: true,
selectionText: 'hi',
editFlags: {
canUndo: true,
canRedo: false,
canCut: true,
canCopy: true,
canPaste: true,
canDelete: true,
canSelectAll: true,
canEditRichly: true
} as ContextMenuParams['editFlags']
})
expect(buildFromTemplate).toHaveBeenCalledTimes(1)
expect(roles()).toEqual([
'undo',
'separator',
'cut',
'copy',
'paste',
'delete',
'separator',
'selectAll'
])
// Must be popped OVER the window it came from — a menu with no owner window
// can appear on the wrong monitor and won't dismiss with the window.
expect(popup).toHaveBeenCalledWith({ window: win })
})
it('pops NOTHING when there is nothing to offer (never show an empty menu)', () => {
const { win, fire } = fakeWindow()
installContextMenu(win)
fire({ isEditable: false, selectionText: '' }) // read-only, no selection
expect(buildFromTemplate).not.toHaveBeenCalled()
expect(popup).not.toHaveBeenCalled()
})
it('offers Open Link / Copy Link Address on a right-clicked http/https link and wires them to electron', () => {
const { win, fire } = fakeWindow()
installContextMenu(win)
fire({ linkURL: 'https://omi.me/x' })
expect(ids()).toEqual(['Open Link', 'Copy Link Address'])
const t = lastTemplate()
// "Open Link" hands the URL to the OS browser via the scheme-checked opener.
t.find((i) => i.label === 'Open Link')?.click?.()
expect(openExternal).toHaveBeenCalledWith('https://omi.me/x')
// "Copy Link Address" copies the raw href to the clipboard.
t.find((i) => i.label === 'Copy Link Address')?.click?.()
expect(writeText).toHaveBeenCalledWith('https://omi.me/x')
expect(popup).toHaveBeenCalledWith({ window: win })
})
it('shows no link menu and never opens the OS for a non-web link (file://)', () => {
const { win, fire } = fakeWindow()
installContextMenu(win)
fire({ linkURL: 'file:///etc/passwd' }) // read-only, no selection, disallowed scheme
expect(buildFromTemplate).not.toHaveBeenCalled()
expect(openExternal).not.toHaveBeenCalled()
})
})