|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { convertArrayToCSV, downloadCSV, escapeCsvCell } from './csvExport'; |
| 3 | + |
| 4 | +describe('escapeCsvCell', () => { |
| 5 | + it('returns empty string for null', () => { |
| 6 | + expect(escapeCsvCell(null)).toBe(''); |
| 7 | + }); |
| 8 | + |
| 9 | + it('returns empty string for undefined', () => { |
| 10 | + expect(escapeCsvCell(undefined)).toBe(''); |
| 11 | + }); |
| 12 | + |
| 13 | + it('returns plain strings unchanged', () => { |
| 14 | + expect(escapeCsvCell('hello')).toBe('hello'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('quotes values containing commas', () => { |
| 18 | + expect(escapeCsvCell('hello,world')).toBe('"hello,world"'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('quotes values containing newlines', () => { |
| 22 | + expect(escapeCsvCell('hello\nworld')).toBe('"hello\nworld"'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('doubles quotes inside values', () => { |
| 26 | + expect(escapeCsvCell('She said "Hi"')).toBe('"She said ""Hi"""'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('formats Date values via toLocaleString', () => { |
| 30 | + const original = Date.prototype.toLocaleString; |
| 31 | + Date.prototype.toLocaleString = function () { |
| 32 | + return '2026-06-01 12:00:00'; |
| 33 | + }; |
| 34 | + |
| 35 | + try { |
| 36 | + expect(escapeCsvCell(new Date('2026-06-01T12:00:00Z'))).toBe('2026-06-01 12:00:00'); |
| 37 | + } finally { |
| 38 | + Date.prototype.toLocaleString = original; |
| 39 | + } |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe('convertArrayToCSV', () => { |
| 44 | + it('returns an empty string for an empty array', () => { |
| 45 | + expect(convertArrayToCSV([])).toBe(''); |
| 46 | + }); |
| 47 | + |
| 48 | + it('serializes a single row correctly', () => { |
| 49 | + const rows = [{ name: 'Alice', amount: 10 }]; |
| 50 | + expect(convertArrayToCSV(rows)).toBe('name,amount\nAlice,10'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('serializes multiple rows with mixed values', () => { |
| 54 | + const rows = [ |
| 55 | + { id: 1, value: 'hello,world' }, |
| 56 | + { id: 2, value: 'line\nbreak' }, |
| 57 | + { id: 3, value: 'quote"test' }, |
| 58 | + ]; |
| 59 | + |
| 60 | + expect(convertArrayToCSV(rows)).toBe( |
| 61 | + 'id,value\n1,"hello,world"\n2,"line\nbreak"\n3,"quote""test"' |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('preserves header order from the first row', () => { |
| 66 | + const rows = [ |
| 67 | + { b: 1, a: 'first' }, |
| 68 | + { b: 2, a: 'second' }, |
| 69 | + ]; |
| 70 | + |
| 71 | + expect(convertArrayToCSV(rows)).toBe('b,a\n1,first\n2,second'); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe('downloadCSV', () => { |
| 76 | + let originalURL: typeof URL; |
| 77 | + |
| 78 | + beforeEach(() => { |
| 79 | + originalURL = globalThis.URL; |
| 80 | + document.body.innerHTML = ''; |
| 81 | + }); |
| 82 | + |
| 83 | + afterEach(() => { |
| 84 | + vi.restoreAllMocks(); |
| 85 | + Object.defineProperty(globalThis, 'URL', { |
| 86 | + configurable: true, |
| 87 | + writable: true, |
| 88 | + value: originalURL, |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('creates a Blob and triggers a download', () => { |
| 93 | + const createObjectURL = vi.fn(() => 'blob://test-url'); |
| 94 | + const revokeObjectURL = vi.fn(); |
| 95 | + |
| 96 | + Object.defineProperty(globalThis, 'URL', { |
| 97 | + configurable: true, |
| 98 | + writable: true, |
| 99 | + value: { |
| 100 | + ...globalThis.URL, |
| 101 | + createObjectURL, |
| 102 | + revokeObjectURL, |
| 103 | + } as unknown as typeof URL, |
| 104 | + }); |
| 105 | + |
| 106 | + const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {}); |
| 107 | + downloadCSV([{ foo: 'bar' }], 'test.csv'); |
| 108 | + |
| 109 | + expect(createObjectURL).toHaveBeenCalledTimes(1); |
| 110 | + expect(clickSpy).toHaveBeenCalledTimes(1); |
| 111 | + expect(revokeObjectURL).toHaveBeenCalledWith('blob://test-url'); |
| 112 | + expect(document.querySelector('a[download="test.csv"]')).toBeNull(); |
| 113 | + }); |
| 114 | +}); |
0 commit comments