diff --git a/web/app/components/develop/secret-key/input-copy.tsx b/web/app/components/develop/secret-key/input-copy.tsx index 170f66050f..64703c857a 100644 --- a/web/app/components/develop/secret-key/input-copy.tsx +++ b/web/app/components/develop/secret-key/input-copy.tsx @@ -1,10 +1,10 @@ 'use client' -import copy from 'copy-to-clipboard' import { t } from 'i18next' import * as React from 'react' import { useEffect, useState } from 'react' import CopyFeedback from '@/app/components/base/copy-feedback' import Tooltip from '@/app/components/base/tooltip' +import { writeTextToClipboard } from '@/utils/clipboard' type IInputCopyProps = { value?: string @@ -39,8 +39,9 @@ const InputCopy = ({
{ - copy(value) - setIsCopied(true) + writeTextToClipboard(value).then(() => { + setIsCopied(true) + }) }} > { describe('writeTextToClipboard', () => { + /** + * Setup global mocks required for the clipboard utility tests. + * We need to mock 'isSecureContext' because the modern Clipboard API + * is only available in secure contexts. We also provide a default mock + * for 'execCommand' to prevent 'is not a function' errors in fallback tests. + */ + beforeAll(() => { + Object.defineProperty(window, 'isSecureContext', { + value: true, + writable: true, + }) + + // Provide a default mock for document.execCommand for JSDOM + document.execCommand = vi.fn().mockReturnValue(true) + }) + afterEach(() => { vi.restoreAllMocks() }) diff --git a/web/utils/clipboard.ts b/web/utils/clipboard.ts index 8e7a4495b3..f2ce93c8fe 100644 --- a/web/utils/clipboard.ts +++ b/web/utils/clipboard.ts @@ -1,5 +1,5 @@ export async function writeTextToClipboard(text: string): Promise { - if (navigator.clipboard && navigator.clipboard.writeText) + if (window.isSecureContext && navigator.clipboard && navigator.clipboard.writeText) return navigator.clipboard.writeText(text) return fallbackCopyTextToClipboard(text)