Files
tauri-plugins-workspace/packages/api-e2e/test/specs/global-shortcut.spec.ts
T
Lucas Fernandes Nogueira 684feb2510 feat(tests): add api e2e tests (#3617)
* feat(tests): add api e2e tests

* lockfile

* mobile

* try linux fix [skip ci]

* ios fix

* fix test on windows

* improve cache

* fix pnpm audit [skip ci]
2026-09-22 18:58:14 -03:00

89 lines
3.4 KiB
TypeScript

// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import { expect } from '@wdio/globals'
import { tauri, tauriError, describePlugin } from '../helpers/index.js'
// Triggering a shortcut needs OS-level synthetic input the WebDriver session
// cannot produce, so the specs cover the registry (register / isRegistered /
// unregister / unregisterAll) and the error paths. The plugin is desktop-only,
// so the whole suite is skipped on mobile.
describePlugin('global-shortcut', { desktopOnly: true }, () => {
afterEach(async () => {
await tauri((api) => api.globalShortcut.unregisterAll())
})
it('register and unregister update isRegistered', async () => {
const result = await tauri(async (api) => {
const shortcut = 'CommandOrControl+Shift+F9'
const before = await api.globalShortcut.isRegistered(shortcut)
await api.globalShortcut.register(shortcut, () => {})
const registered = await api.globalShortcut.isRegistered(shortcut)
await api.globalShortcut.unregister(shortcut)
const after = await api.globalShortcut.isRegistered(shortcut)
return { before, registered, after }
})
expect(result).toEqual({ before: false, registered: true, after: false })
})
it('register accepts a list of shortcuts and unregister a list too', async () => {
const result = await tauri(async (api) => {
const shortcuts = ['Alt+Shift+F7', 'Alt+Shift+F8']
await api.globalShortcut.register(shortcuts, () => {})
const registered = await Promise.all(
shortcuts.map((s) => api.globalShortcut.isRegistered(s))
)
await api.globalShortcut.unregister(shortcuts)
const after = await Promise.all(
shortcuts.map((s) => api.globalShortcut.isRegistered(s))
)
return { registered, after }
})
expect(result.registered).toEqual([true, true])
expect(result.after).toEqual([false, false])
})
it('unregisterAll clears every registration', async () => {
const result = await tauri(async (api) => {
await api.globalShortcut.register('Alt+Shift+F5', () => {})
await api.globalShortcut.register('Alt+Shift+F6', () => {})
await api.globalShortcut.unregisterAll()
return [
await api.globalShortcut.isRegistered('Alt+Shift+F5'),
await api.globalShortcut.isRegistered('Alt+Shift+F6')
]
})
expect(result).toEqual([false, false])
})
it('shortcut names are normalized when checking registrations', async () => {
const result = await tauri(async (api) => {
await api.globalShortcut.register('CmdOrCtrl+Alt+F10', () => {})
return {
aliased: await api.globalShortcut.isRegistered(
'CommandOrControl+Alt+F10'
),
reordered: await api.globalShortcut.isRegistered('Alt+CmdOrCtrl+F10')
}
})
expect(result).toEqual({ aliased: true, reordered: true })
})
it('registering the same shortcut twice rejects', async () => {
const message = await tauriError(async (api) => {
await api.globalShortcut.register('Alt+Shift+F11', () => {})
await api.globalShortcut.register('Alt+Shift+F11', () => {})
})
expect(message).toMatch(/already registered/i)
})
it('rejects shortcuts that cannot be parsed', async () => {
const message = await tauriError((api) =>
api.globalShortcut.register('NotAKey+Nope', () => {})
)
expect(message).toMatch(/NotAKey/)
})
})