|
| 1 | +import { renderHook, act } from '@testing-library/react'; |
| 2 | +import { useCopyTableToClipboard } from '../../hooks/use-copy-table-to-clipboard.hook'; |
| 3 | + |
| 4 | +type TestData = { |
| 5 | + name: string; |
| 6 | + id: string; |
| 7 | + value: string; |
| 8 | +}; |
| 9 | + |
| 10 | +describe('useCopyTableToClipboard', () => { |
| 11 | + let mockClipboardWrite: jest.Mock; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + mockClipboardWrite = jest.fn().mockResolvedValue(undefined); |
| 15 | + |
| 16 | + Object.defineProperty(navigator, 'clipboard', { |
| 17 | + value: { write: mockClipboardWrite }, |
| 18 | + writable: true, |
| 19 | + configurable: true, |
| 20 | + }); |
| 21 | + |
| 22 | + global.ClipboardItem = jest.fn( |
| 23 | + (data) => data |
| 24 | + ) as unknown as typeof ClipboardItem; |
| 25 | + |
| 26 | + jest.useFakeTimers(); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + act(() => { |
| 31 | + jest.runOnlyPendingTimers(); |
| 32 | + }); |
| 33 | + jest.useRealTimers(); |
| 34 | + jest.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should copy data in both CSV and HTML formats to clipboard', async () => { |
| 38 | + const { result } = renderHook(() => useCopyTableToClipboard<TestData>()); |
| 39 | + |
| 40 | + const testData: TestData[] = [ |
| 41 | + { name: 'Test "quoted" value', id: 'id-1', value: '100' }, |
| 42 | + { name: '<template test name>', id: 'id & value', value: '200' }, |
| 43 | + ]; |
| 44 | + |
| 45 | + await act(async () => { |
| 46 | + await result.current.copyToClipboard({ |
| 47 | + data: testData, |
| 48 | + columns: [ |
| 49 | + { key: 'name', header: 'Name' }, |
| 50 | + { key: 'id', header: 'ID' }, |
| 51 | + ], |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + expect(mockClipboardWrite).toHaveBeenCalledTimes(1); |
| 56 | + |
| 57 | + const [clipboardItem] = mockClipboardWrite.mock.calls[0][0]; |
| 58 | + const csv = clipboardItem['text/plain']; |
| 59 | + const html = clipboardItem['text/html']; |
| 60 | + |
| 61 | + const expectedCSV = [ |
| 62 | + 'Name,ID', |
| 63 | + '"Test ""quoted"" value","id-1"', |
| 64 | + '"<template test name>","id & value"', |
| 65 | + ].join('\n'); |
| 66 | + |
| 67 | + expect(csv).toEqual(expectedCSV); |
| 68 | + |
| 69 | + const expectedHTML = ` |
| 70 | + <table> |
| 71 | + <thead> |
| 72 | + <tr> |
| 73 | + <th>Name</th> |
| 74 | + <th>ID</th> |
| 75 | + </tr> |
| 76 | + </thead> |
| 77 | + <tbody> |
| 78 | + <tr> |
| 79 | + <td>Test "quoted" value</td> |
| 80 | + <td>id-1</td> |
| 81 | + </tr> |
| 82 | + <tr> |
| 83 | + <td><template test name></td> |
| 84 | + <td>id & value</td> |
| 85 | + </tr> |
| 86 | + </tbody> |
| 87 | + </table>` |
| 88 | + .replaceAll(/>\s+</g, '><') |
| 89 | + .trim(); |
| 90 | + |
| 91 | + expect(html).toEqual(expectedHTML); |
| 92 | + |
| 93 | + expect(result.current.copied).toBe(true); |
| 94 | + expect(result.current.copyError).toBeNull(); |
| 95 | + |
| 96 | + act(() => { |
| 97 | + jest.advanceTimersByTime(5000); |
| 98 | + }); |
| 99 | + |
| 100 | + expect(result.current.copied).toBe(false); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should handle clipboard write failures', async () => { |
| 104 | + mockClipboardWrite.mockRejectedValueOnce(new Error('Permission denied')); |
| 105 | + |
| 106 | + const { result } = renderHook(() => useCopyTableToClipboard<TestData>()); |
| 107 | + |
| 108 | + await act(async () => { |
| 109 | + await result.current.copyToClipboard({ |
| 110 | + data: [{ name: 'Test', id: 'id-1', value: '100' }], |
| 111 | + columns: [{ key: 'name', header: 'Name' }], |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + expect(result.current.copyError).toEqual(new Error('Permission denied')); |
| 116 | + expect(result.current.copied).toBe(false); |
| 117 | + |
| 118 | + act(() => { |
| 119 | + jest.advanceTimersByTime(5000); |
| 120 | + }); |
| 121 | + |
| 122 | + expect(result.current.copyError).toBeNull(); |
| 123 | + |
| 124 | + mockClipboardWrite.mockResolvedValueOnce(undefined); |
| 125 | + |
| 126 | + await act(async () => { |
| 127 | + await result.current.copyToClipboard({ |
| 128 | + data: [{ name: 'Test', id: 'id-1', value: '100' }], |
| 129 | + columns: [{ key: 'name', header: 'Name' }], |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + expect(result.current.copyError).toBeNull(); |
| 134 | + expect(result.current.copied).toBe(true); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should clear previous timeout when copying multiple times', async () => { |
| 138 | + const { result } = renderHook(() => useCopyTableToClipboard<TestData>()); |
| 139 | + |
| 140 | + const testData: TestData[] = [{ name: 'Test 1', id: 'id-1', value: '100' }]; |
| 141 | + |
| 142 | + await act(async () => { |
| 143 | + await result.current.copyToClipboard({ |
| 144 | + data: testData, |
| 145 | + columns: [{ key: 'name', header: 'Name' }], |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + expect(result.current.copied).toBe(true); |
| 150 | + |
| 151 | + act(() => { |
| 152 | + jest.advanceTimersByTime(2500); |
| 153 | + }); |
| 154 | + |
| 155 | + expect(result.current.copied).toBe(true); |
| 156 | + |
| 157 | + await act(async () => { |
| 158 | + await result.current.copyToClipboard({ |
| 159 | + data: testData, |
| 160 | + columns: [{ key: 'name', header: 'Name' }], |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + expect(result.current.copied).toBe(true); |
| 165 | + |
| 166 | + act(() => { |
| 167 | + jest.advanceTimersByTime(2500); |
| 168 | + }); |
| 169 | + |
| 170 | + expect(result.current.copied).toBe(true); |
| 171 | + |
| 172 | + act(() => { |
| 173 | + jest.advanceTimersByTime(2500); |
| 174 | + }); |
| 175 | + |
| 176 | + expect(result.current.copied).toBe(false); |
| 177 | + }); |
| 178 | +}); |
0 commit comments