32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|||
|
|
import { copyText } from './copyText'
|
||
|
|
|
||
|
|
describe('copyText', () => {
|
||
|
|
afterEach(() => {
|
||
|
|
vi.restoreAllMocks()
|
||
|
|
vi.unstubAllGlobals()
|
||
|
|
Reflect.deleteProperty(document, 'execCommand')
|
||
|
|
document.querySelectorAll('textarea[aria-hidden="true"]').forEach((element) => element.remove())
|
||
|
|
})
|
||
|
|
|
||
|
|
it('uses the Clipboard API when it is available', async () => {
|
||
|
|
const writeText = vi.fn().mockResolvedValue(undefined)
|
||
|
|
vi.stubGlobal('navigator', { clipboard: { writeText } })
|
||
|
|
|
||
|
|
await copyText('request-123')
|
||
|
|
|
||
|
|
expect(writeText).toHaveBeenCalledWith('request-123')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('falls back to a temporary textarea when Clipboard API access is denied', async () => {
|
||
|
|
vi.stubGlobal('navigator', { clipboard: { writeText: vi.fn().mockRejectedValue(new Error('denied')) } })
|
||
|
|
const execCommand = vi.fn().mockReturnValue(true)
|
||
|
|
Object.defineProperty(document, 'execCommand', { configurable: true, value: execCommand })
|
||
|
|
|
||
|
|
await copyText('trace-456')
|
||
|
|
|
||
|
|
expect(execCommand).toHaveBeenCalledWith('copy')
|
||
|
|
expect(document.querySelector('textarea[aria-hidden="true"]')).toBeNull()
|
||
|
|
})
|
||
|
|
})
|