SPB Git forge

spb/cancerindex

Public
37commits 1branches 0releases
2.9 MBsize
maindefault branch
10 days agolast push
TypeScript 97.2% SQL 1.5% CSS 0.6% JavaScript 0.5%
2.0 KB · 44 lines typescript
Raw Blame History
1import { describe, it, expect } from 'vitest';2import { csvCell, csvLine, csvComment, csvFileName, EPI_CSV_COLUMNS } from '@/lib/explorer-csv';34describe('csvCell', () => {5  it('leaves plain values untouched and renders null/undefined as empty', () => {6    expect(csvCell('lung')).toBe('lung');7    expect(csvCell(12.5)).toBe('12.5');8    expect(csvCell(null)).toBe('');9    expect(csvCell(undefined)).toBe('');10  });11  it('quotes commas, quotes, CR and LF and doubles inner quotes', () => {12    expect(csvCell('USCS "Pancreas" (ICD-10 C25; ICD-O-3 C250-C259)')).toBe('"USCS ""Pancreas"" (ICD-10 C25; ICD-O-3 C250-C259)"');13    expect(csvCell('a,b')).toBe('"a,b"');14    expect(csvCell('line1\nline2')).toBe('"line1\nline2"');15    expect(csvCell('x\r\ny')).toBe('"x\r\ny"');16  });17  it('serializes dates as ISO-8601', () => {18    expect(csvCell(new Date('2026-09-08T12:22:48.904Z'))).toBe('2026-09-08T12:22:48.904Z');19  });20});2122describe('csvLine / csvComment', () => {23  it('joins cells with commas', () => {24    expect(csvLine(['a', 1, null, 'b,c'])).toBe('a,1,,"b,c"');25  });26  it('keeps comments on one line', () => {27    expect(csvComment('Source: X\nlicense: Y')).toBe('# Source: X license: Y');28  });29});3031describe('columns and file name', () => {32  it('exports provenance id and source URL columns', () => {33    expect(EPI_CSV_COLUMNS).toContain('provenance_id');34    expect(EPI_CSV_COLUMNS).toContain('source_url');35    expect(EPI_CSV_COLUMNS).toContain('standard_population');36    expect(new Set(EPI_CSV_COLUMNS).size).toBe(EPI_CSV_COLUMNS.length);37  });38  it('builds a safe file name', () => {39    expect(csvFileName(['mortality_count', 'united-states', 'all', 'all', 1999, 2024, '2026-09-11'])).toBe('cancerindex-epidemiology-mortality-count-united-states-all-all-1999-2024-2026-09-11.csv');40    expect(csvFileName([null, '', undefined])).toBe('cancerindex-epidemiology-export.csv');41    expect(csvFileName(['../etc/passwd'])).toBe('cancerindex-epidemiology-etc-passwd.csv');42  });43});44