Files
eSIM-Tools/tests/modules/clipboard.test.js
Abner b042a47807 🐛 fix: 控制台 copyEsimDiagnostics 失焦时降级复制并不再抛 rejection
DevTools 调用时 Document is not focused 会导致 clipboard.writeText 失败。
Clipboard 失败后降级 execCommand;诊断导出始终打印全文,避免 unhandledrejection 与 Sentry 弹窗。
2026-07-22 19:26:12 +08:00

76 lines
2.4 KiB
JavaScript

'use strict';
/**
* clipboard 模块单元测试
*/
import { copyToClipboard } from '../../src/js/modules/clipboard.js';
describe('copyToClipboard', () => {
beforeEach(() => {
document.body.innerHTML = '';
jest.clearAllMocks();
});
it('在安全上下文中应使用 navigator.clipboard.writeText', async () => {
Object.defineProperty(window, 'isSecureContext', {
configurable: true,
value: true
});
navigator.clipboard.writeText.mockResolvedValueOnce(undefined);
const result = await copyToClipboard('hello-esim');
expect(navigator.clipboard.writeText).toHaveBeenCalledWith('hello-esim');
expect(result).toEqual({ method: 'clipboard' });
});
it('clipboard 因页面失焦失败时应降级到 execCommand', async () => {
Object.defineProperty(window, 'isSecureContext', {
configurable: true,
value: true
});
navigator.clipboard.writeText.mockRejectedValueOnce(
new DOMException('Document is not focused.', 'NotAllowedError')
);
document.execCommand = jest.fn(() => true);
const result = await copyToClipboard('from-devtools');
expect(navigator.clipboard.writeText).toHaveBeenCalledWith('from-devtools');
expect(document.execCommand).toHaveBeenCalledWith('copy');
expect(result).toEqual({ method: 'execCommand' });
expect(document.body.querySelector('textarea')).toBeNull();
});
it('非安全上下文应降级到 execCommand', async () => {
Object.defineProperty(window, 'isSecureContext', {
configurable: true,
value: false
});
document.execCommand = jest.fn(() => true);
const result = await copyToClipboard('fallback-text');
expect(navigator.clipboard.writeText).not.toHaveBeenCalled();
expect(document.execCommand).toHaveBeenCalledWith('copy');
expect(result).toEqual({ method: 'execCommand' });
expect(document.body.querySelector('textarea')).toBeNull();
});
it('两种方式都失败时应抛出 ClipboardError', async () => {
Object.defineProperty(window, 'isSecureContext', {
configurable: true,
value: true
});
navigator.clipboard.writeText.mockRejectedValueOnce(
new DOMException('Document is not focused.', 'NotAllowedError')
);
document.execCommand = jest.fn(() => false);
await expect(copyToClipboard('nope')).rejects.toMatchObject({
name: 'ClipboardError'
});
});
});