|
1 | | -import { copyToClipboard } from './ClipboardUtils'; |
| 1 | +import { copyToClipboard, readFromClipboard } from './ClipboardUtils'; |
| 2 | +import { |
| 3 | + ClipboardPermissionsDeniedError, |
| 4 | + ClipboardUnavailableError, |
| 5 | +} from './errors'; |
| 6 | +import { checkPermission } from './PermissionUtils'; |
2 | 7 |
|
3 | 8 | document.execCommand = jest.fn(); |
4 | 9 |
|
| 10 | +jest.mock('./PermissionUtils', () => ({ |
| 11 | + checkPermission: jest.fn(), |
| 12 | +})); |
| 13 | + |
5 | 14 | describe('Clipboard', () => { |
6 | 15 | describe('writeText', () => { |
7 | 16 | beforeEach(() => jest.resetAllMocks()); |
@@ -55,4 +64,113 @@ describe('Clipboard', () => { |
55 | 64 | await expect(document.execCommand).toHaveBeenCalledWith('copy'); |
56 | 65 | }); |
57 | 66 | }); |
| 67 | + |
| 68 | + describe('readFromClipboard', () => { |
| 69 | + beforeEach(() => jest.resetAllMocks()); |
| 70 | + |
| 71 | + it('should throw unavailable error if clipboard is undefined', async () => { |
| 72 | + Object.assign(navigator, { |
| 73 | + clipboard: undefined, |
| 74 | + }); |
| 75 | + |
| 76 | + await expect(readFromClipboard()).rejects.toThrow( |
| 77 | + ClipboardUnavailableError |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should throw unavailable error if PermissionState is null', async () => { |
| 82 | + (checkPermission as jest.Mock).mockResolvedValue(null); |
| 83 | + |
| 84 | + await expect(readFromClipboard()).rejects.toThrow( |
| 85 | + ClipboardUnavailableError |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should return text if PermissionState is granted', async () => { |
| 90 | + Object.assign(navigator, { |
| 91 | + clipboard: { |
| 92 | + readText: jest.fn().mockResolvedValueOnce('text from clipboard'), |
| 93 | + }, |
| 94 | + }); |
| 95 | + |
| 96 | + (checkPermission as jest.Mock).mockResolvedValueOnce('granted'); |
| 97 | + |
| 98 | + await expect(readFromClipboard()).resolves.toBe('text from clipboard'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should throw denied error if PermissionState is denied', async () => { |
| 102 | + Object.assign(navigator, { |
| 103 | + clipboard: { |
| 104 | + readText: jest.fn(), |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + (checkPermission as jest.Mock).mockResolvedValue('denied'); |
| 109 | + |
| 110 | + await expect(readFromClipboard()).rejects.toThrow( |
| 111 | + ClipboardPermissionsDeniedError |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should return text if permission prompt accepted', async () => { |
| 116 | + const mockClipboard = { |
| 117 | + readText: jest |
| 118 | + .fn() |
| 119 | + .mockRejectedValueOnce(new Error('Missing permission')) |
| 120 | + .mockResolvedValueOnce('text from clipboard'), |
| 121 | + }; |
| 122 | + |
| 123 | + Object.assign(navigator, { |
| 124 | + clipboard: mockClipboard, |
| 125 | + }); |
| 126 | + |
| 127 | + (checkPermission as jest.Mock) |
| 128 | + .mockResolvedValueOnce('prompt') |
| 129 | + .mockResolvedValue('granted'); |
| 130 | + |
| 131 | + await expect(readFromClipboard()).resolves.toBe('text from clipboard'); |
| 132 | + expect(checkPermission).toHaveBeenCalledTimes(2); |
| 133 | + expect(mockClipboard.readText).toHaveBeenCalledTimes(2); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should throw denied error if permission prompt denied', async () => { |
| 137 | + const mockClipboard = { |
| 138 | + readText: jest |
| 139 | + .fn() |
| 140 | + .mockRejectedValueOnce(new Error('Missing permission')), |
| 141 | + }; |
| 142 | + |
| 143 | + Object.assign(navigator, { |
| 144 | + clipboard: mockClipboard, |
| 145 | + }); |
| 146 | + |
| 147 | + (checkPermission as jest.Mock) |
| 148 | + .mockResolvedValueOnce('prompt') |
| 149 | + .mockResolvedValue('denied'); |
| 150 | + |
| 151 | + await expect(readFromClipboard()).rejects.toThrow( |
| 152 | + ClipboardPermissionsDeniedError |
| 153 | + ); |
| 154 | + expect(checkPermission).toHaveBeenCalledTimes(2); |
| 155 | + expect(mockClipboard.readText).toHaveBeenCalledTimes(1); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should throw denied error if permission prompt closed', async () => { |
| 159 | + const mockClipboard = { |
| 160 | + readText: jest.fn().mockRejectedValue(new Error('Missing permission')), |
| 161 | + }; |
| 162 | + |
| 163 | + Object.assign(navigator, { |
| 164 | + clipboard: mockClipboard, |
| 165 | + }); |
| 166 | + |
| 167 | + (checkPermission as jest.Mock).mockResolvedValue('prompt'); |
| 168 | + |
| 169 | + await expect(readFromClipboard()).rejects.toThrow( |
| 170 | + ClipboardPermissionsDeniedError |
| 171 | + ); |
| 172 | + expect(checkPermission).toHaveBeenCalledTimes(2); |
| 173 | + expect(mockClipboard.readText).toHaveBeenCalledTimes(1); |
| 174 | + }); |
| 175 | + }); |
58 | 176 | }); |
0 commit comments