mirror of
https://github.com/langgenius/dify.git
synced 2026-01-14 06:07:33 +08:00
Some checks are pending
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Waiting to run
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Blocked by required conditions
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Blocked by required conditions
Main CI Pipeline / Check Changed Files (push) Waiting to run
Main CI Pipeline / API Tests (push) Blocked by required conditions
Main CI Pipeline / Web Tests (push) Blocked by required conditions
Main CI Pipeline / Style Check (push) Waiting to run
Main CI Pipeline / VDB Tests (push) Blocked by required conditions
Main CI Pipeline / DB Migration Test (push) Blocked by required conditions
110 lines
3.4 KiB
TypeScript
110 lines
3.4 KiB
TypeScript
import { writeTextToClipboard } from './clipboard'
|
|
|
|
describe('Clipboard Utilities', () => {
|
|
describe('writeTextToClipboard', () => {
|
|
afterEach(() => {
|
|
jest.restoreAllMocks()
|
|
})
|
|
|
|
it('should use navigator.clipboard.writeText when available', async () => {
|
|
const mockWriteText = jest.fn().mockResolvedValue(undefined)
|
|
Object.defineProperty(navigator, 'clipboard', {
|
|
value: { writeText: mockWriteText },
|
|
writable: true,
|
|
configurable: true,
|
|
})
|
|
|
|
await writeTextToClipboard('test text')
|
|
expect(mockWriteText).toHaveBeenCalledWith('test text')
|
|
})
|
|
|
|
it('should fallback to execCommand when clipboard API not available', async () => {
|
|
Object.defineProperty(navigator, 'clipboard', {
|
|
value: undefined,
|
|
writable: true,
|
|
configurable: true,
|
|
})
|
|
|
|
const mockExecCommand = jest.fn().mockReturnValue(true)
|
|
document.execCommand = mockExecCommand
|
|
|
|
const appendChildSpy = jest.spyOn(document.body, 'appendChild')
|
|
const removeChildSpy = jest.spyOn(document.body, 'removeChild')
|
|
|
|
await writeTextToClipboard('fallback text')
|
|
|
|
expect(appendChildSpy).toHaveBeenCalled()
|
|
expect(mockExecCommand).toHaveBeenCalledWith('copy')
|
|
expect(removeChildSpy).toHaveBeenCalled()
|
|
})
|
|
|
|
it('should handle execCommand failure', async () => {
|
|
Object.defineProperty(navigator, 'clipboard', {
|
|
value: undefined,
|
|
writable: true,
|
|
configurable: true,
|
|
})
|
|
|
|
const mockExecCommand = jest.fn().mockReturnValue(false)
|
|
document.execCommand = mockExecCommand
|
|
|
|
await expect(writeTextToClipboard('fail text')).rejects.toThrow()
|
|
})
|
|
|
|
it('should handle execCommand exception', async () => {
|
|
Object.defineProperty(navigator, 'clipboard', {
|
|
value: undefined,
|
|
writable: true,
|
|
configurable: true,
|
|
})
|
|
|
|
const mockExecCommand = jest.fn().mockImplementation(() => {
|
|
throw new Error('execCommand error')
|
|
})
|
|
document.execCommand = mockExecCommand
|
|
|
|
await expect(writeTextToClipboard('error text')).rejects.toThrow('execCommand error')
|
|
})
|
|
|
|
it('should clean up textarea after fallback', async () => {
|
|
Object.defineProperty(navigator, 'clipboard', {
|
|
value: undefined,
|
|
writable: true,
|
|
configurable: true,
|
|
})
|
|
|
|
document.execCommand = jest.fn().mockReturnValue(true)
|
|
const removeChildSpy = jest.spyOn(document.body, 'removeChild')
|
|
|
|
await writeTextToClipboard('cleanup test')
|
|
|
|
expect(removeChildSpy).toHaveBeenCalled()
|
|
})
|
|
|
|
it('should handle empty string', async () => {
|
|
const mockWriteText = jest.fn().mockResolvedValue(undefined)
|
|
Object.defineProperty(navigator, 'clipboard', {
|
|
value: { writeText: mockWriteText },
|
|
writable: true,
|
|
configurable: true,
|
|
})
|
|
|
|
await writeTextToClipboard('')
|
|
expect(mockWriteText).toHaveBeenCalledWith('')
|
|
})
|
|
|
|
it('should handle special characters', async () => {
|
|
const mockWriteText = jest.fn().mockResolvedValue(undefined)
|
|
Object.defineProperty(navigator, 'clipboard', {
|
|
value: { writeText: mockWriteText },
|
|
writable: true,
|
|
configurable: true,
|
|
})
|
|
|
|
const specialText = 'Test\n\t"quotes"\n中文\n😀'
|
|
await writeTextToClipboard(specialText)
|
|
expect(mockWriteText).toHaveBeenCalledWith(specialText)
|
|
})
|
|
})
|
|
})
|