fix(clipboard): report errors thrown by the copy fallback

This commit is contained in:
chihao.ou
2026-09-15 20:08:45 +08:00
parent 881f320269
commit 130ba634ad
2 changed files with 17 additions and 0 deletions
@@ -126,6 +126,21 @@ describe('useClipboard', () => {
expect(document.execCommand).toHaveBeenCalledWith('copy')
})
it.each([false, true])('reports a thrown fallback error (secure context: %s)', async (secure) => {
Object.defineProperty(window, 'isSecureContext', { value: secure, writable: true })
vi.mocked(navigator.clipboard.writeText).mockRejectedValue(new Error('Clipboard denied'))
Object.defineProperty(document, 'execCommand', {
configurable: true,
value: vi.fn(() => { throw new DOMException('Copy denied', 'NotAllowedError') })
})
const { copyToClipboard, copied } = useClipboard()
await expect(copyToClipboard('text')).resolves.toBe(false)
expect(copied.value).toBe(false)
expect(mockShowError).toHaveBeenCalledWith('common.copyFailed')
expect(mockShowSuccess).not.toHaveBeenCalled()
expect(document.querySelector('textarea')).toBeNull()
})
it('所有复制方式均失败时调用 showError', async () => {
const writeTextMock = navigator.clipboard.writeText as any
writeTextMock.mockRejectedValue(new Error('fail'))
+2
View File
@@ -26,6 +26,8 @@ function fallbackCopy(text: string): boolean {
textarea.setSelectionRange(0, textarea.value.length)
try {
return document.execCommand('copy')
} catch {
return false
} finally {
document.body.removeChild(textarea)
}