|
| 1 | +import { |
| 2 | + getFormattedDateTime, |
| 3 | + convertActivityToCsv, |
| 4 | + saveCsvFile, |
| 5 | + CSV_KEYS |
| 6 | +} from '.././utils/ActivityCsvUtils'; |
| 7 | +import RNFS from 'react-native-fs'; |
| 8 | +import { Platform } from 'react-native'; |
| 9 | + |
| 10 | +jest.mock('react-native-fs', () => ({ |
| 11 | + DownloadDirectoryPath: '/mock/download/path', |
| 12 | + DocumentDirectoryPath: '/mock/document/path', |
| 13 | + writeFile: jest.fn() |
| 14 | +})); |
| 15 | + |
| 16 | +jest.mock('react-native', () => ({ |
| 17 | + Platform: { OS: 'android' } |
| 18 | +})); |
| 19 | + |
| 20 | +describe('activityCsvUtils', () => { |
| 21 | + describe('getFormattedDateTime', () => { |
| 22 | + it('returns a properly formatted timestamp', () => { |
| 23 | + const result = getFormattedDateTime(); |
| 24 | + expect(result).toMatch(/^\d{8}_\d{6}$/); // Example: 20250212_140719 |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('convertActivityToCsv', () => { |
| 29 | + it('correctly formats Invoice CSV data', async () => { |
| 30 | + const mockInvoices = [ |
| 31 | + { |
| 32 | + getAmount: 1500, |
| 33 | + getPaymentRequest: 'inv_req123', |
| 34 | + getRHash: 'hash_inv1', |
| 35 | + getMemo: 'Test Memo', |
| 36 | + getNote: 'Test Note', |
| 37 | + getCreationDate: '2024-02-10', |
| 38 | + formattedTimeUntilExpiry: '30 min' |
| 39 | + }, |
| 40 | + { |
| 41 | + getAmount: 3000, |
| 42 | + getPaymentRequest: 'inv_req456', |
| 43 | + getRHash: 'hash_inv2', |
| 44 | + getMemo: '', |
| 45 | + getNote: '', |
| 46 | + getCreationDate: '2024-02-11', |
| 47 | + formattedTimeUntilExpiry: '1 hour' |
| 48 | + } |
| 49 | + ]; |
| 50 | + |
| 51 | + const result = await convertActivityToCsv( |
| 52 | + mockInvoices, |
| 53 | + CSV_KEYS.invoice |
| 54 | + ); |
| 55 | + expect(result).toContain( |
| 56 | + '"1500","inv_req123","hash_inv1","Test Memo","Test Note","2024-02-10","30 min"' |
| 57 | + ); |
| 58 | + expect(result).toContain( |
| 59 | + '"3000","inv_req456","hash_inv2","","","2024-02-11","1 hour"' |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('correctly formats Payment CSV data', async () => { |
| 64 | + const mockPayments = [ |
| 65 | + { |
| 66 | + getDestination: 'dest123', |
| 67 | + getPaymentRequest: 'pay_req123', |
| 68 | + paymentHash: 'hash_pay1', |
| 69 | + getAmount: 800, |
| 70 | + getMemo: 'Payment Memo', |
| 71 | + getNote: 'Payment Note', |
| 72 | + getDate: '2024-02-09' |
| 73 | + }, |
| 74 | + { |
| 75 | + getDestination: 'dest456', |
| 76 | + getPaymentRequest: 'pay_req456', |
| 77 | + paymentHash: 'hash_pay2', |
| 78 | + getAmount: 1600, |
| 79 | + getMemo: '', |
| 80 | + getNote: '', |
| 81 | + getDate: '2024-02-08' |
| 82 | + } |
| 83 | + ]; |
| 84 | + |
| 85 | + const result = await convertActivityToCsv( |
| 86 | + mockPayments, |
| 87 | + CSV_KEYS.payment |
| 88 | + ); |
| 89 | + expect(result).toContain( |
| 90 | + '"dest123","pay_req123","hash_pay1","800","Payment Memo","Payment Note","2024-02-09"' |
| 91 | + ); |
| 92 | + expect(result).toContain( |
| 93 | + '"dest456","pay_req456","hash_pay2","1600","","","2024-02-08"' |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it('correctly formats Transaction CSV data', async () => { |
| 98 | + const mockTransactions = [ |
| 99 | + { |
| 100 | + tx: 'txhash1', |
| 101 | + getAmount: 2000, |
| 102 | + getFee: 50, |
| 103 | + getNote: 'Tx Note1', |
| 104 | + getDate: '2024-02-07' |
| 105 | + }, |
| 106 | + { |
| 107 | + tx: 'txhash2', |
| 108 | + getAmount: 5000, |
| 109 | + getFee: 100, |
| 110 | + getNote: '', |
| 111 | + getDate: '2024-02-06' |
| 112 | + } |
| 113 | + ]; |
| 114 | + |
| 115 | + const result = await convertActivityToCsv( |
| 116 | + mockTransactions, |
| 117 | + CSV_KEYS.transaction |
| 118 | + ); |
| 119 | + expect(result).toContain( |
| 120 | + '"txhash1","2000","50","Tx Note1","2024-02-07"' |
| 121 | + ); |
| 122 | + expect(result).toContain('"txhash2","5000","100","","2024-02-06"'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('handles missing fields for Invoice CSV', async () => { |
| 126 | + const mockInvoices = [{ getAmount: 1500 }]; |
| 127 | + const result = await convertActivityToCsv( |
| 128 | + mockInvoices, |
| 129 | + CSV_KEYS.invoice |
| 130 | + ); |
| 131 | + expect(result).toContain('"1500","","","","","",""'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('handles missing fields for Payment CSV', async () => { |
| 135 | + const mockPayments = [{ getDestination: 'dest123' }]; |
| 136 | + const result = await convertActivityToCsv( |
| 137 | + mockPayments, |
| 138 | + CSV_KEYS.payment |
| 139 | + ); |
| 140 | + expect(result).toContain('"dest123","","","","","",""'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('handles missing fields for Transaction CSV', async () => { |
| 144 | + const mockTransactions = [{ tx: 'txhash1', getAmount: 2000 }]; |
| 145 | + const result = await convertActivityToCsv( |
| 146 | + mockTransactions, |
| 147 | + CSV_KEYS.transaction |
| 148 | + ); |
| 149 | + expect(result).toContain('"txhash1","2000","","",""'); |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + describe('saveCsvFile', () => { |
| 154 | + beforeEach(() => { |
| 155 | + jest.clearAllMocks(); |
| 156 | + }); |
| 157 | + |
| 158 | + it('writes the CSV file to the correct path on Android', async () => { |
| 159 | + (Platform.OS as any) = 'android'; |
| 160 | + (RNFS.writeFile as jest.Mock).mockResolvedValue(undefined); |
| 161 | + |
| 162 | + await saveCsvFile('test.csv', 'mock,csv,data'); |
| 163 | + |
| 164 | + expect(RNFS.writeFile).toHaveBeenCalledWith( |
| 165 | + '/mock/download/path/test.csv', |
| 166 | + 'mock,csv,data', |
| 167 | + 'utf8' |
| 168 | + ); |
| 169 | + }); |
| 170 | + |
| 171 | + it('writes the CSV file to the correct path on iOS', async () => { |
| 172 | + (Platform.OS as any) = 'ios'; |
| 173 | + (RNFS.writeFile as jest.Mock).mockResolvedValue(undefined); |
| 174 | + |
| 175 | + await saveCsvFile('test.csv', 'mock,csv,data'); |
| 176 | + |
| 177 | + expect(RNFS.writeFile).toHaveBeenCalledWith( |
| 178 | + '/mock/document/path/test.csv', |
| 179 | + 'mock,csv,data', |
| 180 | + 'utf8' |
| 181 | + ); |
| 182 | + }); |
| 183 | + |
| 184 | + it('throws an error when file writing fails (but suppresses console error)', async () => { |
| 185 | + const consoleErrorSpy = jest |
| 186 | + .spyOn(console, 'error') |
| 187 | + .mockImplementation(() => {}); |
| 188 | + |
| 189 | + (RNFS.writeFile as jest.Mock).mockRejectedValue( |
| 190 | + new Error('File write failed') |
| 191 | + ); |
| 192 | + |
| 193 | + await expect( |
| 194 | + saveCsvFile('test.csv', 'mock,csv,data') |
| 195 | + ).rejects.toThrow('File write failed'); |
| 196 | + |
| 197 | + consoleErrorSpy.mockRestore(); |
| 198 | + }); |
| 199 | + }); |
| 200 | +}); |
0 commit comments