feat(frontend): toCSV + csvFilename for data export

This commit is contained in:
Experiments DB Dev
2026-07-06 15:40:24 -04:00
parent b5578f6e24
commit dfe3938afc
2 changed files with 50 additions and 0 deletions
+30
View File
@@ -125,3 +125,33 @@ describe('buildMatrix', () => {
expect(m.columns.length).toBe(2);
});
});
import { toCSV, csvFilename } from '../src/lib/dataExport';
describe('toCSV', () => {
it('writes a header row of Date + column headers, then data rows', () => {
const matrix = {
columns: [{ id: 'a1', header: 'Alpha' }, { id: 'a2', header: 'Beta' }],
rows: [{ date: '2026-07-01', values: [5, ''] }, { date: '2026-07-02', values: ['', 9] }],
};
expect(toCSV(matrix)).toBe('Date,Alpha,Beta\n2026-07-01,5,\n2026-07-02,,9');
});
it('escapes commas, quotes, and newlines per RFC 4180', () => {
const matrix = {
columns: [{ id: 'a1', header: 'Note, field' }],
rows: [{ date: '2026-07-01', values: ['he said "hi"'] }, { date: '2026-07-02', values: ['line1\nline2'] }],
};
expect(toCSV(matrix)).toBe(
'Date,"Note, field"\n2026-07-01,"he said ""hi"""\n2026-07-02,"line1\nline2"',
);
});
});
describe('csvFilename', () => {
it('slugifies the experiment title and parameter label', () => {
expect(csvFilename('My Study #1', 'Success rate')).toBe('My-Study-1-Success-rate.csv');
});
it('falls back to "export" when both slugs are empty', () => {
expect(csvFilename('', '')).toBe('export.csv');
});
});