import { EXPORT_MIME, formatRowCap, pickByKey, pickDatasetFields, resolveExportFormat, resolveRowLimit, } from './export-request.util'; import { CSV_ROW_CAP, PDF_ROW_CAP, XLSX_ROW_CAP } from './tabular-export.service'; describe('resolveExportFormat', () => { it('only \'pdf\' exports as pdf', () => { expect(resolveExportFormat('pdf')).toBe('pdf'); }); it('\'csv\' exports as csv', () => { expect(resolveExportFormat('csv')).toBe('csv'); }); it.each([undefined, 'xlsx', 'doc', ''])('%p falls back to xlsx', (raw) => { expect(resolveExportFormat(raw)).toBe('xlsx'); }); }); describe('formatRowCap', () => { it('is the format\'s hard ceiling and is not caller-controllable', () => { expect(formatRowCap('xlsx')).toBe(XLSX_ROW_CAP); expect(formatRowCap('csv')).toBe(CSV_ROW_CAP); expect(formatRowCap('pdf')).toBe(PDF_ROW_CAP); }); }); describe('resolveRowLimit', () => { it('no limit means "everything, up to the cap"', () => { expect(resolveRowLimit('xlsx', undefined)).toBeUndefined(); }); it('an explicit limit is the caller asking to be truncated — kept as-is', () => { // Distinct from the cap: 100 here must yield 100 rows, not a 400, even // when the unfiltered result is far larger. expect(resolveRowLimit('pdf', '100')).toBe(100); }); it('an explicit limit over the format cap is clamped down', () => { expect(resolveRowLimit('pdf', String(PDF_ROW_CAP + 1000))).toBe(PDF_ROW_CAP); expect(resolveRowLimit('csv', String(CSV_ROW_CAP + 1))).toBe(CSV_ROW_CAP); }); it.each(['0', '-5', 'not-a-number', ''])('non-positive/invalid limit %p means no limit', (raw) => { expect(resolveRowLimit('xlsx', raw)).toBeUndefined(); }); }); describe('EXPORT_MIME', () => { it('every format has a content type and a matching extension', () => { expect(EXPORT_MIME.csv.ext).toBe('csv'); expect(EXPORT_MIME.xlsx.ext).toBe('xlsx'); expect(EXPORT_MIME.pdf.type).toBe('application/pdf'); }); }); describe('pickByKey', () => { const columns = [ { key: 'a', label: 'A', type: 'string' as const }, { key: 'b', label: 'B', type: 'number' as const }, { key: 'c', label: 'C', type: 'money' as const }, ]; it('missing fields returns every column', () => { expect(pickByKey(columns, undefined)).toEqual(columns); }); it('empty fields string returns every column', () => { expect(pickByKey(columns, '')).toEqual(columns); }); it('a known subset filters to just those, in the source\'s own order', () => { expect(pickByKey(columns, 'c,a')).toEqual([columns[0], columns[2]]); }); it('unknown keys are dropped, not passed through', () => { expect(pickByKey(columns, 'a,ghost')).toEqual([columns[0]]); }); it('all-unknown keys falls back to everything instead of a blank sheet', () => { expect(pickByKey(columns, 'ghost,also-ghost')).toEqual(columns); }); }); describe('pickDatasetFields', () => { const fields = [ { key: 'name', label: 'Company', default: true }, { key: 'tin', label: 'TIN', default: true }, { key: 'website', label: 'Website' }, { key: 'kebele', label: 'Kebele' }, ]; const defaults = [fields[0], fields[1]]; it.each([undefined, '', ' ', ','])('%p means the default fields', (raw) => { expect(pickDatasetFields(fields, raw)).toEqual(defaults); }); it('a subset is honoured, in the dataset\'s own order', () => { expect(pickDatasetFields(fields, 'kebele,name')).toEqual([fields[0], fields[3]]); }); it('asking for EVERY field exports every field', () => { // The dialog's "All columns" chip sends exactly this. Falling back to the // defaults here was the bug: 37 ticked customer columns exported as 9. expect(pickDatasetFields(fields, 'name,tin,website,kebele')).toEqual(fields); }); it('a non-default field alone is not widened back to the defaults', () => { expect(pickDatasetFields(fields, 'website')).toEqual([fields[2]]); }); it('unknown keys are dropped, the recognised ones still stand', () => { expect(pickDatasetFields(fields, 'ghost,website')).toEqual([fields[2]]); }); it('all-unknown keys fall back to the defaults, not to everything', () => { expect(pickDatasetFields(fields, 'ghost,also-ghost')).toEqual(defaults); }); it('surrounding whitespace in a hand-built fields list is tolerated', () => { expect(pickDatasetFields(fields, ' name , website ')).toEqual([fields[0], fields[2]]); }); it('a dataset with no default flags falls back to every field', () => { const flat = [{ key: 'a', label: 'A' }, { key: 'b', label: 'B' }]; expect(pickDatasetFields(flat, undefined)).toEqual(flat); }); });