forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.test.ts
More file actions
36 lines (30 loc) · 1.06 KB
/
Copy pathcsv.test.ts
File metadata and controls
36 lines (30 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { describe, expect, it } from 'vitest';
import { escapeCsvCell, toCsv } from './csv';
describe('escapeCsvCell', () => {
it('leaves simple values unchanged', () => {
expect(escapeCsvCell('Completed')).toBe('Completed');
expect(escapeCsvCell(1250)).toBe('1250');
});
it('escapes commas and quotes', () => {
expect(escapeCsvCell('Fee, monthly')).toBe('"Fee, monthly"');
expect(escapeCsvCell('He said "hello"')).toBe('"He said ""hello"""');
});
it('escapes newlines', () => {
expect(escapeCsvCell('Line one\nLine two')).toBe('"Line one\nLine two"');
});
it('normalizes nullish values to empty strings', () => {
expect(escapeCsvCell(null)).toBe('');
expect(escapeCsvCell(undefined)).toBe('');
});
});
describe('toCsv', () => {
it('builds a CRLF-delimited CSV document', () => {
const csv = toCsv(
['Date', 'Note'],
[["2025-02-20", 'Fee, monthly'], ["2025-02-21", 'He said "hello"']],
);
expect(csv).toBe(
'Date,Note\r\n2025-02-20,"Fee, monthly"\r\n2025-02-21,"He said ""hello"""',
);
});
});