|
| 1 | +import { copyTextToClipboard } from '../clipboard'; |
| 2 | + |
| 3 | +describe('copyTextToClipboard', () => { |
| 4 | + const originalClipboard = navigator.clipboard; |
| 5 | + const originalExecCommand = document.execCommand; |
| 6 | + |
| 7 | + afterEach(() => { |
| 8 | + Object.defineProperty(navigator, 'clipboard', { |
| 9 | + configurable: true, |
| 10 | + value: originalClipboard, |
| 11 | + }); |
| 12 | + document.execCommand = originalExecCommand; |
| 13 | + document.body.innerHTML = ''; |
| 14 | + jest.restoreAllMocks(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('uses the Clipboard API when it is available', async () => { |
| 18 | + const writeText = jest.fn().mockResolvedValue(undefined); |
| 19 | + Object.defineProperty(navigator, 'clipboard', { |
| 20 | + configurable: true, |
| 21 | + value: { writeText }, |
| 22 | + }); |
| 23 | + |
| 24 | + await expect(copyTextToClipboard('hello')).resolves.toBe(true); |
| 25 | + |
| 26 | + expect(writeText).toHaveBeenCalledWith('hello'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('falls back to a textarea copy when the Clipboard API is unavailable', async () => { |
| 30 | + Object.defineProperty(navigator, 'clipboard', { |
| 31 | + configurable: true, |
| 32 | + value: undefined, |
| 33 | + }); |
| 34 | + document.execCommand = jest.fn().mockReturnValue(true); |
| 35 | + const initialChildCount = document.body.childElementCount; |
| 36 | + |
| 37 | + await expect(copyTextToClipboard('fallback text')).resolves.toBe(true); |
| 38 | + |
| 39 | + expect(document.execCommand).toHaveBeenCalledWith('copy'); |
| 40 | + expect(document.body.childElementCount).toBe(initialChildCount); |
| 41 | + }); |
| 42 | + |
| 43 | + it('falls back to a textarea copy when the Clipboard API rejects', async () => { |
| 44 | + const writeText = jest.fn().mockRejectedValue(new Error('denied')); |
| 45 | + Object.defineProperty(navigator, 'clipboard', { |
| 46 | + configurable: true, |
| 47 | + value: { writeText }, |
| 48 | + }); |
| 49 | + document.execCommand = jest.fn().mockReturnValue(true); |
| 50 | + |
| 51 | + await expect(copyTextToClipboard('blocked')).resolves.toBe(true); |
| 52 | + |
| 53 | + expect(writeText).toHaveBeenCalledWith('blocked'); |
| 54 | + expect(document.execCommand).toHaveBeenCalledWith('copy'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('removes the fallback textarea when execCommand throws', async () => { |
| 58 | + Object.defineProperty(navigator, 'clipboard', { |
| 59 | + configurable: true, |
| 60 | + value: undefined, |
| 61 | + }); |
| 62 | + document.execCommand = jest.fn(() => { |
| 63 | + throw new Error('copy failed'); |
| 64 | + }); |
| 65 | + const initialChildCount = document.body.childElementCount; |
| 66 | + |
| 67 | + await expect(copyTextToClipboard('throwing copy')).resolves.toBe(false); |
| 68 | + |
| 69 | + expect(document.body.childElementCount).toBe(initialChildCount); |
| 70 | + }); |
| 71 | + |
| 72 | + it('restores the previous selection after the textarea fallback', async () => { |
| 73 | + Object.defineProperty(navigator, 'clipboard', { |
| 74 | + configurable: true, |
| 75 | + value: undefined, |
| 76 | + }); |
| 77 | + document.execCommand = jest.fn().mockReturnValue(true); |
| 78 | + const selectedText = document.createTextNode('selected text'); |
| 79 | + const container = document.createElement('div'); |
| 80 | + container.appendChild(selectedText); |
| 81 | + document.body.appendChild(container); |
| 82 | + const range = document.createRange(); |
| 83 | + range.selectNodeContents(selectedText); |
| 84 | + const selection = document.getSelection(); |
| 85 | + selection?.removeAllRanges(); |
| 86 | + selection?.addRange(range); |
| 87 | + |
| 88 | + await expect(copyTextToClipboard('copy text')).resolves.toBe(true); |
| 89 | + |
| 90 | + expect(selection?.rangeCount).toBe(1); |
| 91 | + expect(selection?.getRangeAt(0).toString()).toBe('selected text'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('still reports the copy result if selection restoration fails', async () => { |
| 95 | + Object.defineProperty(navigator, 'clipboard', { |
| 96 | + configurable: true, |
| 97 | + value: undefined, |
| 98 | + }); |
| 99 | + document.execCommand = jest.fn().mockReturnValue(true); |
| 100 | + const selectedText = document.createTextNode('selected text'); |
| 101 | + const container = document.createElement('div'); |
| 102 | + container.appendChild(selectedText); |
| 103 | + document.body.appendChild(container); |
| 104 | + const range = document.createRange(); |
| 105 | + range.selectNodeContents(selectedText); |
| 106 | + const selection = document.getSelection(); |
| 107 | + selection?.removeAllRanges(); |
| 108 | + selection?.addRange(range); |
| 109 | + const addRange = jest |
| 110 | + .spyOn(selection!, 'addRange') |
| 111 | + .mockImplementation(() => { |
| 112 | + throw new Error('range detached'); |
| 113 | + }); |
| 114 | + |
| 115 | + await expect(copyTextToClipboard('copy text')).resolves.toBe(true); |
| 116 | + |
| 117 | + expect(addRange).toHaveBeenCalled(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('reports failure when both copy methods are unavailable', async () => { |
| 121 | + Object.defineProperty(navigator, 'clipboard', { |
| 122 | + configurable: true, |
| 123 | + value: undefined, |
| 124 | + }); |
| 125 | + document.execCommand = jest.fn().mockReturnValue(false); |
| 126 | + |
| 127 | + await expect(copyTextToClipboard('nope')).resolves.toBe(false); |
| 128 | + |
| 129 | + expect(document.execCommand).toHaveBeenCalledWith('copy'); |
| 130 | + }); |
| 131 | +}); |
0 commit comments